@koakh/nestjs-package-jwt-authentication-ldap
Advanced tools
Comparing version 1.0.37 to 1.0.38
import { JwtService } from '@nestjs/jwt'; | ||
import { ConsumerAppService, ModuleOptionsConfig } from '../common/interfaces'; | ||
import { ConsumerAppService, JwtSecrets, ModuleOptionsConfig } from '../common/interfaces'; | ||
import { AuthService } from './auth.service'; | ||
@@ -26,2 +26,5 @@ import { LoginDto, LoginResponseDto, RevokeRefreshTokenResponseDto } from './dto'; | ||
}>; | ||
getJwtSecrets(): Promise<{ | ||
message: JwtSecrets; | ||
}>; | ||
} |
@@ -146,2 +146,7 @@ "use strict"; | ||
} | ||
getJwtSecrets() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return { message: this.consumerAppService.getJwtSecrets() }; | ||
}); | ||
} | ||
}; | ||
@@ -182,5 +187,5 @@ __decorate([ | ||
__decorate([ | ||
(0, common_1.Post)('invalidate-secrets'), | ||
(0, common_1.Post)('/invalidate-secrets'), | ||
(0, roles_decorator_1.Roles)(process.env.AUTH_ADMIN_ROLE || enums_1.UserRoles.ROLE_ADMIN), | ||
(0, common_1.UseGuards)(guards_1.RolesAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.PermissionsAuthAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.JwtAuthGuard), | ||
@@ -191,2 +196,10 @@ __metadata("design:type", Function), | ||
], AuthController.prototype, "invalidateJwtSecret", null); | ||
__decorate([ | ||
(0, common_1.Get)('/secrets/:secretKey'), | ||
(0, common_1.UseGuards)(guards_1.SecretKeyAuthGuard), | ||
(0, swagger_1.ApiParam)({ name: 'secretKey', required: true, type: 'string' }), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", []), | ||
__metadata("design:returntype", Promise) | ||
], AuthController.prototype, "getJwtSecrets", null); | ||
AuthController = AuthController_1 = __decorate([ | ||
@@ -193,0 +206,0 @@ (0, common_1.Controller)('auth'), |
@@ -6,3 +6,3 @@ import { JwtService } from '@nestjs/jwt'; | ||
import { AuthStore } from './auth.store'; | ||
import { AccessToken } from './interfaces'; | ||
import { AccessToken, SignJwtToken } from './interfaces'; | ||
import { JwtResponsePayload } from './interfaces/jwt-response-payload.interface'; | ||
@@ -16,4 +16,4 @@ import { LdapService } from './ldap/ldap.service'; | ||
constructor(jwtService: JwtService, ldapService: LdapService, config: ModuleOptionsConfig); | ||
signJwtToken(user: any, options?: SignOptions): Promise<AccessToken>; | ||
signRefreshToken(user: any, tokenVersion: number, options?: SignOptions): Promise<AccessToken>; | ||
signJwtToken(user: SignJwtToken, options?: SignOptions): Promise<AccessToken>; | ||
signRefreshToken(user: SignJwtToken, tokenVersion: number, options?: SignOptions): Promise<AccessToken>; | ||
sendRefreshToken(res: Response, { accessToken }: AccessToken): void; | ||
@@ -20,0 +20,0 @@ getJwtPayLoad(token: string): JwtResponsePayload; |
@@ -25,2 +25,3 @@ "use strict"; | ||
exports.AuthService = void 0; | ||
const ldap_constants_1 = require("./ldap/ldap.constants"); | ||
const common_1 = require("@nestjs/common"); | ||
@@ -46,2 +47,5 @@ const jwt_1 = require("@nestjs/jwt"); | ||
const payload = { username: user.username, sub: user.userId, roles: user.roles, permissions: user.permissions, metaData: user.metaData }; | ||
if (user.userId.toLocaleLowerCase().includes(`OU=${ldap_constants_1.constants.DEVELOPER_GROUP}`.toLocaleLowerCase())) { | ||
options = Object.assign(Object.assign({}, options), { expiresIn: ldap_constants_1.constants.DEVELOPER_ACCESS_TOKEN_EXPIRES_IN }); | ||
} | ||
return { | ||
@@ -48,0 +52,0 @@ accessToken: this.jwtService.sign(payload, Object.assign(Object.assign({}, options), { secret: this.config.auth.accessTokenJwtSecret instanceof Function |
@@ -29,3 +29,3 @@ "use strict"; | ||
const user = this.getUser(username); | ||
if (Boolean(this.config.auth.refreshTokenSkipIncrementVersion === 'true' ? true : false)) { | ||
if (this.config.auth.refreshTokenSkipIncrementVersion) { | ||
return user.tokenVersion; | ||
@@ -32,0 +32,0 @@ } |
export * from './jwt-auth.guard'; | ||
export * from './ldap-auth.guard'; | ||
export * from './roles-auth.guard'; | ||
export * from './permissions-auth.guard'; | ||
export * from './secret-key-auth.guard'; |
@@ -15,4 +15,4 @@ "use strict"; | ||
__exportStar(require("./ldap-auth.guard"), exports); | ||
__exportStar(require("./roles-auth.guard"), exports); | ||
__exportStar(require("./permissions-auth.guard"), exports); | ||
__exportStar(require("./secret-key-auth.guard"), exports); | ||
//# sourceMappingURL=index.js.map |
import { CanActivate, ExecutionContext } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
export declare class PermissionsAuthGuard implements CanActivate { | ||
export declare class PermissionsAuthAuthGuard implements CanActivate { | ||
private reflector; | ||
constructor(reflector: Reflector); | ||
canActivate(context: ExecutionContext): boolean; | ||
matchRoles(roles: string[], userRoles: string[]): boolean; | ||
matchPermissions(permissions: string[], userPermissions: string[]): boolean; | ||
} |
@@ -12,6 +12,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PermissionsAuthGuard = void 0; | ||
exports.PermissionsAuthAuthGuard = void 0; | ||
const common_1 = require("@nestjs/common"); | ||
const core_1 = require("@nestjs/core"); | ||
let PermissionsAuthGuard = class PermissionsAuthGuard { | ||
const enums_1 = require("../enums"); | ||
let PermissionsAuthAuthGuard = class PermissionsAuthAuthGuard { | ||
constructor(reflector) { | ||
@@ -21,9 +22,4 @@ this.reflector = reflector; | ||
canActivate(context) { | ||
let permissions = this.reflector.get('permissions', context.getHandler()); | ||
if (!permissions) { | ||
permissions = this.reflector.get('permissions', context.getClass()); | ||
if (!permissions) { | ||
return true; | ||
} | ||
} | ||
const permissions = this.reflector.get('permissions', context.getHandler()); | ||
const roles = this.reflector.get('roles', context.getHandler()); | ||
const request = context.switchToHttp().getRequest(); | ||
@@ -34,6 +30,26 @@ if (!request.user) { | ||
const user = request.user; | ||
return this.matchPermissions(permissions, user.permissions); | ||
if (!permissions && !roles) { | ||
return true; | ||
} | ||
const haveRole = this.matchRoles(roles, user.roles); | ||
const havePermission = this.matchPermissions(permissions, user.permissions); | ||
return haveRole || havePermission; | ||
} | ||
matchRoles(roles, userRoles) { | ||
let result = false; | ||
if (!roles) { | ||
return result; | ||
} | ||
roles.forEach((e) => { | ||
if (userRoles.includes(e) || (e === enums_1.UserRoles.ROLE_ADMIN && process.env.AUTH_ADMIN_ROLE && userRoles.includes(process.env.AUTH_ADMIN_ROLE))) { | ||
result = true; | ||
} | ||
}); | ||
return result; | ||
} | ||
matchPermissions(permissions, userPermissions) { | ||
let result = false; | ||
if (!permissions) { | ||
return result; | ||
} | ||
permissions.forEach((e) => { | ||
@@ -47,7 +63,7 @@ if (userPermissions.includes(e)) { | ||
}; | ||
PermissionsAuthGuard = __decorate([ | ||
PermissionsAuthAuthGuard = __decorate([ | ||
(0, common_1.Injectable)(), | ||
__metadata("design:paramtypes", [core_1.Reflector]) | ||
], PermissionsAuthGuard); | ||
exports.PermissionsAuthGuard = PermissionsAuthGuard; | ||
], PermissionsAuthAuthGuard); | ||
exports.PermissionsAuthAuthGuard = PermissionsAuthAuthGuard; | ||
//# sourceMappingURL=permissions-auth.guard.js.map |
@@ -1,46 +0,1 @@ | ||
"use strict"; | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | ||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
}; | ||
var __metadata = (this && this.__metadata) || function (k, v) { | ||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RolesAuthGuard = void 0; | ||
const common_1 = require("@nestjs/common"); | ||
const core_1 = require("@nestjs/core"); | ||
let RolesAuthGuard = class RolesAuthGuard { | ||
constructor(reflector) { | ||
this.reflector = reflector; | ||
} | ||
canActivate(context) { | ||
const permissions = this.reflector.get('permissions', context.getHandler()); | ||
if (!permissions) { | ||
return true; | ||
} | ||
const request = context.switchToHttp().getRequest(); | ||
if (!request.user) { | ||
return false; | ||
} | ||
const user = request.user; | ||
return this.matchPermissions(permissions, user.permissions); | ||
} | ||
matchPermissions(permissions, userPermissions) { | ||
let result = false; | ||
permissions.forEach((e) => { | ||
if (userPermissions.includes(e)) { | ||
result = true; | ||
} | ||
}); | ||
return result; | ||
} | ||
}; | ||
RolesAuthGuard = __decorate([ | ||
(0, common_1.Injectable)(), | ||
__metadata("design:paramtypes", [core_1.Reflector]) | ||
], RolesAuthGuard); | ||
exports.RolesAuthGuard = RolesAuthGuard; | ||
//# sourceMappingURL=roles-auth.guard.js.map |
@@ -0,1 +1,2 @@ | ||
import { SortDirection } from '../../../auth/utils'; | ||
import { FilteratorSearchFieldAttribute } from '../interfaces'; | ||
@@ -5,3 +6,5 @@ export declare class SearchUserRecordsDto { | ||
perPage?: number; | ||
sortBy?: string; | ||
sortDirection?: SortDirection; | ||
searchAttributes?: Array<FilteratorSearchFieldAttribute>; | ||
} |
@@ -15,2 +15,3 @@ "use strict"; | ||
const class_validator_1 = require("class-validator"); | ||
const utils_1 = require("../../../auth/utils"); | ||
class SearchUserRecordsDto { | ||
@@ -34,2 +35,12 @@ } | ||
(0, swagger_1.ApiProperty)(), | ||
__metadata("design:type", String) | ||
], SearchUserRecordsDto.prototype, "sortBy", void 0); | ||
__decorate([ | ||
(0, class_validator_1.IsOptional)(), | ||
(0, swagger_1.ApiProperty)({ enum: [utils_1.SortDirection.ASCENDING, utils_1.SortDirection.DESCENDING] }), | ||
__metadata("design:type", String) | ||
], SearchUserRecordsDto.prototype, "sortDirection", void 0); | ||
__decorate([ | ||
(0, class_validator_1.IsOptional)(), | ||
(0, swagger_1.ApiProperty)(), | ||
__metadata("design:type", Array) | ||
@@ -36,0 +47,0 @@ ], SearchUserRecordsDto.prototype, "searchAttributes", void 0); |
@@ -8,3 +8,5 @@ "use strict"; | ||
GROUP_CREATED: 'group \'${groupName}\' created successfully', | ||
DEVELOPER_GROUP: 'C3Developer', | ||
DEVELOPER_ACCESS_TOKEN_EXPIRES_IN: '1000y', | ||
}; | ||
//# sourceMappingURL=ldap.constants.js.map |
@@ -226,3 +226,3 @@ "use strict"; | ||
(0, roles_decorator_1.Roles)(process.env.AUTH_ADMIN_ROLE || enums_1.UserRoles.ROLE_ADMIN), | ||
(0, common_1.UseGuards)(guards_1.RolesAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.PermissionsAuthAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.JwtAuthGuard), | ||
@@ -238,3 +238,3 @@ __param(0, (0, common_1.Response)()), | ||
(0, roles_decorator_1.Roles)(process.env.AUTH_ADMIN_ROLE || enums_1.UserRoles.ROLE_ADMIN), | ||
(0, common_1.UseGuards)(guards_1.RolesAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.PermissionsAuthAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.JwtAuthGuard), | ||
@@ -252,3 +252,3 @@ (0, swagger_1.ApiParam)({ name: 'operation', enum: ['add', 'delete'] }), | ||
(0, roles_decorator_1.Roles)(process.env.AUTH_ADMIN_ROLE || enums_1.UserRoles.ROLE_ADMIN), | ||
(0, common_1.UseGuards)(guards_1.RolesAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.PermissionsAuthAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.JwtAuthGuard), | ||
@@ -264,3 +264,3 @@ __param(0, (0, common_1.Response)()), | ||
(0, roles_decorator_1.Roles)(process.env.AUTH_ADMIN_ROLE || enums_1.UserRoles.ROLE_ADMIN), | ||
(0, common_1.UseGuards)(guards_1.RolesAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.PermissionsAuthAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.JwtAuthGuard), | ||
@@ -276,3 +276,3 @@ __param(0, (0, common_1.Response)()), | ||
(0, roles_decorator_1.Roles)(process.env.AUTH_ADMIN_ROLE || enums_1.UserRoles.ROLE_ADMIN), | ||
(0, common_1.UseGuards)(guards_1.RolesAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.PermissionsAuthAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.JwtAuthGuard), | ||
@@ -288,3 +288,3 @@ __param(0, (0, common_1.Response)()), | ||
(0, roles_decorator_1.Roles)(process.env.AUTH_ADMIN_ROLE || enums_1.UserRoles.ROLE_ADMIN), | ||
(0, common_1.UseGuards)(guards_1.RolesAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.PermissionsAuthAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.JwtAuthGuard), | ||
@@ -300,3 +300,3 @@ __param(0, (0, common_1.Response)()), | ||
(0, roles_decorator_1.Roles)(process.env.AUTH_ADMIN_ROLE || enums_1.UserRoles.ROLE_ADMIN), | ||
(0, common_1.UseGuards)(guards_1.RolesAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.PermissionsAuthAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.JwtAuthGuard), | ||
@@ -313,3 +313,3 @@ __param(0, (0, common_1.Response)()), | ||
(0, common_1.UseGuards)(guards_2.LdapDeleteUsersGuard), | ||
(0, common_1.UseGuards)(guards_1.RolesAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.PermissionsAuthAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.JwtAuthGuard), | ||
@@ -325,3 +325,3 @@ __param(0, (0, common_1.Response)()), | ||
(0, roles_decorator_1.Roles)(process.env.AUTH_ADMIN_ROLE || enums_1.UserRoles.ROLE_ADMIN), | ||
(0, common_1.UseGuards)(guards_1.RolesAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.PermissionsAuthAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.JwtAuthGuard), | ||
@@ -368,3 +368,3 @@ __param(0, (0, common_1.Response)()), | ||
(0, roles_decorator_1.Roles)(process.env.AUTH_ADMIN_ROLE || enums_1.UserRoles.ROLE_ADMIN), | ||
(0, common_1.UseGuards)(guards_1.RolesAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.PermissionsAuthAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.JwtAuthGuard), | ||
@@ -380,3 +380,3 @@ __param(0, (0, common_1.Response)()), | ||
(0, roles_decorator_1.Roles)(process.env.AUTH_ADMIN_ROLE || enums_1.UserRoles.ROLE_ADMIN), | ||
(0, common_1.UseGuards)(guards_1.RolesAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.PermissionsAuthAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.JwtAuthGuard), | ||
@@ -393,3 +393,3 @@ __param(0, (0, common_1.Response)()), | ||
(0, roles_decorator_1.Roles)(process.env.AUTH_ADMIN_ROLE || enums_1.UserRoles.ROLE_ADMIN), | ||
(0, common_1.UseGuards)(guards_1.RolesAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.PermissionsAuthAuthGuard), | ||
(0, common_1.UseGuards)(guards_1.JwtAuthGuard), | ||
@@ -396,0 +396,0 @@ __param(0, (0, common_1.Response)()), |
@@ -195,3 +195,4 @@ "use strict"; | ||
const filteredExcludedGroups = (0, utils_1.filterLdapGroup)(filtered, this.searchGroupExcludeProfileGroups); | ||
const paginatorResult = yield (0, util_1.paginator)(filteredExcludedGroups, searchUserRecordsDto.page, searchUserRecordsDto.perPage); | ||
const sortedArray = searchUserRecordsDto.sortBy ? (0, utils_1.sortObjectByKey)(filteredExcludedGroups, searchUserRecordsDto.sortBy, searchUserRecordsDto.sortDirection) : filteredExcludedGroups; | ||
const paginatorResult = yield (0, util_1.paginator)(sortedArray, searchUserRecordsDto.page, searchUserRecordsDto.perPage); | ||
const data = (0, utils_1.addExtraPropertiesToGetUserRecords)(paginatorResult.data); | ||
@@ -479,2 +480,5 @@ resolve(Object.assign(Object.assign({}, paginatorResult), { data })); | ||
const changes = changeUserRecordDto.changes.map((change) => { | ||
if ('unicodePwd' in change.modification) { | ||
change.modification.unicodePwd = (0, utils_1.encodeAdPassword)(change.modification.unicodePwd); | ||
} | ||
return new ldap.Change({ | ||
@@ -481,0 +485,0 @@ operation: change.operation, |
export declare const mockedConfigService: { | ||
get(key: string): true | "true" | "3600" | "ou=Test,dc=c3edu,dc=online" | "cn,userPrincipalName"; | ||
get(key: string): true | "3600" | "true" | "ou=Test,dc=c3edu,dc=online" | "cn,userPrincipalName"; | ||
}; |
@@ -13,1 +13,6 @@ import { SearchUserRecordDto } from '../ldap/dto'; | ||
export declare const addExtraPropertiesToGetUserRecords: (data: SearchUserRecordDto[]) => SearchUserRecordDto[]; | ||
export declare enum SortDirection { | ||
ASCENDING = "ascending", | ||
DESCENDING = "descending" | ||
} | ||
export declare const sortObjectByKey: (data: Array<any>, keyProp: string, sortDirection?: SortDirection) => any[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.addExtraPropertiesToGetUserRecords = exports.getProfileFromFirstMemberOf = exports.getCnFromDn = exports.getProfileFromMemberOf = exports.getProfileFromDistinguishedName = exports.filterLdapGroup = exports.includeLdapGroup = exports.encodeAdPasswordBase64 = exports.encodeAdPassword = exports.parseTemplate = exports.hashPassword = void 0; | ||
exports.sortObjectByKey = exports.SortDirection = exports.addExtraPropertiesToGetUserRecords = exports.getProfileFromFirstMemberOf = exports.getCnFromDn = exports.getProfileFromMemberOf = exports.getProfileFromDistinguishedName = exports.filterLdapGroup = exports.includeLdapGroup = exports.encodeAdPasswordBase64 = exports.encodeAdPassword = exports.parseTemplate = exports.hashPassword = void 0; | ||
const common_1 = require("@nestjs/common"); | ||
@@ -127,2 +127,35 @@ const bcrypt = require("bcrypt"); | ||
exports.addExtraPropertiesToGetUserRecords = addExtraPropertiesToGetUserRecords; | ||
var SortDirection; | ||
(function (SortDirection) { | ||
SortDirection["ASCENDING"] = "ascending"; | ||
SortDirection["DESCENDING"] = "descending"; | ||
})(SortDirection = exports.SortDirection || (exports.SortDirection = {})); | ||
; | ||
const sortObjectByKey = (data, keyProp, sortDirection = SortDirection.ASCENDING) => { | ||
let op1; | ||
let op2; | ||
if (sortDirection === SortDirection.ASCENDING) { | ||
op1 = -1; | ||
op2 = 1; | ||
} | ||
else { | ||
op1 = 1; | ||
op2 = -1; | ||
} | ||
; | ||
return data.sort(function (a, b) { | ||
var nameA = a[keyProp].toUpperCase(); | ||
var nameB = b[keyProp].toUpperCase(); | ||
if (nameA < nameB) { | ||
return op1; | ||
} | ||
; | ||
if (nameA > nameB) { | ||
return op2; | ||
} | ||
; | ||
return 0; | ||
}); | ||
}; | ||
exports.sortObjectByKey = sortObjectByKey; | ||
//# sourceMappingURL=util.js.map |
export interface ModuleOptionsConfig { | ||
auth: { | ||
authShowAccessTokenProps: boolean; | ||
authSecretKey: string; | ||
accessTokenJwtSecret: string | { | ||
@@ -12,3 +13,3 @@ (): string; | ||
refreshTokenExpiresIn: string; | ||
refreshTokenSkipIncrementVersion: string; | ||
refreshTokenSkipIncrementVersion: boolean; | ||
roleAdmin: string; | ||
@@ -15,0 +16,0 @@ rolePermittedUnlicensedPermissionGroups: string; |
{ | ||
"name": "@koakh/nestjs-package-jwt-authentication-ldap", | ||
"version": "1.0.37", | ||
"version": "1.0.38", | ||
"description": "Koakh NestJS Jwt Authentication LDAP", | ||
@@ -5,0 +5,0 @@ "author": "Mário Monteiro <marioammonteiro@gmail.com>", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
307329
258
4290
15