oAuth 2.0 - Acting on behalf of the user

2020-04-19 08:03发布

问题:

I'm new to oAUth2 and I'm trying to get a few things straight.

I understand the basic principles involved with oAuth2 but I am not sure how to implement it in my situation.

I am writing an app that acts on behalf of the user to automate a manual process and perform some tasks(update/request status...etc). The API we are connecting to uses oAuth2 to grant our application permission. We plan on having the user grant our application permission when they create a new account with us.

I understand that the user will request an authentication code that is provided to our application. Then our application will use the authentication code to generate an access token.

We would like to do this only once. Then act as the user to send and receive notifications without having to have the user to log into the service with their credentials.

I am not sure how to implement this without having to store the user credentials to get an auth code since the auth code and auth tokens expire. I'm guessing that this is a common scenario.

What would I need to do to get what I want accomplished?

回答1:

You can get a new AccessToken using a RefreshToken, if this is provided by the Authorization Server.

If it's not provided I would contact the Api provider, you should never store a users credentials. In fact if the OAuth protocol is well implemented as a client you should never be able to even get the client credentials. When the user has to login you should redirect the user to the Authorization Server, there the user should login and then the authorization token should be redirected to your application by the Authorization Server.

See also this explanation on Refresh Tokens from the OAuth 2.0 spec:

Refresh tokens are credentials used to obtain access tokens. Refresh tokens are issued to the client by the authorization server and are used to obtain a new access token when the current access token becomes invalid or expires, or to obtain additional access tokens with identical or narrower scope (access tokens may have a shorter lifetime and fewer permissions than authorized by the resource owner). Issuing a refresh token is optional at the discretion of the authorization server. If the authorization server issues a refresh token, it is included when issuing an access token

Note

If you request a new AccessToken using your RefreshToken and the response includes a new RefreshToken you should overwrite your currently saved RefreshToken. With other words, you should always use the latest RefresthToken you received.