Skip to main content

@auvious/common / OAuth2Client

Class: OAuth2Client

oauth2 client implementation for auvious' services.

At the moment a login method with the password grant is supported.

More info: https://alexbilbie.com/guide-to-oauth-2-grants

Hierarchy​

Table of contents​

Constructors​

Properties​

Accessors​

Methods​

Constructors​

constructor​

• new OAuth2Client(clientId, clientSecret, serverUrl?, saveTokenOnLocalStorage?, eventHandlers?, useStandardOAuth2?, timers): OAuth2Client

Parameters​

NameTypeDefault value
clientIdstringundefined
clientSecretstringundefined
serverUrlstring"/"
saveTokenOnLocalStoragebooleantrue
eventHandlersOAuth2ClientHandlersnull
useStandardOAuth2booleanfalse
timersTimersundefined

Returns​

OAuth2Client

Overrides​

EventFiringClient<OAuth2ClientHandlers>.constructor

Properties​

GUEST_ROUTE​

• GUEST_ROUTE: string = "/security/guest"


LOGIN_ROUTE​

• LOGIN_ROUTE: string = "/security/authenticate/login"


TOKEN_STORAGE_KEY​

• TOKEN_STORAGE_KEY: string = "auvious.oauth2.client.token"


_authGeneration​

• Private _authGeneration: number = 0

Bumped whenever the stored token changes (saved or deleted) and whenever a refresh starts. A refresh captures it when it starts, so a settlement that arrives after the attempt was superseded can be recognised and ignored.


_clientId​

• Private _clientId: string


_clientSecret​

• Private _clientSecret: string


_eventHandlers​

• Protected _eventHandlers: OAuth2ClientHandlers

Inherited from​

EventFiringClient._eventHandlers


_postman​

• Private _postman: Postman


_realEventHandlers​

• Protected _realEventHandlers: any

Inherited from​

EventFiringClient._realEventHandlers


_refreshInFlight​

• Private _refreshInFlight: Promise<IOAuth2Token> = null

The refresh that is currently in flight, if any.

The server rotates the refresh token on every use, so two parallel refreshes would replay the same, already consumed token and the second one would be rejected with a 400. Every caller that asks for a refresh while one is running joins this promise instead of starting a second one.


_saveTokenOnLocalStorage​

• Private _saveTokenOnLocalStorage: boolean


_token​

• Private _token: IOAuth2Token


_useStandardOAuth2​

• Private _useStandardOAuth2: boolean


log​

• Protected log: Logger

Inherited from​

EventFiringClient.log


preemptiveTokenRefreshTask​

• Private preemptiveTokenRefreshTask: any


refreshTokenProviderFn​

• Private refreshTokenProviderFn: (refreshToken: any) => Promise<IJwtTokenResponse>

Type declaration​

▸ (refreshToken): Promise<IJwtTokenResponse>

Parameters​
NameType
refreshTokenany
Returns​

Promise<IJwtTokenResponse>


timers​

• Private timers: Timers


tokenProviderFn​

• Private tokenProviderFn: () => Promise<IJwtTokenResponse>

Type declaration​

▸ (): Promise<IJwtTokenResponse>

Returns​

Promise<IJwtTokenResponse>


CLIENT_ID​

▪ Static Private CLIENT_ID: string = "auvious"


OAUTH2_TOKEN_ROUTE​

▪ Static OAUTH2_TOKEN_ROUTE: string = "/security/oauth/token"


TOKEN_REFRESH_ROUTE​

▪ Static TOKEN_REFRESH_ROUTE: string = "/security/authenticate/refresh"

Accessors​

_defaultHandlers​

• get _defaultHandlers(): OAuth2ClientHandlers

Returns​

OAuth2ClientHandlers

Overrides​

EventFiringClient._defaultHandlers


clientId​

• get clientId(): string

Gets the oauth2 client id.

Returns​

string


clientSecret​

• get clientSecret(): string

Gets the oauth2 client secret.

Returns​

string


isAuthenticated​

• get isAuthenticated(): boolean

Checks if the token instance exists.

When an authenticated client gets an authentication error (due to expiration) it should issue a refresh_token grant.

However, non authenticated client should issue a password grant to login.

Returns​

boolean


postman​

• get postman(): Postman

Returns​

Postman


token​

• get token(): IOAuth2Token

Gets the current token of the authenticated user. Can be undefined.

Returns​

IOAuth2Token

Methods​

_cleanEventHandlers​

▸ _cleanEventHandlers(): void

Clean event handler callbacks.

We actually just reassign to the default callbacks.

Returns​

void

Inherited from​

EventFiringClient._cleanEventHandlers


_setEventHandlers​

▸ _setEventHandlers(handlers): void

Sets event handler callbacks.

Every event handler callback defaults to _defaultHandlers which just prints the event to console.

