rauth
Version:
Authentication and Authorization library via JWT
75 lines • 2.91 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const JWTControl_1 = require("./JWTControl");
const ms_1 = __importDefault(require("ms"));
exports.msToSec = (inp) => {
return Math.floor(ms_1.default(inp.toString() ?? '1h') / 1000);
};
exports.scopeToString = (scope) => {
if (Array.isArray(scope))
return scope.join(' ');
return scope;
};
class Session {
constructor(options) {
this.options = options;
this.refreshAt = this.options?.refreshAt;
this.createdAt = this.options?.createdAt;
this.sessionControl = this.options?.sessionControl;
this.jwtControl = this.options?.sessionControl?.jwtControl ?? new JWTControl_1.JWTControl();
this.userId = this.options?.userId;
this.scope = exports.scopeToString(this.options?.scope);
this.sessionId = this.options?.sessionId;
this.clientId = this.options?.clientId;
this.data = this.options?.data;
this.meta = this.options?.meta;
this.otherDataSession = this.options?.otherDataSession;
this.mode = this.options?.mode ?? 'Token';
this.accessTokenExpires = exports.msToSec(this.sessionControl?.accessTokenExpires ?? '1h');
this.refreshTokenExpires = exports.msToSec(this.sessionControl?.refreshTokenExpires ?? '4w');
}
static from({ userId, scope, sessionId, data, meta, clientId, mode, refreshAt, createdAt, ...otherDataSession }, sessionControl) {
return new Session({ userId, scope, sessionId, data, meta, clientId, mode, sessionControl, otherDataSession, refreshAt, createdAt });
}
get refreshToken() {
if (this.mode === 'OnlyAccessToken')
return undefined;
return this.jwtControl.sign({
userId: this.userId,
sessionId: this.sessionId,
scope: this.scope,
refreshAt: this.refreshAt,
createdAt: this.createdAt,
clientId: this.clientId,
}, {
subject: 'refresh_token',
expiresIn: this.refreshTokenExpires,
});
}
get accessToken() {
return this.jwtControl.sign({
userId: this.userId,
scope: this.scope,
sessionId: this.sessionId,
data: this.data,
refreshAt: this.refreshAt,
createdAt: this.createdAt,
clientId: this.clientId,
}, {
subject: 'access_token',
expiresIn: this.accessTokenExpires,
});
}
toJSON() {
return {
access_token: this.accessToken,
refresh_token: this.refreshToken,
expires_in: this.accessTokenExpires,
};
}
}
exports.Session = Session;
//# sourceMappingURL=Session.js.map