Dorinian Military Corps
  1971 active members
  176 are Online

Year

27

Day

307

Time

18:13:38

Guest
Login

Web Services Hub » OAuth 2.0 » Flows » Using OAuth 2.0 for Installed Applications

Using OAuth 2.0 for Installed Applications

The Web Service OAuth 2.0 endpoint supports applications that are installed on a device (e.g. Mobile, Mac, PC). These applications are distributed to individual machines, and it is assumed that these applications cannot keep secrets. These applications may access a Resourece while the user is present at the application or when the application is running in the background for long periods of time without direct interaction with the user. This flow requires that the application has access to the system browser or the ability to embed a browser control in the application.

Overview

The sequence is similar to the one shown in the Using OAuth 2.0 for Web Server Applications, but there are three exceptions:

  1. When registering the application, you specify that the application is a Installed application. This results in a different value for the redirect_uri parameter.
  2. The client_id and client_secret obtained during registration are embedded in the source code of your application. In this context, the client_secret is obviously not treated as a secret.
  3. The authorisation code is returned to your application in the title bar of the browser and body of the web page.

This sequence starts by redirecting a browser (system browser or embedded in the application as a web view) to a SW Combine URL with a set of query parameters that indicate the type of Resource access the application requires. Like other scenarios, SW Combine handles the user authentication and consent, but the result of the sequence is an authorisation code. The authorization code is returned in the title bar of the browser and body of web page.

After receiving the authorisation code, the application can exchange the code for an access token and a refresh token. The application presents its client_id and client_secret (obtained during application registration) and the authorisation code during this exchange. Upon receipt of the refresh token, the application should store it for future use. The access token gives your application access to a Resource.

Forming the URL

The URL used when authenticating a user is https://www2.swcombine.com/ws/oauth2/auth/. This endpoint is accessible over SSL, and HTTP connections are refused.

Endpoint Description
https://www2.swcombine.com/ws/oauth2/auth/ This endpoint is the target of the initial request for an access token. It handles active session lookup, authenticating the user, and user consent. The result of requests of this endpoint include access tokens, refresh tokens, and authorisation codes.

The set of query string parameters supported by the Web Service Authorisation Server for web server applications are:

Parameter Values Description
response_type code Determines if the OAuth 2.0 endpoint returns an authorisation code. For web server applications, a value of code should be used.
client_id the client_id obtained from the Web Service console Indicates the client that is making the request. The value passed in this parameter must exactly match the value shown in the Web Service console.
redirect_uri The redirect_uri values registered at the Application Registration Form Determines where the response is sent. For installed applications this must be either urn:ietf:wg:oauth:2.0:oob or an http://localhost address (on any port you choose). See Choosing a Redirect URI for more details.
scope space delimited set of permissions the application requests Required. Indicates the resource access your application is requesting, as a space-delimited list of permission names (for example character_read character_credits, URL-encoded with %20 or +). See the permissions documentation for the available permissions (listed there in upper case; use lower case in the scope parameter). The values passed in this parameter inform the consent page shown to the user. There is an inverse relationship between the number of permissions requested and the likelihood of obtaining user consent.
state any string Indicates any state which may be useful to your application upon receipt of the response. The Web Service Authorisation Server roundtrips this parameter, so your application receives the same value it sent.
access_type online or offline Indicates if your application needs to access a Resource when the user is not present. Defaults to online. Include offline so the consent page tells the user that offline access is being requested, and include it again when exchanging the authorisation code: a refresh token is only issued when the token request contains access_type=offline.
renew_previously_granted Optional string with value: 'yes' Indicates whether previously granted permissions should be renewed if not explicitly included within the scope parameter. Defaults to not renewing previously granted permissions.
code_challenge Optional, 43-128 characters Enables PKCE, which is strongly recommended for installed applications since they cannot keep the client_secret confidential. This should be a high-entropy random string generated by your application (the "code_verifier"), transformed as described by code_challenge_method. See Using PKCE below.
code_challenge_method Optional: S256 or plain The transformation applied to the code_verifier to produce code_challenge. S256 is strongly recommended over plain. Defaults to plain if code_challenge is supplied without this parameter.

An example URL is shown below.

https://www2.swcombine.com/ws/oauth2/auth/?
scope=character_read%20character_credits&
redirect_uri=urn:ietf:wg:oauth:2.0:oob&
response_type=code&
client_id={client_id}
		

