Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service-Based Authentication Using Tokens

I'm having a tough time trying to find clear and concise examples of how one would implement a service-based authentication scheme using tokens. As far as I can tell, the basic steps are as follows:

  1. Client requests username/password from user
  2. Client passes username/password to identity provider
  3. Provider checks username/password and sends back a token if the user is valid
  4. Client does something with the token?

The third and fourth step are where I'm getting stuck. I assume the "token" in this case just has to be either an encrypted string that the client can decrypt or some random string that gets stored somewhere (i.e. a database) that the client can then verify against, but I'm not really sure what the client is then supposed to do with the token or why you even need a token at all -- couldn't a simple user ID also suffice?

like image 979
jerhinesmith Avatar asked Jun 02 '09 14:06

jerhinesmith


People also ask

What are token based authentication?

Token-based authentication is a protocol which allows users to verify their identity, and in return receive a unique access token.

What are the types of token based authentication?

The most common types of tokens are key fobs and USB or wireless tokens. Hardware tokens can be divided into three categories. Contactless—a contactless token doesn't require you to enter an access code or connect to a device.

Is OAuth token based authentication?

Connections Mobile supports OAuth 2.0 token-based authentication using the internet standard RFC 6749 – The OAuth 2.0 Authorization Framework. Because Connections Mobile is a public application available on public app stores, it implements the Authorization Code Grant Flow to an Authorization Server.

Is SSO token based authentication?

An SSO token is data, such as the user's login email address, that is passed from one system to another during the SSO process. Using a token-based authentication method, users verify their data and then receive a unique access token (created using the Skilljar API - see below), allowing them to log in.


2 Answers

I assume the "token" in this case just has to be either an encrypted string that the client can decrypt or some random string that gets stored somewhere (i.e. a database) that the client can then verify against, but I'm not really sure what the client is then supposed to do with the token or why you even need a token at all -- couldn't a simple user ID also suffice?

No - the token is a "ticket to ride." Just like a subway token. The client presents it to the gatekeeper when requesting service. In this case the provider does its own authentication, so the client presents the token back to the provider. In some cases the provider might delegate authentication - for example in the STS model, in which case the provider might hand the token off to a third party for authentication and even authorization.

From the service point of view, this token must:

  • have a "shelf life". Otherwise, the token would be infinitely re-usable. So on the server-side, you can store the token in a session-based store, where you will get timeout for free. Or you can build a simple hashtable with expiration.
  • be associated solely to the holder. In many cases the provider uses an approximation here and asserts that the token can be used only from the original requesting IP address.

So in step 3, the provider needs to check the username and password. If that validates, then create a token (hash), which refers to an entry in a Dictionary or Hashtable. The objects in the Hashtable are structures containing the username, the IP address, probably the original time-of-issuance, maybe the roles associated to the username, and whatever else you want to store. The service provider sends back this token - the hash, not the structure - to the client, typically as a Set-Cookie. When the client sends back the token (as a Cookie) on subsequent requests, the provider checks the dictionary to see if the token is available, has not timed out, matches the requesting IP address, is authorized for the requested resource, etc. If everything is ok, honor the request.


EDIT 2013 June

It's been several years now, and this answer is still getting votes. I'd suggest that people look into the OAuth 2.0 Framework, and Bearer tokens.

Basically they do just what is described here.

If you want a good example implementation, you can look at Apigee's Usergrid. It works this way:

  1. User Authenticates

    POST https://api.usergrid.com/token -d '{"username":"Joe","password":"Sec4et!","grant_type" : "password"}'

  2. User receives an access_token in response

  3. User makes subsequent calls with header Authorization: Bearer XXXXXXXXXX where XXXXX is replaced with the bearer token. This token has a Time-to-Live set by the usergrid server.

like image 145
Cheeso Avatar answered Nov 03 '22 06:11

Cheeso


A typical token is a random hash that's stored on the server side. The client passes it back with its requests. What this gets you is that you don't have to pass around the password all the time, which is obviously all to the good, and the token can be invalidated any time you like without having to change the password.

like image 30
chaos Avatar answered Nov 03 '22 07:11

chaos