TypeScript OAuth 2.0 Client
An exstensible OAuth 2.0, standard compliant client library for Node.js and the Web.
Also supports the Bearer Token Usage and Token Introspection standards.
Basic usage
Start by creating and configuring your OAuth 2.0 client.
import * as OAuth2 from 'oauth2-client-ts';
const client = new OAuth2.Client({
credentials: new OAuth2.ClientCredentials('myClientId', 'myClientSecret'),
tokenEndpoint: 'https://www.example.com/auth/token',
authorizationEndpoint: 'https://www.example.com/auth/authorize',
});
You can then use one of the flows described in the OAuth 2.0 standard.
Resource Owner Password Credentials
const flow = client.startResourceOwnerPasswordCredentialsFlow();
const token = await flow.getToken(new OAuth2.ResourceOwnerPasswordCredentialsGrant('myUsername', 'myPassword'), 'scope.read scope.write');
Client Credentials
const flow = client.startClientCredentialsFlow();
const token = await flow.getToken('scope.read scope.write');
Refresh Token
const flow = client.startRefreshTokenFlow();
const token = await flow.getToken(new OAuth2.RefreshTokenGrant('ey.myRefresh.token'), 'scope.read scope.write');
Bearer Token Usage
Import the Bearer Token Usage extension.
import 'oauth2-client-ts/dist/extensions/bearer_token_usage';
You can then use the convenience functions on the TokenCredentials
type.
token.getBodyParameters();
token.getQueryParameters();
token.getRequestHeaders();
...
const token = OAuth2.TokenCredentials.fromAuthorizationHeader('Bearer ey.received.token');
Token Introspection
Import the Token Introspection extension.
import 'oauth2-client-ts/dist/extensions/token_introspection';
When creating your OAuth 2.0 client, you can now specify the token introspection endpoint of the OAuth server.
const client = new OAuth2.Client({
...
introspectionEndpoint: 'https://www.example.com/auth/introspect',
});
Finally, introspect access or refresh tokens using your client directly.
const result = await client.introspect(
new OAuth2.TokenCredentials('my.authorization.token', 'Bearer'),
'token.to.introspect'
);