본문 바로가기
  • AI (Artificial Intelligence)
Security

The standard authorization code flow - Good Sample Design

by 로샤스 2021. 1. 26.

Ref. developer.xero.com/documentation/oauth2/auth-flow

The standard authorization code flow


The standard authorization code flow is suitable for web server applications that can securely store a client secret. If you’re building a native app (desktop or mobile) then you should refer to the PKCE flow.

To get started, create an OAuth2.0 app and make sure you select the “Auth Code” grant type. Your app is assigned a unique Client ID and you can then generate a Client Secret. The Client Secret is private and should not be shared.


Xero Tenants

Xero is a multi-tenanted platform with different types of tenants. Core Xero APIs (e.g. Accounting, Payroll, Files) operate within the context of an Organisation tenant but a tenant can also be a WorkflowMax account, a Practice Manager account or a XeroHQ practice. A user may have access to multiple tenants and will choose which ones to connect to your app. The type of tenant they can select will be determined by the scopes your app requests in the authorization step.

At the successful completion of an OAuth flow you will be granted an access token to act on behalf of the user. You will then use that access token to find out which tenants the user has connected to your app. From there, you can make API calls by providing both a valid access token and authorized tenant id with each request.


1. Send a user to authorize your app

Your app should direct users to the following url:

https://login.xero.com/identity/connect/authorize?response_type=code&client_id=YOURCLIENTID&redirect_uri=YOURREDIRECTURI&scope=openid profile email accounting.transactions&state=123

The following values should be passed in as parameters:

  • response_type=code
  • client_id issued when you create your app
  • scope permissions to request (see below)
  • redirect_uri the URL on your server to redirect back to
  • state a unique string to be passed back on completion (optional) (see below)

ScopesThe scope parameter is a space-separated list of OAuth scopes, indicating what data you'd like your app to be able to access. You should request the minimum scopes required for whatever the user is doing at the time. The complete list of scopes can be found here.State

The state parameter should be used to avoid forgery attacks. Pass in a value that's unique to the user you're sending through authorisation. It will be passed back after the user completes authorisation.


2. Users are redirected back to you with a code

If the user authorizes your app, Xero will redirect back to your specified redirect_uri with:

  • code a temporary code that may only be exchanged once and expires 5 minutes after issuance.
  • state (if you provided one). If the states don't match, the request may have been created by a third party and you should abort the process.

If any errors occur or the user denies the request, we redirect back to your redirect_uri with an error parameter.


3. Exchange the code

You can now exchange the verification code for an access token. You will also receive an identity token if you’ve requested OpenID Connect scopes and a refresh token if you’ve requested the offline_access scope.

To do this you will need to make a POST request to our token endpoint:

https://identity.xero.com/connect/token

The request will require an authorization header containing your app’s client_id and client_secret

  • Authorization: "Basic " + base64encode(client_id + ":" + client_secret)

The request body will need to contain the grant type (authorization_code), code and redirect_uri

  • grant_type=authorization_code
  • code=The authorization code you received in the callback
  • redirect_uri=The same redirect URI that was used when requesting the code

POST https://identity.xero.com/connect/token authorization: "Basic " + base64encode(client_id + ":" + client_secret) Content-Type: application/x-www-form-urlencoded grant_type=authorization_code &code=xxxxxx &redirect_uri=https://myapp.com/redirect


4. Receive your tokens

The token endpoint will verify all the parameters in the request, ensuring the code hasn’t expired and that the client ID and secret match. If everything checks out, it will generate your tokens and return them in the response.

It will contain the following parameters:

  • access_token The token used to call the API.
  • id_tokenThe token containing user identity details (only returned if OpenID Connect scopes are requested).
  • expires_inThe amount of seconds until the access token expires.
  • token_type: Bearer
  • refresh_tokenThe token used to refresh the access token once it has expired (only returned if the offline_access scope is requested).

The access token

The access token is a JSON Web Token (JWT) which can be decoded to a JSON object containing information about the user and the authentication performed. One particularly useful value is the authentication_event_id which can be used in the next step to find out which tenant/s the user connected in the current auth flow.

{ "nbf": 1589363023, "exp": 1589364823, "iss": "https://identity.xero.com", "aud": "https://identity.xero.com/resources", "client_id": "91E5715B1199038080D6D0296EBC1648", "sub": "a3a4dbafh3495a808ed7a7b964388f53", "auth_time": 1589361892, "xero_userid": "1945393b-6eb7-4143-b083-7ab26cd7690b", "global_session_id": "ac2202575e824af3a181c50fcaa65c3c", "jti": "4e7747cec4ce54d6512b4b0775166c5f", "authentication_event_id": "d0ddcf81-f942-4f4d-b3c7-f98045204db4", "scope": [ "email", "profile", "openid", "accounting.transactions", "accounting.settings", "offline_access" ] }


