0.5.0 • Published 2 years ago
@dev-jo/authentication-lib v0.5.0
Description
Library with RBAC for Bupa. It means to be used with Azure AD. It is upload to npm as "Library Name". In the project you can try it with the server.
Installation
$ yarn
Running the app to test RBAC
# development
$ yarn start
# watch mode
$ yarn start:dev
# production mode
$ yarn start:prod
Using the Auth modules
// It is needed to import AuthzModule to configure Passport.js to verify access token from azure
// Example using clientId and tenantId from configuration file
AuthzModule.forRoot({
clientId: configuration().azure.clientId,
tenantId: configuration().azure.tenantId,
logLevel: configuration().azure.logLevel,
}, configuration().azure.rolesKey),
// Then we can use AuthGuard to guard a route or controller
import { AuthGuard } from '@nestjs/passport';
@UseGuards(AuthGuard('oauth-bearer'))
// Can use PermissionGuard if you need to protect a route with specific permission set as metadata with decorator @Permissions
@UseGuards(AuthGuard('oauth-bearer'), PermissionsGuard)
@Permissions('write:example-permission')
// Can use RolesGuard if you need to protect a route with specific role set as metadata with decorator @Roles
@UseGuards(AuthGuard('oauth-bearer'), RolesGuard)
@Roles('read:permission')
Test
# unit tests
$ yarn test
# test coverage
$ yarn test:cov
Environment variable
Name | Required | Example |
---|---|---|
PORT | No | 3000 |
AZURE_CLIENT_ID | Yes | uri |
AZURE_TENANT_ID | Yes | uri |
AZURE_ROLES_KEY | Yes | roles |
AZURE_LOG_LEVEL | Yes | 'info' or false |