2.0.0 • Published 2 years ago

@evokegroup/oauth2 v2.0.0

Weekly downloads
-
License
ISC
Repository
bitbucket
Last release
2 years ago

@evokegroup/oauth2

Library for helping with OAuth 2.0 authorizations

Class: OAuth2

Class: OAuth2.AccessToken

constructor({ authResponse, value = null, expires = 0 })

ParameterTypeDefaultDescription
authResponseobject,stringThe authorization response
valuestringnullAn existing access token value
expiresnumber0The expiration date for an existing token

get value => string

Returns the access token value

get expires => number

Returns the expiration date

get created => number

Returns the date the AccessToken was created

get(parameter = null) => object | string

Get a parameter that was returned with the authorization response or all parameters if null

isExpired() => boolean

toJSON() => object

For JSON.stringify

toObject() => object

static parse(obj) => OAuth2.AccessToken

Parses an AccessToken

Class: OAuth2.Authorizer

Abstract base class for authorization implementations

abstract getAccessToken() => OAuth2.AccessToken

abstract setAccessToken(accessToken)

authorize() => Promise<OAuth2.AccessToken>

Determines if a valid AccessToken exists via getAccessToken and is valid via AccessToken.isExpired and if so resolved the Promise passing in the token. Otherwise authenticate is called.

abstract authenticate() => Promise<OAuth2.AccessToken>

Class: OAuth2.WebTokenAuthorizer extends OAuth2.Authorizer

An OAuth2.Authorizer implementation which makes a web request to retrive the token

constructor(args)

ParameterTypeDefaultDescription
urlstringThe authentication server URL
clientIdstringnullThe client_id parameter
clientSecretstringnullThe client_secret parameter
scopestringnullThe scope parameter
grantTypestringclient_credentialsThe grant_type parameter. If for some reason this should not be part of the payload, set it to null.
parametersobject{}Additional payload parameters. Overrides other data.
contentTypestringapplication/jsonThe Content-Type request header.
headersobject{}Additional request headers. Override other data.
timeoutnumber30000The request timeout in milliseconds
cachebooleantrueIf true, the OAuth2.AccessToken created during authentication will be cached and reused while valid
const OAuth2 = require('@evokegroup/oauth2');
const webTokenAuthorizer = new OAuth2.WebTokenAuthorizer({
  url: 'https://api.domain.com/auth/token',
  clientId: 'abc',
  clientSecret: '123'
});
webTokenAuthorizer.authorize()
  .then((accessToken) => {
    // do something with the access token
  })