5. Check the tenants you're authorized to access

Find out which tenants the user has authorized by calling the connections endpoint. If the user has authorized your app previously, they may have existing tenant connections. All of the connected tenants can now be accessed with the most recent access token.

If you want to retrieve the connection/s that were newly authorized in the current auth flow you can filter by the authEventId query parameter (e.g. ...?authEventId=d0ddcf81-f942-4f4d-b3c7-f98045204db4). You do this using the authentication_event_id found on the decoded access token.

Each connection will have a created date and an updated date. If they differ, that means the user has reconnected this tenant to your app (having previosuly connected and disconnected it).

GET https://api.xero.com/connections Authorization: "Bearer " + access_token Content-Type: application/json Response: [ { "id": "e1eede29-f875-4a5d-8470-17f6a29a88b1", "authEventId": "d99ecdfe-391d-43d2-b834-17636ba90e8d", "tenantId": "70784a63-d24b-46a9-a4db-0e70a274b056", "tenantType": "ORGANISATION", "tenantName": "Maple Florist", "createdDateUtc": "2019-07-09T23:40:30.1833130", "updatedDateUtc": "2020-05-15T01:35:13.8491980" }, { "id": "32587c85-a9b3-4306-ac30-b416e8f2c841", "authEventId": "d0ddcf81-f942-4f4d-b3c7-f98045204db4", "tenantId": "e0da6937-de07-4a14-adee-37abfac298ce", "tenantType": "ORGANISATION", "tenantName": "Adam Demo Company (NZ)", "createdDateUtc": "2020-03-23T02:24:22.2328510", "updatedDateUtc": "2020-05-13T09:43:40.7689720" }, { "id": "74305bf3-12e0-45e2-8dc8-e3ec73e3b1f9", "authEventId": "d0ddcf81-f942-4f4d-b3c7-f98045204db4", "tenantId": "c3d5e782-2153-4cda-bdb4-cec791ceb90d", "tenantType": "PRACTICEMANAGER", "tenantName": null, "createdDateUtc": "2020-01-30T01:33:36.2717380", "updatedDateUtc": "2020-02-02T19:21:08.5739590" } ]

Remember! Uncertified apps can only connect to 25 tenants. Once you've got a few customers we recommend applying to become an app partner to have this limit removed.


6. Call the API

Make calls against the Xero APIs by simply adding the following headers to your request:

  • Authorization: "Bearer " + access_token
  • xero-tenant-id: tenantId

GET ../api.xro/2.0/Invoices Authorization: "Bearer " + access_token Accept: application/json Xero-tenant-id: 45e4708e-d862-4111-ab3a-dd8cd03913e1


Refreshing access tokens

Access tokens expire after 30 minutes. Your app can refresh an access token without user interaction by using a refresh token. You get a refresh token by requesting the offline_access scope during the initial user authorization.

To refresh your access token you need to POST to the token endpoint:

https://identity.xero.com/connect/token

The request will require an authorization header containing your app’s client_id and client_secret

  • Authorization: "Basic " + base64encode(client_id + ":" + client_secret)

The request body will need to contain the grant type and refresh token

  • grant_type=refresh_token
  • refresh_token=Your refresh token

POST https://identity.xero.com/connect/token authorization: "Basic " + base64encode(client_id + ":" + client_secret) Content-Type: application/x-www-form-urlencoded grant_type=refresh_token &refresh_token=xxxxxx

Each time you perform a token refresh, you should save the new refresh token returned in the response. If, for whatever reason, your app doesn't receive the response you can retry your existing token for a grace period of 30 minutes.


Removing connections

If you would like to remove an individual tenant connection from your app (e.g. a user wants to disconnect one of their orgs) you can make a DELETE request on the Connections endpoint:

DELETE https://api.xero.com/connections/{connectionId}


Revoking tokens

You can revoke a user's refresh token and remove all their connections to your app by making a request to the revocation endpoint.

To revoke a refresh token you need to POST to the revocation endpoint:

https://identity.xero.com/connect/revocation

The request will require an authorization header containing your app’s client_id and client_secret

  • Authorization: "Basic " + base64encode(client_id + ":" + client_secret)

The request body will contain the refresh token being revoked

  • token=Your refresh token

POST https://identity.xero.com/connect/revocation authorization: "Basic " + base64encode(client_id + ":" + client_secret) Content-Type: application/x-www-form-urlencoded token=xxxxxx

A successful revocation request will return a 200 response with an empty body.

'Security' 카테고리의 다른 글

Authentication and Authorization  (0) 2021.02.03
The standard authorization code flow  (0) 2021.02.03
OpenID Connect Core 1.0  (0) 2021.01.22
OpenID Connect Flows  (0) 2021.01.22
New Architecture of OAuth 2.0 and OpenID Connect Implementation  (0) 2021.01.21

댓글