0

I am trying to implement OAuth with my javascript client application. I already have the server up and running.

My workflow is as following:

  • Open app
  • Check if token is present
  • Validate the token
  • If not present or not valid go to oauth server for the token
  • Get back to app and repeat 2 and 3
  • I everything is ok show my app

I am not sure how to implement point 2. I understand that I need to make another call to the server for this but where is the validation endpoint? I only have /authorize, /user and /logout

Also how should the request be formed? Do I attach token as a GET parameter and thats all? What if somebody intercepts valid the token?

  • What a first question. Oauth is a big pain. Check [Oauth bible](http://oauthbible.com/) and [Oauth for dummies](https://marktrapp.com/blog/2009/09/17/oauth-dummies/) as they've helped me. – evolutionxbox Aug 11 '15 at 11:19

1 Answers1

3

Depends on your application, but since you have a javascript web application, that most probably can't keep it's credentials secret, you'll need to implement the Implicit Grant as stated in the OAuth 2.0 spec. If however your application CAN keep it's credentials secret, you should implement the Client Credentials Grant (server side application) because it's more secure.

I am not sure how to implement point 2

You should store your access token somewhere in your web application, for instance in localStorage.

The first time a user will access your web application, the user will have NO access token and NO session with your authorization server. Your web application could see if you have an access token by doing something like:

if (!localStorage.getItem('accessToken') {
  window.location.replace('https://your-authorization-server/authorize?response_type=token&client_id=<CLIENT_ID>&redirect_uri=<CALLBACK_URL>&scope=<SCOPE>');
}

If there's no access token, you'll need to redirect to your authorization server so that the user can log in with the authorization server. After the user has logged in, the authorization server will redirect the user back to your web application (after the user has granted permission to whatever resource your web application likes to access on behalf of said user) with a valid access token. This access token must be exposed as a hash fragment named access_token, i.e:

https://web-app.com#access_token=123456789X

Now your web application can extract the access token and store it somewhere.

I understand that I need to make another call to the server for this but where is the validation endpoint? I only have /authorize, /user and /logout

Your authorization server requires a token validation endpoint. This endpoint will be used by your web application to validate said token (the one you stored somewhere, for instance localStorage). When this endpoint is called with an access token and it turns out the token is valid, the user can continue, otherwise the user will be redirected to the authorization server. If the user already has a session with the authorization server, the user will be redirected back immediately with a new access token, otherwise the user needs to authenticate (login) first.

Also how should the request be formed? Do I attach token as a GET parameter and thats all?

Some send a GET request with the access token as a url query parameter, others send a POST request with the access token as the payload of the request body.

What if somebody intercepts the valid token?

Always use https and your access token should be valid for a limited amount of time.

Something to keep in mind is that because your application can't keep it's credentials secret, you need to Implement the Implicit Grant, this means you immediately receive the access token when the authorization server authorized the web application based on Client Id and domain. As opposed to Client Credentials Grant where you first receive an Authorization Code that needs to be exchanged for an access token. When using Implicit Grant you can NOT use Refresh Tokens. This means the user NEEDS to go through the entire flow with the authorization server to obtain a new access token. But this isn't really a big deal, because the user will already be logged in with the authorization server, resulting in an immediate redirect, so when implemented correctly in the web application, the user won't notice.

This is just covering it in broad strokes, it really helped me (and I advise you) to read the OAuth 2.0 spec. Hope this helps you out a bit, OAuth is a complex subject!

Community
  • 1
  • 1
danillouz
  • 6,121
  • 3
  • 17
  • 27
  • Thanks, i've it turns out that indeed I had a valkidation endpoint. It was not accessible by default thought: For those who have similar problems I found answer here: http://stackoverflow.com/questions/26750999/spring-security-oauth2-check-token-endpoint –  Aug 11 '15 at 12:26