vulcain-corejs
Version: 
Vulcain micro-service framework
66 lines (64 loc) • 3.13 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments)).next());
    });
};
const apiKeyStrategy_1 = require("./apiKeyStrategy");
const bearerStrategy_1 = require("./bearerStrategy");
const system_1 = require("../configurations/globals/system");
const passport = require('passport');
const AnonymousStrategy = require('passport-anonymous');
class AuthenticationStrategies {
    static initAnonymous() {
        passport.use(new AnonymousStrategy());
    }
    static initBearer() {
        let strategy = new bearerStrategy_1.BearerStrategy((accessToken, callback, ctx) => __awaiter(this, void 0, void 0, function* () {
            try {
                let tokens = ctx.container.get("TokenService");
                let token = yield tokens.verifyTokenAsync({ token: accessToken, tenant: ctx.tenant });
                // No token found
                if (!token) {
                    system_1.System.log.info(ctx, "request: Invalid jwtToken " + accessToken);
                    return callback(null, false);
                }
                token.user.tenant = token.user.tenant || token.tenantId;
                token.user.scopes = token.scopes;
                token.user.data = token.user.data || token.data;
                token.user.bearer = accessToken;
                callback(null, token.user);
            }
            catch (err) {
                return callback(null, false);
            }
        }));
        passport.use(strategy);
    }
    static initApiKey() {
        let strategy = new apiKeyStrategy_1.ApiKeyStrategy((params, callback, ctx) => __awaiter(this, void 0, void 0, function* () {
            try {
                let apiKeys = ctx.container.get("ApiKeyService");
                let token = yield apiKeys.verifyTokenAsync(params);
                // No token found
                if (!token) {
                    system_1.System.log.info(ctx, "request: Invalid apiKey " + params.token);
                    return callback(null, false);
                }
                token.user.data = token.user.data || token.data;
                token.user.scopes = Array.isArray(token.token.scopes) ? token.token.scopes : [token.token.scopes];
                callback(null, token.user);
            }
            catch (err) {
                return callback(null, false);
            }
        }));
        passport.use(strategy);
    }
}
exports.AuthenticationStrategies = AuthenticationStrategies;
AuthenticationStrategies.Anonymous = { id: "_anonymous", name: "anonymous", scopes: [], tenant: null };
//# sourceMappingURL=authenticationStrategies.js.map