If the user logs in and grants access via a URL similar to the one shown above, the result will be a dialog similar to the following:

Another example URL is shown below.

https://www2.swcombine.com/ws/oauth2/auth/?
scope=character_read%20character_credits&
redirect_uri=http%3A%2F%2Flocalhost:1099&
response_type=code&
client_id={client_id}
		

The difference between these two URLs is the redirect_uri parameter. The first one results in an authorisation code in the title of the page, and the second one results in the authorisation code sent to a http://localhost address as part of the query string.

Choosing a Redirect URI

When you register your installed application in the Web Service Console, urn:ietf:wg:oauth:2.0:oob is registered as its redirect_uri, and an http://localhost address (on any port) is also accepted. The value your application uses determines how the authorisation code is returned to your application, as described below.

http://localhost

This value signals to the Web Service Authorisation Server that the authorisation code should be returned as a query string parameter to the web server on the client. You may specify any port number without changing the Web Service Console configuration. To receive the authorisation code on this URL, your application must be listening on the local web server. This is possible on many, but not all platforms. In some cases, it is possible, but other software (e.g. Windows firewall) prevents delivery of the message without significant client configuration. If your platform supports it, this is the recommended mechanism for obtaining the authorisation code.

urn:ietf:wg:oauth:2.0:oob

This value signals to the Web Service Authorisation Server that the authorisation code should be returned in the title bar of the browser. This is useful when the client cannot listen on an HTTP port without significant client configuration. Windows applications possess this characteristic.

When this value is used, your application can sense that the page has loaded and the title of the HTML page contains the authorisation code. It is then up to your application to close the browser window if you want to ensure that the user never sees the page that contains the authorisation code. The mechanism for doing this varies from platform to platform.

Handling the Response

After the application receives the authorisation code, it may exchange the authorisation code for an access token and a refresh token. This request is an HTTP post, and includes the following parameters:

Field Description
code The authorisation code returned from the initial request. Authorisation codes expire after 5 minutes
client_id The client_id obtained during application registration
client_secret The client secret obtained during application registration
redirect_uri The same redirect_uri that was used in the authorisation request
grant_type As defined in the OAuth 2.0 specification, this field must contain a value of "authorization_code"
access_type Optional. Set to offline to receive a refresh token along with the access token. Without it (or with online) only an access token is issued
code_verifier Required if code_challenge was included in the authorisation request. See Using PKCE below.

The actual request might look like:

POST /ws/oauth2/token/ HTTP/1.1
Host: www2.swcombine.com
Content-Type: application/x-www-form-urlencoded code=8F3K2JD9X0QW7LZP5N1VB6MC4TY8RHGE& client_id={client_id}& client_secret={client_secret}& redirect_uri=urn:ietf:wg:oauth:2.0:oob& grant_type=authorization_code& access_type=offline& code_verifier={code_verifier}

Using PKCE

PKCE (Proof Key for Code Exchange, RFC 7636) protects the authorisation code flow for applications, such as installed and mobile apps, that cannot reliably keep a client_secret confidential. It is optional, but strongly recommended for these applications.

To use PKCE:

  1. Before redirecting to the authorisation URL, generate a high-entropy random string of 43-128 characters (letters, digits, and the characters -, ., _, ~) called the code_verifier, and keep it in your application.
  2. Compute the code_challenge from the code_verifier. If using code_challenge_method=S256 (recommended), the code_challenge is the SHA-256 hash of the code_verifier, base64url-encoded without padding. If using code_challenge_method=plain, the code_challenge is simply the code_verifier itself.
  3. Include code_challenge and code_challenge_method as query string parameters on the authorisation request, as shown in the table above.
  4. When exchanging the authorisation code for an access token, include the original code_verifier as a parameter. The Web Service Authorisation Server recomputes the challenge from the supplied code_verifier and compares it to the code_challenge stored with the authorisation code, rejecting the exchange with an invalid_grant error if they do not match.

A successful response to this request contains the following fields:

Field Description
access_token The token that can be sent to a Resource
refresh_token A token that may be used to obtain a new access token. Refresh tokens are valid until the user revokes access, or until the token is used to obtain a new access token (see Using a Refresh Token below). This field is only present if access_type=offline is included in the authorisation code exchange request.
expires_in The lifetime of the access token, in seconds (currently one hour)
scope The space delimited set of permissions the access token grants
token_type The kind of token being returned. Always Bearer

