@sphereon/ssi-express-support
Version:
146 lines • 5.81 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MapBasedStaticBearerUserProvider = exports.StaticBearerAuth = void 0;
const passport_1 = __importDefault(require("passport"));
const u8a = __importStar(require("uint8arrays"));
class StaticBearerAuth {
static init(strategy, provider) {
return new StaticBearerAuth(strategy !== null && strategy !== void 0 ? strategy : 'bearer', provider !== null && provider !== void 0 ? provider : new MapBasedStaticBearerUserProvider(strategy));
}
constructor(strategy, provider) {
this.hashTokens = false;
this.strategy = strategy;
if (StaticBearerAuth.providers.has(strategy)) {
if (StaticBearerAuth.providers.get(strategy) !== provider) {
throw Error('Cannot register another user provider for strategy: ' + strategy);
}
}
else {
StaticBearerAuth.providers.set(strategy, provider);
}
}
get provider() {
const provider = StaticBearerAuth.providers.get(this.strategy);
if (!provider) {
throw Error('Could not get user provider for ' + this.strategy);
}
return provider;
}
withHashTokens(hashTokens) {
this.hashTokens = hashTokens;
return this;
}
withUsers(users) {
this.addUser(users);
return this;
}
addUser(user) {
this.provider.addUser(user);
return this;
}
withVerifyOptions(options) {
StaticBearerAuth.verifyOptions.set(this.strategy, options);
return this;
}
connectPassport() {
const _provider = this.provider;
function findUser(token, cb) {
const user = _provider.getUser(token);
if (user) {
return cb(null, user);
}
return cb('bearer token not found or incorrect', false);
}
Promise.resolve().then(() => __importStar(require('passport-http-bearer'))).then((httpBearer) => {
var _a;
const hashTokens = (_a = this.hashTokens) !== null && _a !== void 0 ? _a : false;
passport_1.default.use(this.strategy, new httpBearer.Strategy({ passReqToCallback: false }, function (token, cb) {
if (hashTokens) {
Promise.resolve().then(() => __importStar(require('@noble/hashes/sha256'))).then((hash) => {
findUser(u8a.toString(hash.sha256(token)), cb);
})
.catch((error) => {
console.log(`hash problem: ${error}`);
throw Error('Did you include @noble/hashes in package.json?');
});
}
else {
findUser(token, cb);
}
}));
})
.catch((error) => {
console.log(`passport-http-bearer package problem: ${error}`);
throw Error('Did you include passport-http-bearer in package.json?');
});
}
}
exports.StaticBearerAuth = StaticBearerAuth;
StaticBearerAuth.providers = new Map();
StaticBearerAuth.verifyOptions = new Map();
class MapBasedStaticBearerUserProvider {
constructor(strategy, hashedTokens) {
this._users = [];
this._strategy = strategy;
this._hashedTokens = hashedTokens !== null && hashedTokens !== void 0 ? hashedTokens : false;
}
get users() {
return this._users;
}
get hashedTokens() {
return this._hashedTokens;
}
get strategy() {
return this._strategy;
}
getUser(token) {
return this.users.find((user) => user.token === token);
}
addUser(user, hashToken) {
const users = Array.isArray(user) ? user : [user];
if (hashToken) {
if (!this.hashedTokens) {
throw Error('Cannot hash token, when hashed tokens is not enabled on the user provider for strategy ' + this.strategy);
}
Promise.resolve().then(() => __importStar(require('@noble/hashes/sha256'))).then((hash) => {
users.forEach((user) => (user.token = u8a.toString(hash.sha256(user.token))));
})
.catch((error) => {
console.log(`hash problem: ${error}`);
throw Error('Did you include @noble/hashes in package.json?');
});
}
this._users.push(...users);
}
getUsers() {
return this._users;
}
}
exports.MapBasedStaticBearerUserProvider = MapBasedStaticBearerUserProvider;
//# sourceMappingURL=static-bearer-auth.js.map