import OAuth2PasswordGrantAuthenticator from 'ember-simple-auth/authenticators/oauth2-password-grant';

Authenticator that conforms to OAuth 2 (RFC 6749), specifically the "Resource Owner Password Credentials Grant Type".

This authenticator also automatically refreshes access tokens (see RFC 6749, section 6) if the server supports it.

Methods

source public

authenticate(identification, password, scope, headers) Ember.RSVP.Promise

Overrides: authenticate of BaseAuthenticator.

Authenticates the session with the specified identification, password and optional scope; issues a POST request to the serverTokenEndpoint and receives the access token in response (see http://tools.ietf.org/html/rfc6749#section-4.3).

If the credentials are valid (and the optionally requested scope is granted) and thus authentication succeeds, a promise that resolves with the server's response is returned, otherwise a promise that rejects with the error as returned by the server is returned.

If the server supports it, this method also schedules refresh requests for the access token before it expires.

The server responses are expected to look as defined in the spec (see http://tools.ietf.org/html/rfc6749#section-5). The response to a successful authentication request should be:

HTTP/1.1 200 OK
                          Content-Type: application/json;charset=UTF-8
                          
                          {
                            "access_token":"2YotnFZFEjr1zCsicMWpAA",
                            "token_type":"bearer",
                            "expires_in":3600, // optional
                            "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA" // optional
                          }
                          

The response for a failing authentication request should be:

HTTP/1.1 400 Bad Request
                          Content-Type: application/json;charset=UTF-8
                          
                          {
                            "error":"invalid_grant"
                          }
                          

A full list of error codes can be found here.

Arguments

identification: String

The resource owner username

password: String

The resource owner password

scope: String | Array

The scope of the access request (see RFC 6749, section 3.3)

headers: Object

Optional headers that particular backends may require (for example sending 2FA challenge responses)

Returns

Ember.RSVP.Promise

A promise that when it resolves results in the session becoming authenticated

source public

invalidate(data) Ember.RSVP.Promise

Overrides: invalidate of BaseAuthenticator.

If token revocation is enabled, this will revoke the access token (and the refresh token if present). If token revocation succeeds, this method returns a resolving promise, otherwise it will return a rejecting promise, thus intercepting session invalidation.

If token revocation is not enabled this method simply returns a resolving promise.

Arguments

data: Object

The current authenticated session data

Returns

Ember.RSVP.Promise

A promise that when it resolves results in the session being invalidated

source protected

makeRequest(url, data, headers) Promise

Makes a request to the OAuth 2.0 server.

Arguments

url: String

The request URL

data: Object

The request data

headers: Object

Additional headers to send in request

Returns

Promise

A promise that resolves with the response object

source public

restore(data) Ember.RSVP.Promise

Overrides: restore of BaseAuthenticator.

Restores the session from a session data object; will return a resolving promise when there is a non-empty access_token in the session data and a rejecting promise otherwise.

If the server issues expiring access tokens and there is an expired access token in the session data along with a refresh token, the authenticator will try to refresh the access token and return a promise that resolves with the new access token if the refresh was successful. If there is no refresh token or the token refresh is not successful, a rejecting promise will be returned.

Arguments

data: Object

The data to restore the session from

Returns

Ember.RSVP.Promise

A promise that when it resolves results in the session becoming or remaining authenticated

Properties

source public

clientId: String

Default: null

The client_id to be sent to the authentication server (see https://tools.ietf.org/html/rfc6749#appendix-A.1). This should only be used for statistics or logging etc. as it cannot actually be trusted since it could have been manipulated on the client!

source public

refreshAccessTokens: Boolean

Default: true

Sets whether the authenticator automatically refreshes access tokens if the server supports it.

source public

rejectWithResponse: Boolean

Default: false

When authentication fails, the rejection callback is provided with the whole Fetch API Response object instead of its responseJSON or responseText.

This is useful for cases when the backend provides additional context not available in the response body.

source public

rejectWithXhr: Boolean

Deprecated:Use rejectWithResponse instead

Default: false

When authentication fails, the rejection callback is provided with the whole Fetch API Response object instead of its responseJSON or responseText.

This is useful for cases when the backend provides additional context not available in the response body.

source public

sendClientIdAsQueryParam: Boolean

Default: false

The OAuth2 standard is to send the client_id as a query parameter. This is a feature flag that turns on the correct behavior for OAuth2 requests.

source public

serverTokenEndpoint: String

Default: '/token'

The endpoint on the server that authentication and token refresh requests are sent to.

source public

serverTokenRevocationEndpoint: String

Default: null

The endpoint on the server that token revocation requests are sent to. Only set this if the server actually supports token revocation. If this is null, the authenticator will not revoke tokens on session invalidation.

If token revocation is enabled but fails, session invalidation will be intercepted and the session will remain authenticated (see invalidate).

source public

tokenRefreshOffset: Integer

Default: a random number between 5 and 10

The offset time in milliseconds to refresh the access token. This must return a random number. This randomization is needed because in case of multiple tabs, we need to prevent the tabs from sending refresh token request at the same exact moment.

When overriding this property, make sure to mark the overridden property as volatile so it will actually have a different value each time it is accessed.

Events

source

sessionDataInvalidated

Inherited from: sessionDataInvalidated of BaseAuthenticator.

Triggered when the authentication data is invalidated by the authenticator due to an external or scheduled event. This might happen, e.g., if a token expires or an event is triggered from an external authentication provider that the authenticator uses. The session handles the event and will invalidate itself when it is triggered.

source

sessionDataUpdated

Overrides: sessionDataUpdated of BaseAuthenticator.

Triggered when the authenticator refreshed the access token (see RFC 6749, section 6).

Arguments

data: Object

The updated session data