Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is function of authorization code and refresh token duplicated in design of oauth2?

there's two ways to fetch access token.

  1. use authorization code to exchange it
  2. use refresh token to refresh it

think about it!!

though the word of exchange and refresh is different,what they do are the same.

both action need to parse client id & client secret(Or signature) and token

we can just save the authorization code in our system,and again use auth code to refresh access token just like refresh token do.

Except that authorization code is expired too soon.

so I wonder why the designers of oauth2 designed these two concepts while not used just one single concept or say just design the authorization code and give it a long expired-time.

like image 669
user1784472 Avatar asked Jul 15 '13 07:07

user1784472


3 Answers

I am afraid that you have not understood the concepts of oauth2 too well. There aren't just two ways of getting the access token, there are more. Each is basically called a 'grant type'. I'm describing the use cases of the ones which I have deployed below :

1- Authorization code : This is similar to the flow of "Login with Facebook" etc buttons which you see on different websites, which allow you to register/login using your facebook etc accounts. Here, on clicking this button, control is directed to Facebook, where the user enters his login credentials. If successful, an authorization code is sent to whatever redirecturl you entered while registering as a developer with Facebook. You then use this authorization code to request the access token service to get the access token which you then use whenever accessing any Facebook webservices to get the user's details.

2- Client credentials : If you are running your own webservices and you want to allow access only to valid clients, then this is the grant type you would use. For example, you are running your webservices and now you want to consume it in your own native mobile app which you distribute through any app store. This will ensure that only those who installed your app will be able to access your webservice.

3- User credentials : Same as above, only in this case this would allow you to authenticate a registered user as well and then give access to user restricted services like my account etc.

4- Refresh token : By design, the access token service gives an access token as well as a refresh token. You would use the refresh token obtained from it here to refresh an expired access token. Essentially, this does not generate a new access token, it only "refreshes" an existing token. It will give you a new access token and refresh token and extend the expiry time. When this access token expires, you again call refresh token using the refresh token obtained last time, and keep repeating the process every time the token expires.

like image 56
s1d Avatar answered Oct 06 '22 14:10

s1d


According to RFC 6749 10.5 The authorization codes are short lived and single-use. Therefore, you cannot use them again and again to get new authorization tokens.

Authorization codes MUST be short lived and single-use. If the authorization server observes multiple attempts to exchange an authorization code for an access token, the authorization server SHOULD attempt to revoke all access tokens already granted based on the compromised authorization code.

like image 3
Brian Agbay Avatar answered Oct 06 '22 14:10

Brian Agbay


There are some additional misconceptions that seem to be presented here, so I wanted to help clear them up.

The differences between an access token and a refresh token can be summarised as follows:

  • An access token is used to provide access to restricted resources to an authorized client after authentication has taken place.
  • A refresh token, on the other hand, is used by a client in order to retrieve new access tokens with identical or narrower scopes.

The different between the Authorization Code Grant and the Implicit Grant (as well as their usages) help to illustrate how both should be used.

In general, the Authorization Code Grant should be preferred over the Implicit Grant unless a resource is being accessed directly via a publicly implemented client (e.g., browser-run code) or there is a specific reason that the Authorization Code Grant cannot be used (e.g., feasibility or performance). This is explained in the RFC definition for the Implicit flow.

During an Implicit Grant, access tokens are exposed to the user-agent which could lead to them being compromised since they are no longer under the control of a server app (confidential client) that could otherwise be requesting the protected resources. This is why refresh tokens are not issued during Implicit Grants. Though access tokens might be exposed, they are short-lived. Resource tokens, on the other hand, are long-lived and can be used to retrieve new access tokens.

The Authorization Code Grant, on the other hand, prevents the potential for refresh tokens to be exposed. During this grant, the authorisation server issues a code instead of tokens. The code is then passed by the user-agent to the client application which exchanges the code with the authorization server to retrieve access and refresh tokens. Since the code exchange is performed directly between the client application and a trusted authorization server, a refresh token can be securely issued.

The RFC spec cautions that the security implications of implementing the Authorization Code Grant in a public client versus a confidential (e.g., server-side) client should be seriously considered. "More OAuth 2.0 Surprises: The Refresh Token" clears up a few misconceptions and furthers the idea that auth codes should not be sent directly by the user-agent to the auth server in order to retrieve refresh tokens, though the OAuth 2.0 spec does not technically dictate this.

like image 2
ComfortableDust Avatar answered Oct 06 '22 12:10

ComfortableDust