Other fields may be included in the response. Your application should allow additional fields to be returned in the response. The set shown above is the minimum set.

A successful response is returned as a JSON array, similar to the following:

JSON

{
	"access_token":"Q7ZP2M9XK4D8VB1N6CJ3HT5RW0LY8FGE",
	"expires_in":3600,
	"scope":"character_read character_credits",
	"token_type":"Bearer",
  	"refresh_token":"R4TN8W2KJ7XM1QZ5HB9CV3DP6LY0GFEA"
}
		

XML

<?xml version="1.0" encoding="utf-8"?>
<OAuth>
    <access_token>Q7ZP2M9XK4D8VB1N6CJ3HT5RW0LY8FGE</access_token>
    <expires_in>3600</expires_in>
    <scope>character_read character_credits</scope>
    <token_type>Bearer</token_type>
    <refresh_token>R4TN8W2KJ7XM1QZ5HB9CV3DP6LY0GFEA</refresh_token>
</OAuth>
		

Calling a Resource

After your application has obtained an access token, your application can access a Reource by including it in either an access_token query parameter or an Authorization: OAuth HTTP header.

For example, a call to the Character Resource using the access_token query string parameter looks like the following:

GET https://www2.swcombine.com/ws/v2.0/character/Testing%20Character?access_token=M2XK9D4PQ7VB1N6CJ3HT5RW0LY8FGEZA

A call to the same resource using the access_token Authorization: OAuth HTTP header looks like the following:

GET /ws/v2.0/character/Testing%20Character HTTP/1.1
Authorization: OAuth M2XK9D4PQ7VB1N6CJ3HT5RW0LY8FGEZA
Host: www2.swcombine.com

You can try either out in the CURL command line application. Here's an example of the query string parameter option:

curl https://www2.swcombine.com/ws/v2.0/character/Testing%20Character?access_token=M2XK9D4PQ7VB1N6CJ3HT5RW0LY8FGEZA

And the HTTP header option:

curl -H "Authorization: OAuth M2XK9D4PQ7VB1N6CJ3HT5RW0LY8FGEZA" https://www2.swcombine.com/ws/v2.0/character/Testing%20Character

Using a Refresh Token

To obtain a new access token is simple. To obtain a new access token, make an HTTPs POST to https://www2.swcombine.com/ws/oauth2/token/. The request must include the following parameters:

Field Description
refresh_token The refresh token returned from the authorisation code exchange
client_id The client_id obtained during application registration
client_secret The client secret obtained during application registration
grant_type As defined in the OAuth 2.0 specification, this field must contain a value of refresh_token

Such a request will look similar to the following:

POST /ws/oauth2/token/ HTTP/1.1
Host: www2.swcombine.com
Content-Type: application/x-www-form-urlencoded client_id={client_id}& client_secret={client_secret}& refresh_token=R4TN8W2KJ7XM1QZ5HB9CV3DP6LY0GFEA& grant_type=refresh_token

As long as the user has not revoked the access granted to your application, the response includes a new access token and a new refresh token. A response from such a request is shown below:

JSON

{
  "access_token":"M2XK9D4PQ7VB1N6CJ3HT5RW0LY8FGEZA",
  "expires_in":3600,
  "scope":"character_read character_credits",
  "token_type":"Bearer",
  "refresh_token":"K9D2M7XQ4PVB1N6CJ3HT8RW0LY5FGEZA"
}
		

XML

<?xml version="1.0" encoding="utf-8"?>
<OAuth>
    <access_token>M2XK9D4PQ7VB1N6CJ3HT5RW0LY8FGEZA</access_token>
    <expires_in>3600</expires_in>
    <scope>character_read character_credits</scope>
    <token_type>Bearer</token_type>
    <refresh_token>K9D2M7XQ4PVB1N6CJ3HT8RW0LY5FGEZA</refresh_token>
</OAuth>
		

Important

Refresh tokens are single use. When a refresh token is used, it is invalidated and replaced by the new refresh_token in the response, so your application must store the new refresh token every time it refreshes. Presenting an old refresh token again fails with an invalid_grant error.