@hapic/oauth2 🛡️

This client provides an easy way to authenticate and authorize users, clients, robots, ...
using OAuth2 and OpenID Connect standards.
With this API client, developers can easily interact with the server's endpoints,
such as authentication flows, token issuance, and user management.
The client offers a range of abstractions to simplify interactions with the server
and speed up the development process.
Whether you are an experienced developer or new to OAuth2/OpenID,
this API client is a powerful tool to help you implement secure users, clients & robots authentication
and authorization in your applications.
Table of Contents
Documentation
To read the docs, visit https://hapic.tada5hi.net
Installation
npm install @hapic/oauth2 --save
Usage
Authorize
URL
import { OAuth2Client } from '@hapic/oauth2';
const client = new OAuth2Client({
request: {
baseURL: 'http://localhost:3000/',
},
options: {
authorizationEndpoint: 'https://example.com/authorize'
}
});
const authorizeUrl = client.authorize.buildURL({
client_id: 'client',
redirect_uri: 'http://localhost:3000/redirect-callback'
});
console.log(authorizeUrl);
Token
Create
import { OAuth2Client } from '@hapic/oauth2';
const client = new OAuth2Client({
request: {
baseURL: 'http://localhost:3000/'
},
options: {
tokenEndpoint: 'https://example.com/authorize',
clientId: 'client',
clientSecret: 'secret',
}
});
let token = await client.token.createWithRefreshTokenGrant({
refresh_token: 'refresh_token'
});
token = await client.token.createWithClientCredentialsGrant({
client_id: 'foo',
client_secret: 'bar',
});
token = await client.token.createWithPasswordGrant({
username: 'admin',
password: 'start123'
});
token = await client.token.createWithAuthorizationCodeGrant({
state: 'state',
code: 'code'
});
token = await client.token.createWithRobotCredentialsGrant({
id: 'system',
secret: 'start123'
});
console.log(token);
Introspect
import { OAuth2Client } from '@hapic/oauth2';
const client = new OAuth2Client({
request: {
baseURL: 'http://localhost:3000/'
},
options: {
introspectionEndpoint: 'https://example.com/token/introspect'
}
});
let token = await client.token.introspect({
token: 'xxx',
});
token = await client.token.introspect(
{
token: 'xxx',
},
{
authorizationHeader: 'xxx',
}
)
token = await client.token.introspect(
{
token: 'xxx',
},
{
authorizationHeaderInherit: true,
}
)
token = await client.token.introspect(
{
token: 'xxx',
client_id: 'client',
client_secret: 'secret'
}
)
token = await client.token.introspect(
{
token: 'xxx'
},
{
clientId: 'client',
clientSecret: 'secret'
}
);
console.log(token);
UserInfo
import { OAuth2Client } from '@hapic/oauth2';
const client = new OAuth2Client({
request: {
baseURL: 'http://localhost:3000/'
},
options: {
userInfoEndpoint: 'https://example.com/users/@me'
}
});
let userInfo = await client.userInfo.get('Bearer xxx');
userInfo = await client.userInfo.get({
type: 'Basic',
username: 'admin',
password: 'start123'
});
userInfo = await client.userInfo.get();
console.log(userInfo);
License
Made with 💚
Published under MIT License.