realm-object-server
Version:
136 lines • 4.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const crypto = require("crypto");
const Constants_1 = require("./Constants");
const jwt = require("jsonwebtoken");
const moment = require("moment");
const uuid = require("uuid");
const util_1 = require("./util");
class Token {
constructor(params) {
this.identity = params.identity;
this.appId = params.appId;
this.expires = params.expires;
this.canSkipRevocationCheck = params.canSkipRevocationCheck;
this.tokenId = util_1.getValueOrDefault(params.tokenId, uuid.v4());
}
toJSON() {
const json = {
app_id: this.appId,
identity: this.identity,
access: this.access,
tokenId: this.tokenId,
};
if (this.expires) {
json.expires = this.expires;
}
if (this.canSkipRevocationCheck) {
json.canSkipRevocationCheck = this.canSkipRevocationCheck;
}
return json;
}
data() {
return JSON.stringify(this.toJSON());
}
encodedData() {
return Buffer.from(this.data()).toString("base64");
}
getRevocationId() {
return this.tokenId || this.encodedData();
}
getPayload() {
const payload = {};
for (const key of Object.keys(this)) {
switch (key) {
case util_1.nameof("identity"):
payload.sub = this[key];
break;
case util_1.nameof("expires"):
case util_1.nameof("tokenId"):
break;
default:
payload[key] = this[key];
break;
}
}
return payload;
}
sign(privateKey) {
const payload = this.getPayload();
const options = {
audience: "realm",
issuer: "realm",
algorithm: "RS256",
};
if (this.expires) {
options.expiresIn = moment.unix(this.expires).diff(moment(), "seconds");
}
if (this.tokenId) {
options.jwtid = this.tokenId;
}
return jwt.sign(payload, privateKey, options);
}
}
exports.Token = Token;
class UserToken extends Token {
constructor(params) {
super(params);
this.isAdmin = params.isAdmin;
}
toJSON() {
return Object.assign(super.toJSON(), {
is_admin: this.isAdmin,
});
}
}
exports.UserToken = UserToken;
class RefreshToken extends Token {
constructor(params) {
super(params);
this.access = ["refresh"];
this.isAdmin = params.isAdmin;
this.isEmailConfirmed = params.isEmailConfirmed || false;
}
toJSON() {
return Object.assign(super.toJSON(), {
is_admin: this.isAdmin,
isEmailConfirmed: this.isEmailConfirmed
});
}
}
exports.RefreshToken = RefreshToken;
class AccessToken extends Token {
constructor(params) {
super(params);
this.access = params.access;
this.path = params.path;
this.syncLabel = params.syncLabel;
this.salt = params.salt || crypto.randomBytes(4).toString("hex");
}
toJSON() {
return Object.assign(super.toJSON(), {
path: this.path,
sync_label: this.syncLabel,
salt: this.salt,
});
}
isAdminToken() {
if (!this.path && this.identity === Constants_1.Constants.AdminUserId) {
return true;
}
return false;
}
}
exports.AccessToken = AccessToken;
function generateAdminToken({ privateKey, expires, access = ["download", "upload", "manage"], appId = "io.realm.auth", canSkipRevocationCheck = false, }) {
const accessToken = new AccessToken({
identity: Constants_1.Constants.AdminUserId,
appId: appId,
access: access,
expires: expires,
canSkipRevocationCheck,
});
return accessToken.sign(privateKey);
}
exports.generateAdminToken = generateAdminToken;
//# sourceMappingURL=Token.js.map