0.0.29 • Published 2 years ago
@typescript-auth/domains v0.0.29
@typescript-auth/domains 🎉
The main propose of this package, is to provide general classes, interfaces & types for authorization & authentication.
Table of Contents
Installation
npm install @typescript-auth/domains --save
Usage
Ability/Permissions
The AbilityManager
provides an easy way to group permissions for a client session
and allows sharing those permissions between UI, API and microservices.
It easily scales between a claim based and subject/attribute based authorization.
AbilityManager
import {
AbilityManager,
buildAbilityMetaFromName,
PermissionItem
} from "@typescript-auth/domains";
type User = {
id: number;
name: string;
age: number;
}
const permissions: PermissionItem<User>[] = [
{
id: 'user_add',
condition: {
age: {$gt: 20}
}
},
{
id: 'user_drop'
}
];
const manager = new AbilityManager(permissions);
console.log(manager.can('add', 'user', {age: 40}));
// true
console.log(manager.can('add', 'user', {age: 18}))
// false
console.log(manager.can('drop','user'));
// true
AbilityMeta
import {
buildAbilityMetaFromName
} from "@typescript-auth/domains";
const meta = buildAbilityMetaFromName('user_add');
console.log(meta);
// {action: 'add', subject: 'user'}
HTTP Clients
General
import {HTTPClient} from "@typescript-auth/domains";
const client = new HTTPClient({
driver: {
baseURL: 'http://127.0.0.1:3010/'
}
});
(async () => {
const response = await client.role.getMany({
page: {
limit: 10,
offset: 0
}
});
console.log(response);
// {
// meta: {total: 1, limit: 10, offset: 0},
// data: [{id: 'xxx', name: 'admin', description: null}],
// }
})();
OAuth2
URL
import {HTTPOAuth2Client} from "@typescript-auth/domains";
let client = new HTTPOAuth2Client({
token_host: 'https://example.com/',
client_id: 'client'
});
let url = client.buildAuthorizeURL({
redirect_uri: 'https://example.com/redirect'
});
console.log(url);
// https://example.com/oauth/authorize?response_type=code&client_id=client&redirect_uri=https://example.com/redirect
Token
import {HTTPOAuth2Client} from "@typescript-auth/domains";
const client = new HTTPOAuth2Client({
client_id: 'client',
client_secret: 'secret',
token_host: 'https://example.com/',
redirect_uri: 'https://example.com/redirect',
scope: ['email']
});
let token = await client.getTokenWithRefreshToken({refresh_token: 'refresh_token'});
console.log(token);
// {...}
token = await client.getTokenWithClientCredentials();
token = await client.getTokenWithPasswordGrant({username: 'admin', password: 'start123'});
token = await client.getTokenWithAuthorizeGrant({state: 'state', code: 'code'});
0.0.29
2 years ago
0.0.28
2 years ago
0.0.27
2 years ago
0.0.26
2 years ago
0.0.25
2 years ago
0.0.24
2 years ago
0.0.23
2 years ago
0.0.22
2 years ago
0.0.21
2 years ago
0.0.20
2 years ago
0.0.19
2 years ago
0.0.18
2 years ago
0.0.17
2 years ago
0.0.16
2 years ago
0.0.15
2 years ago
0.0.14
2 years ago
0.0.13
2 years ago
0.0.11
2 years ago
0.0.10
2 years ago
0.0.9
2 years ago
0.0.8
2 years ago
0.0.7
2 years ago
0.0.6
2 years ago
0.0.5
2 years ago
0.0.4
2 years ago
0.0.3
2 years ago
0.0.2
2 years ago
0.0.1
2 years ago