An event callback that was not passed in the argument, will default to either:

  • the previous callback is ever set.
  • the _defaultHandlers's corresponding callback.

The default behaviour when appendToPrevious = true is to append the handlers passed in the arguments.

If appendToPrevious is set to false the previous handlers will be cleaned before we the the argument handlers.

Parameters​

NameType
handlersOAuth2ClientHandlers

Returns​

void

Inherited from​

EventFiringClient._setEventHandlers


attachOAuth2Handler​

▸ attachOAuth2Handler(handlers): void

Exposes the internal auth event handlers.

Parameters​

NameTypeDescription
handlersOAuth2ClientHandlers: OAuth2ClientHandlers

Returns​

void


auviousOAuth2RefreshToken​

▸ auviousOAuth2RefreshToken(generation): Promise<IOAuth2Token>

Parameters​

NameType
generationnumber

Returns​

Promise<IOAuth2Token>


clearPreemptiveTokenRefresh​

▸ clearPreemptiveTokenRefresh(): void

Cancel any scheduled preemptive refresh.

The id belongs to this.timers, a worker-timers broker, so it means nothing to the global clearTimeout that logout() used to call - the timer was never actually being cancelled.

Returns​

void


deleteToken​

▸ deleteToken(): void

Removes the token json from local storage.

Returns​

void


guestLogin​

▸ guestLogin(username, firstName?, lastName?): Promise<IGuestUser>

Simpler (used by Epi) guest login.

Parameters​

NameType
usernamestring
firstName?string
lastName?string

Returns​

Promise<IGuestUser>


isCurrentAuthGeneration​

▸ isCurrentAuthGeneration(generation): boolean

Tells whether a refresh attempt still owns the session.

An attempt loses ownership when another refresh starts, when a login stores a token or when the client logs out. Such an attempt must not store its own result and, above all, must not delete a token that was saved after it was issued.

Parameters​

NameType
generationnumber

Returns​

boolean


loadToken​

▸ loadToken(): void

Loads the token json from local storage. Returns undefined if no exists.

Returns​

void


login​

▸ login(clientId, username, password, organization): Promise<IOAuth2Token>

Login method w/o password grant.

Parameters​

NameTypeDescription
clientIdstringthe subdomain of the request.
usernamestringuser username to login.
passwordstringuser password to validate.
organizationstringthe organization to sign in.

Returns​

Promise<IOAuth2Token>


loginGuest​

▸ loginGuest(clientId, email, organization, firstName, lastName?): Promise<IGuestUser>

Login method for guest.

Parameters​

NameTypeDescription
clientIdstringthe subdomain of the request.
emailstring-
organizationstring-
firstNamestring-
lastName?string-

Returns​

Promise<IGuestUser>


loginTemplate​

▸ loginTemplate(tokenProviderFn, refreshTokenProviderFn?): Promise<IOAuth2Token>

Parameters​

NameType
tokenProviderFn() => Promise<IJwtTokenResponse>
refreshTokenProviderFn?(refreshToken: string) => Promise<IJwtTokenResponse>

Returns​

Promise<IOAuth2Token>


logout​

▸ logout(): Promise<void>

Logout the client.

Cleans the oauth2 token from the client.

Returns​

Promise<void>


refreshToken​

▸ refreshToken(tokenToRefresh?): Promise<IOAuth2Token>

Refresh method with refresh_token grant.

Parameters​

NameTypeDefault value
tokenToRefreshIOAuth2Tokennull

Returns​

Promise<IOAuth2Token>


refreshTokenExpired​

▸ refreshTokenExpired(): void

Fires the corresponding event of the expired token.

Returns​

void


refreshTokenRequested​

▸ refreshTokenRequested(): Promise<IOAuth2Token>

Handles the refresh of the expired token.

Returns​

Promise<IOAuth2Token>


refreshTokenUsingProviderFn​

▸ refreshTokenUsingProviderFn(generation): Promise<IOAuth2Token>

Parameters​

NameType
generationnumber

Returns​

Promise<IOAuth2Token>


saveToken​

▸ saveToken(token): void

Saves the token json to local storage. If not authenticated, it ensures the token json is deleted from local storage.

Parameters​

NameType
tokenIOAuth2Token

Returns​

void


setupPreemptiveTokenRefresh​

▸ setupPreemptiveTokenRefresh(): void

Returns​

void


standardOAuth2Login​

▸ standardOAuth2Login(username, password): Promise<IOAuth2Token>

Standard OAuth2 Login.

Parameters​

NameTypeDescription
usernamestringusername
passwordstringpassword

Returns​

Promise<IOAuth2Token>


standardOAuth2RefreshToken​

▸ standardOAuth2RefreshToken(generation): Promise<IOAuth2Token>

Parameters​

NameType
generationnumber

Returns​

Promise<IOAuth2Token>


supersededError​

▸ supersededError(): AuviousError

Returns​

AuviousError