This question is in some way related to the below linked question. However, I need a little more clarity on some aspects and some additional information. Refer: REST Web Service authentication token implementation
Background:
- I need to implement security for a REST Web Service using token
- The webservice is intended for use with Java client. Hence, form authentication and popups for credentials are not useful.
- I'm new to REST security and encryption
This is what I have understood till now:
For first request:
- User establishes https connection (or container ensures https using 301)
- User POSTs username and password to login service
- If credentials are valid we:
- Generate a random temporary token
- Store the random token on server mapping it to actual username
- Encrypt the token using a symmetric key only known to server
- Hash the encrypted token
- Send the encrypted token and the hash to the client
For subsequent requests:
- Client sends this encrypted token and hash combination (using username field of basic?)
- We make sure the encrypted token is not tampered using the hash and then decrypt it
- We check the decrypted token in the session-tracking-table for a not-expired entry and get the actual username (expiry to be managed by code?)
- If the username is found, based on allowed roles, allowed operations are configured
More details:
- Since client is a java client, the first request can be a POST containing the credentials. However, this looks like it may expose the credentials before the https gets established. Hence should there be a dummy GET to a secured resource so that https is established first?
- Assuming above is required, the second request is a LoginAction POST with credentials. This request is handled manually (not using container's authorisation). Is this right?
- The above LoginAction returns the user the combination of encrypted token + hash
- User sets it to the header that is used by BASIC authentication mechanism (field username)
- We implement a JAASRealm to decrypt and validate the token, and find the roles allowed
- The rest of authorisation process is taken care of by the container with the WebResourceCollection defined in the web.xml
Is this the correct approach?