Understanding the Web Server OAuth Authentication Flow

The Web server authentication flow is used by applications that are hosted on a secure server. A critical aspect of the Web server flow is that the server must be able to protect the consumer secret.

In this flow, the client application requests the authorization server to redirect the user to another web server or resource that authorizes the user and sends the application an authorization code. The application uses the authorization code to request an access token. The following shows the steps for this flow.

Web server OAuth authentication flow
  1. The application redirects the user to the appropriate Salesforce authorization endpoint, such as https://login.salesforce.com/services/oauth2/authorize. The following parameters are required:
    response_typeMust be code for this authentication flow.
    client_idThe Consumer Key from the connected app definition.
    redirect_uriThe Callback URL from the connected app definition.
    The following parameters are optional:
    displayChanges the login page’s display type. Valid values are:
    • page—Full-page authorization screen. This is the default value if none is specified.
    • popup—Compact dialog optimized for modern Web browser popup windows.
    • touch—Mobile-optimized dialog designed for modern smartphones such as Android and iPhone.
    • mobile—Mobile optimized dialog designed for smartphones such as BlackBerry OS 5 that don’t support touch screens.
    immediateDetermines whether the user should be prompted for login and approval. Values are either true or false. Default is false.
    • If set to true, and if the user is currently logged in and has previously approved the application, the approval step is skipped.
    • If set to true and the user is not logged in or has not previously approved the application, the session is immediately terminated with the immediate_unsuccessful error code.
    stateSpecifies any additional URL-encoded state data to be returned in the callback URL after approval.
    scopeSpecifies what data your application can access. See “Scope Parameter Values” in the online help for more information.
    promptSpecifies how the authorization server prompts the user for reauthentication and reapproval. This parameter is optional. The only values Salesforce supports are:
    • login—The authorization server must prompt the user for reauthentication, forcing the user to log in again.
    • consent—The authorization server must prompt the user for reapproval before returning information to the client.
    It is valid to pass both values, separated by a space, to require the user to both log in and reauthorize. For example:
    An example authorization URL might look something like the following:
  2. The user logs into Salesforce with their credentials. The user is interacting with the authorization endpoint directly, so the application never sees the user’s credentials. After successfully logging in, the user is asked to authorize the application. Note that if the user has already authorized the application, this step is skipped.
  3. Once Salesforce confirms that the client application is authorized, the end-user’s Web browser is redirected to the callback URL specified by the redirect_uri parameter. Salesforce appends authorization information to the redirect URL with the following values:
    codeAuthorization code the consumer must use to obtain the access and refresh tokens.
    stateThe state value that was passed in as part of the initial request, if applicable.
    An example callback URL with authorization information might look something like:
  4. The application extracts the authorization code and passes it in a request to Salesforce for an access token. This request is a POST request sent to the appropriate Salesforce token request endpoint, such as https://login.salesforce.com/services/oauth2/token. The following parameters are required:
    grant_typeValue must be authorization_code for this flow.
    client_idThe Consumer Key from the connected app definition.
    client_secretThe Consumer Secret from the connected app definition.
    redirect_uriThe Callback URL from the connected app definition.
    codeAuthorization code the consumer must use to obtain the access and refresh tokens.
    The following parameter is optional:
    formatExpected return format. The default is json. Values are:
    • urlencoded
    • json
    • xml
    The return format can also be specified in the header of the request using one of the following:
    • Accept: application/x-www-form-urlencoded
    • Accept: application/json
    • Accept: application/xml
    An example access token POST request might look something like:
    POST /services/oauth2/token HTTP/1.1
    Host: login.salesforce.com 
  5. If this request is successful, the server returns a response body that contains the following:
    access_tokenAccess token that acts as a session ID that the application uses for making requests. This token should be protected as though it were user credentials.
    refresh_tokenToken that can be used in the future to obtain new access tokens.
    This value is a secret. You should treat it like the user's password and use appropriate measures to protect it.
    instance_urlIdentifies the Salesforce instance to which API calls should be sent.
    idIdentity URL that can be used to both identify the user as well as query for more information about the user. Can be used in an HTTP request to get more information about the end user.
    issued_atWhen the signature was created, represented as the number of seconds since the Unix epoch (00:00:00 UTC on 1 January 1970).
    signatureBase64-encoded HMAC-SHA256 signature signed with the consumer's private key containing the concatenated ID and issued_at value. The signature can be used to verify that the identity URL wasn’t modified because it was sent by the server.
    An example JSON response body might look something like:
  6. The application uses the provided access token and refresh token to access protected user data.
© Copyright 2000–2014 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.