nest-phylax
Version:
Security library for NestJS
131 lines • 7.22 kB
JavaScript
"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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JwtAuthService = void 0;
const common_1 = require("@nestjs/common");
const common_2 = require("../../common");
const jwt_util_1 = require("../jwt.util");
const jwt_auth_constants_1 = require("../jwt.auth.constants");
let JwtAuthService = class JwtAuthService {
constructor(options, userRepository, passwordEncoder) {
this.options = options;
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}
async passwordLogin({ email, password, config }) {
var _a, _b, _c, _d, _e, _f, _g, _h;
email = email.toLowerCase();
const user = await this.userRepository.findUserByUsername(email);
if (!user) {
throw new common_1.UnauthorizedException({
message: 'Username or password is incorrect',
});
}
if (!user.password) {
throw new common_1.UnauthorizedException({
message: 'Username or password is incorrect',
});
}
const isPasswordValid = await this.passwordEncoder.compare(password, user.password);
if (!isPasswordValid) {
throw new common_1.UnauthorizedException({
message: 'Username or password is incorrect',
});
}
const jwtUtil = new jwt_util_1.JwtUtil();
let refreshToken = null;
if (config === null || config === void 0 ? void 0 : config.generateRefreshToken) {
if (!this.options.refreshTokenConfig) {
throw new Error('Refresh token secret key is not set');
}
const refreshTokenString = jwtUtil.generateToken({
user,
secretKey: (_b = (_a = this === null || this === void 0 ? void 0 : this.options) === null || _a === void 0 ? void 0 : _a.refreshTokenConfig) === null || _b === void 0 ? void 0 : _b.secretKey,
signOptions: {
expiresIn: (_d = (_c = this === null || this === void 0 ? void 0 : this.options) === null || _c === void 0 ? void 0 : _c.refreshTokenConfig) === null || _d === void 0 ? void 0 : _d.expiresIn,
},
});
refreshToken = {
value: refreshTokenString,
sub: user.id,
role: await this.userRepository.getUserRole(user),
};
await this.userRepository.saveRefreshToken(refreshToken, user.id);
}
return {
accessToken: {
value: jwtUtil.generateToken({
user,
secretKey: (_f = (_e = this === null || this === void 0 ? void 0 : this.options) === null || _e === void 0 ? void 0 : _e.accessTokenConfig) === null || _f === void 0 ? void 0 : _f.secretKey,
signOptions: {
expiresIn: ((_h = (_g = this === null || this === void 0 ? void 0 : this.options) === null || _g === void 0 ? void 0 : _g.accessTokenConfig) === null || _h === void 0 ? void 0 : _h.expiresIn) || '1h',
},
}),
sub: user.id,
role: await this.userRepository.getUserRole(user),
},
refreshToken,
};
}
async refreshToken(refreshToken) {
var _a, _b, _c, _d, _e, _f, _g, _h;
const jwtUtil = new jwt_util_1.JwtUtil();
const decoded = jwtUtil.verifyJwt(refreshToken, (_b = (_a = this === null || this === void 0 ? void 0 : this.options) === null || _a === void 0 ? void 0 : _a.refreshTokenConfig) === null || _b === void 0 ? void 0 : _b.secretKey);
if (!(decoded === null || decoded === void 0 ? void 0 : decoded.sub)) {
throw new common_1.UnauthorizedException({
message: 'Invalid refresh token',
});
}
const user = await this.userRepository.findUserByUsername((_c = decoded === null || decoded === void 0 ? void 0 : decoded.sub) === null || _c === void 0 ? void 0 : _c.toString());
if (!user) {
throw new common_1.UnauthorizedException({
message: jwt_auth_constants_1.AUTHENTICATION_FAILED_ERROR_MESSAGE,
});
}
const refreshTokens = (_d = user === null || user === void 0 ? void 0 : user.getRefreshTokens) === null || _d === void 0 ? void 0 : _d.call(user);
if (!refreshTokens) {
throw new common_1.UnauthorizedException({
message: jwt_auth_constants_1.AUTHENTICATION_FAILED_ERROR_MESSAGE,
});
}
const refreshTokenObj = refreshTokens.find(token => (decoded === null || decoded === void 0 ? void 0 : decoded.jti) === (token === null || token === void 0 ? void 0 : token.jti));
if (!refreshTokenObj) {
throw new common_1.UnauthorizedException({
message: jwt_auth_constants_1.AUTHENTICATION_FAILED_ERROR_MESSAGE,
});
}
return {
accessToken: {
value: jwtUtil.generateToken({
user,
secretKey: (_f = (_e = this === null || this === void 0 ? void 0 : this.options) === null || _e === void 0 ? void 0 : _e.accessTokenConfig) === null || _f === void 0 ? void 0 : _f.secretKey,
signOptions: {
expiresIn: ((_h = (_g = this === null || this === void 0 ? void 0 : this.options) === null || _g === void 0 ? void 0 : _g.accessTokenConfig) === null || _h === void 0 ? void 0 : _h.expiresIn) || '1h',
},
}),
sub: user.id,
role: await this.userRepository.getUserRole(user),
},
};
}
};
JwtAuthService = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(common_2.JWT_AUTH_MODULE_OPTIONS_TOKEN)),
__param(1, (0, common_1.Inject)(common_2.USER_REPOSITORY_TOKEN)),
__param(2, (0, common_1.Inject)(common_2.PASSWORD_ENCODER_TOKEN)),
__metadata("design:paramtypes", [Object, Object, Object])
], JwtAuthService);
exports.JwtAuthService = JwtAuthService;
//# sourceMappingURL=jwt.auth.service.js.map