| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052 |
- import urljoin from 'url-join';
- import luceneQueryParser from 'lucene-query-parser';
- import passport from 'passport';
- import LdapStrategy from 'passport-ldapauth';
- import { Strategy as LocalStrategy } from 'passport-local';
- import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
- import { Strategy as GitHubStrategy } from 'passport-github';
- import { Strategy as TwitterStrategy } from 'passport-twitter';
- import { Strategy as OidcStrategy, Issuer as OIDCIssuer } from 'openid-client';
- import { Profile, Strategy as SamlStrategy, VerifiedCallback } from 'passport-saml';
- import { BasicStrategy } from 'passport-http';
- import { IncomingMessage } from 'http';
- import got from 'got';
- import pRetry from 'p-retry';
- import loggerFactory from '~/utils/logger';
- import S2sMessage from '../models/vo/s2s-message';
- import { S2sMessageHandlable } from './s2s-messaging/handlable';
- const logger = loggerFactory('growi:service:PassportService');
- interface IncomingMessageWithLdapAccountInfo extends IncomingMessage {
- ldapAccountInfo: any;
- }
- /**
- * the service class of Passport
- */
- class PassportService implements S2sMessageHandlable {
- // see '/lib/form/login.js'
- static get USERNAME_FIELD() { return 'loginForm[username]' }
- static get PASSWORD_FIELD() { return 'loginForm[password]' }
- crowi!: any;
- lastLoadedAt?: Date;
- /**
- * the flag whether LocalStrategy is set up successfully
- */
- isLocalStrategySetup = false;
- /**
- * the flag whether LdapStrategy is set up successfully
- */
- isLdapStrategySetup = false;
- /**
- * the flag whether GoogleStrategy is set up successfully
- */
- isGoogleStrategySetup = false;
- /**
- * the flag whether GitHubStrategy is set up successfully
- */
- isGitHubStrategySetup = false;
- /**
- * the flag whether TwitterStrategy is set up successfully
- */
- isTwitterStrategySetup = false;
- /**
- * the flag whether OidcStrategy is set up successfully
- */
- isOidcStrategySetup = false;
- /**
- * the flag whether SamlStrategy is set up successfully
- */
- isSamlStrategySetup = false;
- /**
- * the flag whether BasicStrategy is set up successfully
- */
- isBasicStrategySetup = false;
- /**
- * the flag whether serializer/deserializer are set up successfully
- */
- isSerializerSetup = false;
- /**
- * the keys of mandatory configs for SAML
- */
- mandatoryConfigKeysForSaml = [
- 'security:passport-saml:entryPoint',
- 'security:passport-saml:issuer',
- 'security:passport-saml:cert',
- 'security:passport-saml:attrMapId',
- 'security:passport-saml:attrMapUsername',
- 'security:passport-saml:attrMapMail',
- ];
- setupFunction = {
- local: {
- setup: 'setupLocalStrategy',
- reset: 'resetLocalStrategy',
- },
- ldap: {
- setup: 'setupLdapStrategy',
- reset: 'resetLdapStrategy',
- },
- saml: {
- setup: 'setupSamlStrategy',
- reset: 'resetSamlStrategy',
- },
- oidc: {
- setup: 'setupOidcStrategy',
- reset: 'resetOidcStrategy',
- },
- basic: {
- setup: 'setupBasicStrategy',
- reset: 'resetBasicStrategy',
- },
- google: {
- setup: 'setupGoogleStrategy',
- reset: 'resetGoogleStrategy',
- },
- github: {
- setup: 'setupGitHubStrategy',
- reset: 'resetGitHubStrategy',
- },
- twitter: {
- setup: 'setupTwitterStrategy',
- reset: 'resetTwitterStrategy',
- },
- };
- constructor(crowi: any) {
- this.crowi = crowi;
- }
- /**
- * @inheritdoc
- */
- shouldHandleS2sMessage(s2sMessage) {
- const { eventName, updatedAt, strategyId } = s2sMessage;
- if (eventName !== 'passportServiceUpdated' || updatedAt == null || strategyId == null) {
- return false;
- }
- return this.lastLoadedAt == null || this.lastLoadedAt < new Date(s2sMessage.updatedAt);
- }
- /**
- * @inheritdoc
- */
- async handleS2sMessage(s2sMessage) {
- const { configManager } = this.crowi;
- const { strategyId } = s2sMessage;
- logger.info('Reset strategy by pubsub notification');
- await configManager.loadConfigs();
- return this.setupStrategyById(strategyId);
- }
- async publishUpdatedMessage(strategyId) {
- const { s2sMessagingService } = this.crowi;
- if (s2sMessagingService != null) {
- const s2sMessage = new S2sMessage('passportStrategyReloaded', {
- updatedAt: new Date(),
- strategyId,
- });
- try {
- await s2sMessagingService.publish(s2sMessage);
- }
- catch (e) {
- logger.error('Failed to publish update message with S2sMessagingService: ', e.message);
- }
- }
- }
- /**
- * get SetupStrategies
- *
- * @return {Array}
- * @memberof PassportService
- */
- getSetupStrategies() {
- const setupStrategies: string[] = [];
- if (this.isLocalStrategySetup) { setupStrategies.push('local') }
- if (this.isLdapStrategySetup) { setupStrategies.push('ldap') }
- if (this.isSamlStrategySetup) { setupStrategies.push('saml') }
- if (this.isOidcStrategySetup) { setupStrategies.push('oidc') }
- if (this.isBasicStrategySetup) { setupStrategies.push('basic') }
- if (this.isGoogleStrategySetup) { setupStrategies.push('google') }
- if (this.isGitHubStrategySetup) { setupStrategies.push('github') }
- if (this.isTwitterStrategySetup) { setupStrategies.push('twitter') }
- return setupStrategies;
- }
- /**
- * get SetupFunction
- *
- * @return {Object}
- * @param {string} authId
- */
- getSetupFunction(authId) {
- return this.setupFunction[authId];
- }
- /**
- * setup strategy by target name
- */
- async setupStrategyById(authId) {
- const func = this.getSetupFunction(authId);
- try {
- this[func.setup]();
- }
- catch (err) {
- logger.debug(err);
- this[func.reset]();
- }
- this.lastLoadedAt = new Date();
- }
- /**
- * reset LocalStrategy
- *
- * @memberof PassportService
- */
- resetLocalStrategy() {
- logger.debug('LocalStrategy: reset');
- passport.unuse('local');
- this.isLocalStrategySetup = false;
- }
- /**
- * setup LocalStrategy
- *
- * @memberof PassportService
- */
- setupLocalStrategy() {
- this.resetLocalStrategy();
- const { configManager } = this.crowi;
- const isEnabled = configManager.getConfig('crowi', 'security:passport-local:isEnabled');
- // when disabled
- if (!isEnabled) {
- return;
- }
- logger.debug('LocalStrategy: setting up..');
- const User = this.crowi.model('User');
- passport.use(new LocalStrategy(
- {
- usernameField: PassportService.USERNAME_FIELD,
- passwordField: PassportService.PASSWORD_FIELD,
- },
- (username, password, done) => {
- // find user
- User.findUserByUsernameOrEmail(username, password, (err, user) => {
- if (err) { return done(err) }
- // check existence and password
- if (!user || !user.isPasswordValid(password)) {
- return done(null, false, { message: 'Incorrect credentials.' });
- }
- return done(null, user);
- });
- },
- ));
- this.isLocalStrategySetup = true;
- logger.debug('LocalStrategy: setup is done');
- }
- /**
- * reset LdapStrategy
- *
- * @memberof PassportService
- */
- resetLdapStrategy() {
- logger.debug('LdapStrategy: reset');
- passport.unuse('ldapauth');
- this.isLdapStrategySetup = false;
- }
- /**
- * Asynchronous configuration retrieval
- *
- * @memberof PassportService
- */
- setupLdapStrategy() {
- this.resetLdapStrategy();
- const config = this.crowi.config;
- const { configManager } = this.crowi;
- const isLdapEnabled = configManager.getConfig('crowi', 'security:passport-ldap:isEnabled');
- // when disabled
- if (!isLdapEnabled) {
- return;
- }
- logger.debug('LdapStrategy: setting up..');
- passport.use(new LdapStrategy(this.getLdapConfigurationFunc(config, { passReqToCallback: true }),
- (req, ldapAccountInfo, done) => {
- logger.debug('LDAP authentication has succeeded', ldapAccountInfo);
- // store ldapAccountInfo to req
- (req as IncomingMessageWithLdapAccountInfo).ldapAccountInfo = ldapAccountInfo;
- done(null, ldapAccountInfo);
- }));
- this.isLdapStrategySetup = true;
- logger.debug('LdapStrategy: setup is done');
- }
- /**
- * return attribute name for mapping to username of Crowi DB
- *
- * @returns
- * @memberof PassportService
- */
- getLdapAttrNameMappedToUsername() {
- return this.crowi.configManager.getConfig('crowi', 'security:passport-ldap:attrMapUsername') || 'uid';
- }
- /**
- * return attribute name for mapping to name of Crowi DB
- *
- * @returns
- * @memberof PassportService
- */
- getLdapAttrNameMappedToName() {
- return this.crowi.configManager.getConfig('crowi', 'security:passport-ldap:attrMapName') || '';
- }
- /**
- * return attribute name for mapping to name of Crowi DB
- *
- * @returns
- * @memberof PassportService
- */
- getLdapAttrNameMappedToMail() {
- return this.crowi.configManager.getConfig('crowi', 'security:passport-ldap:attrMapMail') || 'mail';
- }
- /**
- * CAUTION: this method is capable to use only when `req.body.loginForm` is not null
- *
- * @param {any} req
- * @returns
- * @memberof PassportService
- */
- getLdapAccountIdFromReq(req) {
- return req.body.loginForm.username;
- }
- /**
- * Asynchronous configuration retrieval
- * @see https://github.com/vesse/passport-ldapauth#asynchronous-configuration-retrieval
- *
- * @param {object} config
- * @param {object} opts
- * @returns
- * @memberof PassportService
- */
- getLdapConfigurationFunc(config, opts) {
- /* eslint-disable no-multi-spaces */
- const { configManager } = this.crowi;
- // get configurations
- const isUserBind = configManager.getConfig('crowi', 'security:passport-ldap:isUserBind');
- const serverUrl = configManager.getConfig('crowi', 'security:passport-ldap:serverUrl');
- const bindDN = configManager.getConfig('crowi', 'security:passport-ldap:bindDN');
- const bindCredentials = configManager.getConfig('crowi', 'security:passport-ldap:bindDNPassword');
- const searchFilter = configManager.getConfig('crowi', 'security:passport-ldap:searchFilter') || '(uid={{username}})';
- const groupSearchBase = configManager.getConfig('crowi', 'security:passport-ldap:groupSearchBase');
- const groupSearchFilter = configManager.getConfig('crowi', 'security:passport-ldap:groupSearchFilter');
- const groupDnProperty = configManager.getConfig('crowi', 'security:passport-ldap:groupDnProperty') || 'uid';
- /* eslint-enable no-multi-spaces */
- // parse serverUrl
- // see: https://regex101.com/r/0tuYBB/1
- const match = serverUrl.match(/(ldaps?:\/\/[^/]+)\/(.*)?/);
- if (match == null || match.length < 1) {
- logger.debug('LdapStrategy: serverUrl is invalid');
- return (req, callback) => { callback({ message: 'serverUrl is invalid' }) };
- }
- const url = match[1];
- const searchBase = match[2] || '';
- logger.debug(`LdapStrategy: url=${url}`);
- logger.debug(`LdapStrategy: searchBase=${searchBase}`);
- logger.debug(`LdapStrategy: isUserBind=${isUserBind}`);
- if (!isUserBind) {
- logger.debug(`LdapStrategy: bindDN=${bindDN}`);
- logger.debug(`LdapStrategy: bindCredentials=${bindCredentials}`);
- }
- logger.debug(`LdapStrategy: searchFilter=${searchFilter}`);
- logger.debug(`LdapStrategy: groupSearchBase=${groupSearchBase}`);
- logger.debug(`LdapStrategy: groupSearchFilter=${groupSearchFilter}`);
- logger.debug(`LdapStrategy: groupDnProperty=${groupDnProperty}`);
- return (req, callback) => {
- // get credentials from form data
- const loginForm = req.body.loginForm;
- if (!req.form.isValid) {
- return callback({ message: 'Incorrect credentials.' });
- }
- // user bind
- const fixedBindDN = (isUserBind)
- ? bindDN.replace(/{{username}}/, loginForm.username)
- : bindDN;
- const fixedBindCredentials = (isUserBind) ? loginForm.password : bindCredentials;
- let serverOpt = {
- url,
- bindDN: fixedBindDN,
- bindCredentials: fixedBindCredentials,
- searchBase,
- searchFilter,
- attrMapUsername: this.getLdapAttrNameMappedToUsername(),
- attrMapName: this.getLdapAttrNameMappedToName(),
- };
- if (groupSearchBase && groupSearchFilter) {
- serverOpt = Object.assign(serverOpt, { groupSearchBase, groupSearchFilter, groupDnProperty });
- }
- process.nextTick(() => {
- const mergedOpts = Object.assign({
- usernameField: PassportService.USERNAME_FIELD,
- passwordField: PassportService.PASSWORD_FIELD,
- server: serverOpt,
- }, opts);
- logger.debug('ldap configuration: ', mergedOpts);
- // store configuration to req
- req.ldapConfiguration = mergedOpts;
- callback(null, mergedOpts);
- });
- };
- }
- /**
- * Asynchronous configuration retrieval
- *
- * @memberof PassportService
- */
- setupGoogleStrategy() {
- this.resetGoogleStrategy();
- const { configManager } = this.crowi;
- const isGoogleEnabled = configManager.getConfig('crowi', 'security:passport-google:isEnabled');
- // when disabled
- if (!isGoogleEnabled) {
- return;
- }
- logger.debug('GoogleStrategy: setting up..');
- passport.use(
- new GoogleStrategy(
- {
- clientID: configManager.getConfig('crowi', 'security:passport-google:clientId'),
- clientSecret: configManager.getConfig('crowi', 'security:passport-google:clientSecret'),
- callbackURL: (this.crowi.appService.getSiteUrl() != null)
- ? urljoin(this.crowi.appService.getSiteUrl(), '/passport/google/callback') // auto-generated with v3.2.4 and above
- : configManager.getConfig('crowi', 'security:passport-google:callbackUrl'), // DEPRECATED: backward compatible with v3.2.3 and below
- skipUserProfile: false,
- },
- (accessToken, refreshToken, profile, done) => {
- if (profile) {
- return done(null, profile);
- }
- return done(null, false);
- },
- ),
- );
- this.isGoogleStrategySetup = true;
- logger.debug('GoogleStrategy: setup is done');
- }
- /**
- * reset GoogleStrategy
- *
- * @memberof PassportService
- */
- resetGoogleStrategy() {
- logger.debug('GoogleStrategy: reset');
- passport.unuse('google');
- this.isGoogleStrategySetup = false;
- }
- setupGitHubStrategy() {
- this.resetGitHubStrategy();
- const { configManager } = this.crowi;
- const isGitHubEnabled = configManager.getConfig('crowi', 'security:passport-github:isEnabled');
- // when disabled
- if (!isGitHubEnabled) {
- return;
- }
- logger.debug('GitHubStrategy: setting up..');
- passport.use(
- new GitHubStrategy(
- {
- clientID: configManager.getConfig('crowi', 'security:passport-github:clientId'),
- clientSecret: configManager.getConfig('crowi', 'security:passport-github:clientSecret'),
- callbackURL: (this.crowi.appService.getSiteUrl() != null)
- ? urljoin(this.crowi.appService.getSiteUrl(), '/passport/github/callback') // auto-generated with v3.2.4 and above
- : configManager.getConfig('crowi', 'security:passport-github:callbackUrl'), // DEPRECATED: backward compatible with v3.2.3 and below
- skipUserProfile: false,
- },
- (accessToken, refreshToken, profile, done) => {
- if (profile) {
- return done(null, profile);
- }
- return done(null, false);
- },
- ),
- );
- this.isGitHubStrategySetup = true;
- logger.debug('GitHubStrategy: setup is done');
- }
- /**
- * reset GitHubStrategy
- *
- * @memberof PassportService
- */
- resetGitHubStrategy() {
- logger.debug('GitHubStrategy: reset');
- passport.unuse('github');
- this.isGitHubStrategySetup = false;
- }
- setupTwitterStrategy() {
- this.resetTwitterStrategy();
- const { configManager } = this.crowi;
- const isTwitterEnabled = configManager.getConfig('crowi', 'security:passport-twitter:isEnabled');
- // when disabled
- if (!isTwitterEnabled) {
- return;
- }
- logger.debug('TwitterStrategy: setting up..');
- passport.use(
- new TwitterStrategy(
- {
- consumerKey: configManager.getConfig('crowi', 'security:passport-twitter:consumerKey'),
- consumerSecret: configManager.getConfig('crowi', 'security:passport-twitter:consumerSecret'),
- callbackURL: (this.crowi.appService.getSiteUrl() != null)
- ? urljoin(this.crowi.appService.getSiteUrl(), '/passport/twitter/callback') // auto-generated with v3.2.4 and above
- : configManager.getConfig('crowi', 'security:passport-twitter:callbackUrl'), // DEPRECATED: backward compatible with v3.2.3 and below
- skipUserProfile: false,
- },
- (accessToken, refreshToken, profile, done) => {
- if (profile) {
- return done(null, profile);
- }
- return done(null, false);
- },
- ),
- );
- this.isTwitterStrategySetup = true;
- logger.debug('TwitterStrategy: setup is done');
- }
- /**
- * reset TwitterStrategy
- *
- * @memberof PassportService
- */
- resetTwitterStrategy() {
- logger.debug('TwitterStrategy: reset');
- passport.unuse('twitter');
- this.isTwitterStrategySetup = false;
- }
- async setupOidcStrategy() {
- this.resetOidcStrategy();
- const { configManager } = this.crowi;
- const isOidcEnabled = configManager.getConfig('crowi', 'security:passport-oidc:isEnabled');
- // when disabled
- if (!isOidcEnabled) {
- return;
- }
- logger.debug('OidcStrategy: setting up..');
- // setup client
- // extend oidc request timeouts
- OIDCIssuer.defaultHttpOptions = { timeout: 5000 };
- const issuerHost = configManager.getConfig('crowi', 'security:passport-oidc:issuerHost');
- const clientId = configManager.getConfig('crowi', 'security:passport-oidc:clientId');
- const clientSecret = configManager.getConfig('crowi', 'security:passport-oidc:clientSecret');
- const redirectUri = (configManager.getConfig('crowi', 'app:siteUrl') != null)
- ? urljoin(this.crowi.appService.getSiteUrl(), '/passport/oidc/callback')
- : configManager.getConfig('crowi', 'security:passport-oidc:callbackUrl'); // DEPRECATED: backward compatible with v3.2.3 and below
- // Prevent request timeout error on app init
- const oidcIssuer = await this.getOIDCIssuerInstace(issuerHost);
- if (oidcIssuer != null) {
- logger.debug('Discovered issuer %s %O', oidcIssuer.issuer, oidcIssuer.metadata);
- const authorizationEndpoint = configManager.getConfig('crowi', 'security:passport-oidc:authorizationEndpoint');
- if (authorizationEndpoint) {
- oidcIssuer.metadata.authorization_endpoint = authorizationEndpoint;
- }
- const tokenEndpoint = configManager.getConfig('crowi', 'security:passport-oidc:tokenEndpoint');
- if (tokenEndpoint) {
- oidcIssuer.metadata.token_endpoint = tokenEndpoint;
- }
- const revocationEndpoint = configManager.getConfig('crowi', 'security:passport-oidc:revocationEndpoint');
- if (revocationEndpoint) {
- oidcIssuer.metadata.revocation_endpoint = revocationEndpoint;
- }
- const introspectionEndpoint = configManager.getConfig('crowi', 'security:passport-oidc:introspectionEndpoint');
- if (introspectionEndpoint) {
- oidcIssuer.metadata.introspection_endpoint = introspectionEndpoint;
- }
- const userInfoEndpoint = configManager.getConfig('crowi', 'security:passport-oidc:userInfoEndpoint');
- if (userInfoEndpoint) {
- oidcIssuer.metadata.userinfo_endpoint = userInfoEndpoint;
- }
- const endSessionEndpoint = configManager.getConfig('crowi', 'security:passport-oidc:endSessionEndpoint');
- if (endSessionEndpoint) {
- oidcIssuer.metadata.end_session_endpoint = endSessionEndpoint;
- }
- const registrationEndpoint = configManager.getConfig('crowi', 'security:passport-oidc:registrationEndpoint');
- if (registrationEndpoint) {
- oidcIssuer.metadata.registration_endpoint = registrationEndpoint;
- }
- const jwksUri = configManager.getConfig('crowi', 'security:passport-oidc:jwksUri');
- if (jwksUri) {
- oidcIssuer.metadata.jwks_uri = jwksUri;
- }
- logger.debug('Configured issuer %s %O', oidcIssuer.issuer, oidcIssuer.metadata);
- const client = new oidcIssuer.Client({
- client_id: clientId,
- client_secret: clientSecret,
- redirect_uris: [redirectUri],
- response_types: ['code'],
- });
- // prevent error AssertionError [ERR_ASSERTION]: id_token issued in the future
- // Doc: https://github.com/panva/node-openid-client/tree/v2.x#allow-for-system-clock-skew
- const OIDC_CLIENT_CLOCK_TOLERANCE = await this.crowi.configManager.getConfig('crowi', 'security:passport-oidc:oidcClientClockTolerance');
- client.CLOCK_TOLERANCE = OIDC_CLIENT_CLOCK_TOLERANCE;
- passport.use('oidc', new OidcStrategy(
- {
- client,
- params: { scope: 'openid email profile' },
- },
- (tokenset, userinfo, done) => {
- if (userinfo) {
- return done(null, userinfo);
- }
- return done(null, false);
- },
- ));
- this.isOidcStrategySetup = true;
- logger.debug('OidcStrategy: setup is done');
- }
- }
- /**
- * reset OidcStrategy
- *
- * @memberof PassportService
- */
- resetOidcStrategy() {
- logger.debug('OidcStrategy: reset');
- passport.unuse('oidc');
- this.isOidcStrategySetup = false;
- }
- /**
- *
- * Check and initialize connection to OIDC issuer host
- * Prevent request timeout error on app init
- *
- * @param issuerHost
- * @returns boolean
- */
- async isOidcHostReachable(issuerHost) {
- try {
- const response = await got(issuerHost, { retry: { limit: 3 } });
- return response.statusCode === 200;
- }
- catch (err) {
- logger.error('OidcStrategy: issuer host unreachable:', err.code);
- }
- }
- /**
- * Get oidcIssuer object
- * Utilize p-retry package to retry oidcIssuer initialization 3 times
- *
- * @param issuerHost
- * @returns instance of OIDCIssuer
- */
- async getOIDCIssuerInstace(issuerHost) {
- const OIDC_TIMEOUT_MULTIPLIER = await this.crowi.configManager.getConfig('crowi', 'security:passport-oidc:timeoutMultiplier');
- const OIDC_DISCOVERY_RETRIES = await this.crowi.configManager.getConfig('crowi', 'security:passport-oidc:discoveryRetries');
- const oidcIssuerHostReady = await this.isOidcHostReachable(issuerHost);
- if (!oidcIssuerHostReady) {
- logger.error('OidcStrategy: setup failed: OIDC Issur host unreachable');
- return;
- }
- const oidcIssuer = await pRetry(async() => {
- return OIDCIssuer.discover(issuerHost);
- }, {
- onFailedAttempt: (error) => {
- // get current OIDCIssuer.defaultHttpOptions.timeout
- const oidcOptionTimeout = OIDCIssuer.defaultHttpOptions.timeout;
- // Increases OIDCIssuer.defaultHttpOptions.timeout by multiply with 1.5
- OIDCIssuer.defaultHttpOptions = { timeout: oidcOptionTimeout * OIDC_TIMEOUT_MULTIPLIER };
- logger.debug(`OidcStrategy: setup attempt ${error.attemptNumber} failed with error: ${error}. Retrying ...`);
- },
- retries: OIDC_DISCOVERY_RETRIES,
- }).catch((error) => {
- logger.error(`OidcStrategy: setup failed with error: ${error} `);
- });
- return oidcIssuer;
- }
- setupSamlStrategy() {
- this.resetSamlStrategy();
- const { configManager } = this.crowi;
- const isSamlEnabled = configManager.getConfig('crowi', 'security:passport-saml:isEnabled');
- // when disabled
- if (!isSamlEnabled) {
- return;
- }
- logger.debug('SamlStrategy: setting up..');
- passport.use(
- new SamlStrategy(
- {
- entryPoint: configManager.getConfig('crowi', 'security:passport-saml:entryPoint'),
- callbackUrl: (this.crowi.appService.getSiteUrl() != null)
- ? urljoin(this.crowi.appService.getSiteUrl(), '/passport/saml/callback') // auto-generated with v3.2.4 and above
- : configManager.getConfig('crowi', 'security:passport-saml:callbackUrl'), // DEPRECATED: backward compatible with v3.2.3 and below
- issuer: configManager.getConfig('crowi', 'security:passport-saml:issuer'),
- cert: configManager.getConfig('crowi', 'security:passport-saml:cert'),
- },
- (profile: Profile, done: VerifiedCallback) => {
- if (profile) {
- return done(null, profile);
- }
- return done(null);
- },
- ),
- );
- this.isSamlStrategySetup = true;
- logger.debug('SamlStrategy: setup is done');
- }
- /**
- * reset SamlStrategy
- *
- * @memberof PassportService
- */
- resetSamlStrategy() {
- logger.debug('SamlStrategy: reset');
- passport.unuse('saml');
- this.isSamlStrategySetup = false;
- }
- /**
- * return the keys of the configs mandatory for SAML whose value are empty.
- */
- getSamlMissingMandatoryConfigKeys() {
- const missingRequireds: string[] = [];
- for (const key of this.mandatoryConfigKeysForSaml) {
- if (this.crowi.configManager.getConfig('crowi', key) === null) {
- missingRequireds.push(key);
- }
- }
- return missingRequireds;
- }
- /**
- * Parse Attribute-Based Login Control Rule as Lucene Query
- * @param {string} rule Lucene syntax string
- * @returns {object} Expression Tree Structure generated by lucene-query-parser
- * @see https://github.com/thoward/lucene-query-parser.js/wiki
- */
- parseABLCRule(rule) {
- // parse with lucene-query-parser
- // see https://github.com/thoward/lucene-query-parser.js/wiki
- return luceneQueryParser.parse(rule);
- }
- /**
- * Verify that a SAML response meets the attribute-base login control rule
- */
- verifySAMLResponseByABLCRule(response) {
- const rule = this.crowi.configManager.getConfig('crowi', 'security:passport-saml:ABLCRule');
- if (rule == null) {
- logger.debug('There is no ABLCRule.');
- return true;
- }
- const luceneRule = this.parseABLCRule(rule);
- logger.debug({ 'Parsed Rule': JSON.stringify(luceneRule, null, 2) });
- const attributes = this.extractAttributesFromSAMLResponse(response);
- logger.debug({ 'Extracted Attributes': JSON.stringify(attributes, null, 2) });
- return this.evaluateRuleForSamlAttributes(attributes, luceneRule);
- }
- /**
- * Evaluate whether the specified rule is satisfied under the specified attributes
- *
- * @param {object} attributes results by extractAttributesFromSAMLResponse
- * @param {object} luceneRule Expression Tree Structure generated by lucene-query-parser
- * @see https://github.com/thoward/lucene-query-parser.js/wiki
- */
- evaluateRuleForSamlAttributes(attributes, luceneRule) {
- const { left, right, operator } = luceneRule;
- // when combined rules
- if (right != null) {
- return this.evaluateCombinedRulesForSamlAttributes(attributes, left, right, operator);
- }
- if (left != null) {
- return this.evaluateRuleForSamlAttributes(attributes, left);
- }
- const { field, term } = luceneRule;
- const unescapedField = this.literalUnescape(field);
- if (unescapedField === '<implicit>') {
- return attributes[term] != null;
- }
- if (attributes[unescapedField] == null) {
- return false;
- }
- return attributes[unescapedField].includes(term);
- }
- /**
- * Evaluate whether the specified two rules are satisfied under the specified attributes
- *
- * @param {object} attributes results by extractAttributesFromSAMLResponse
- * @param {object} luceneRuleLeft Expression Tree Structure generated by lucene-query-parser
- * @param {object} luceneRuleRight Expression Tree Structure generated by lucene-query-parser
- * @param {string} luceneOperator operator string expression
- * @see https://github.com/thoward/lucene-query-parser.js/wiki
- */
- evaluateCombinedRulesForSamlAttributes(attributes, luceneRuleLeft, luceneRuleRight, luceneOperator) {
- if (luceneOperator === 'OR') {
- return this.evaluateRuleForSamlAttributes(attributes, luceneRuleLeft) || this.evaluateRuleForSamlAttributes(attributes, luceneRuleRight);
- }
- if (luceneOperator === 'AND') {
- return this.evaluateRuleForSamlAttributes(attributes, luceneRuleLeft) && this.evaluateRuleForSamlAttributes(attributes, luceneRuleRight);
- }
- if (luceneOperator === 'NOT') {
- return this.evaluateRuleForSamlAttributes(attributes, luceneRuleLeft) && !this.evaluateRuleForSamlAttributes(attributes, luceneRuleRight);
- }
- throw new Error(`Unsupported operator: ${luceneOperator}`);
- }
- /**
- * Extract attributes from a SAML response
- *
- * The format of extracted attributes is the following.
- *
- * {
- * "attribute_name1": ["value1", "value2", ...],
- * "attribute_name2": ["value1", "value2", ...],
- * ...
- * }
- */
- extractAttributesFromSAMLResponse(response) {
- const attributeStatement = response.getAssertion().Assertion.AttributeStatement;
- if (attributeStatement == null || attributeStatement[0] == null) {
- return {};
- }
- const attributes = attributeStatement[0].Attribute;
- if (attributes == null) {
- return {};
- }
- const result = {};
- for (const attribute of attributes) {
- const name = attribute.$.Name;
- const attributeValues = attribute.AttributeValue.map(v => v._);
- if (result[name] == null) {
- result[name] = attributeValues;
- }
- else {
- result[name] = result[name].concat(attributeValues);
- }
- }
- return result;
- }
- /**
- * reset BasicStrategy
- *
- * @memberof PassportService
- */
- resetBasicStrategy() {
- logger.debug('BasicStrategy: reset');
- passport.unuse('basic');
- this.isBasicStrategySetup = false;
- }
- /**
- * setup BasicStrategy
- *
- * @memberof PassportService
- */
- setupBasicStrategy() {
- this.resetBasicStrategy();
- const configManager = this.crowi.configManager;
- const isBasicEnabled = configManager.getConfig('crowi', 'security:passport-basic:isEnabled');
- // when disabled
- if (!isBasicEnabled) {
- return;
- }
- logger.debug('BasicStrategy: setting up..');
- passport.use(new BasicStrategy(
- (userId, password, done) => {
- if (userId != null) {
- return done(null, userId);
- }
- return done(null, false, { message: 'Incorrect credentials.' });
- },
- ));
- this.isBasicStrategySetup = true;
- logger.debug('BasicStrategy: setup is done');
- }
- /**
- * setup serializer and deserializer
- *
- * @memberof PassportService
- */
- setupSerializer() {
- // check whether the serializer/deserializer have already been set up
- if (this.isSerializerSetup) {
- throw new Error('serializer/deserializer have already been set up');
- }
- logger.debug('setting up serializer and deserializer');
- const User = this.crowi.model('User');
- passport.serializeUser((user, done) => {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- done(null, (user as any).id);
- });
- passport.deserializeUser(async(id, done) => {
- try {
- const user = await User.findById(id);
- if (user == null) {
- throw new Error('user not found');
- }
- if (user.imageUrlCached == null) {
- await user.updateImageUrlCached();
- await user.save();
- }
- done(null, user);
- }
- catch (err) {
- done(err);
- }
- });
- this.isSerializerSetup = true;
- }
- isSameUsernameTreatedAsIdenticalUser(providerType) {
- const key = `security:passport-${providerType}:isSameUsernameTreatedAsIdenticalUser`;
- return this.crowi.configManager.getConfig('crowi', key);
- }
- isSameEmailTreatedAsIdenticalUser(providerType) {
- const key = `security:passport-${providerType}:isSameEmailTreatedAsIdenticalUser`;
- return this.crowi.configManager.getConfig('crowi', key);
- }
- literalUnescape(string: string) {
- return string
- .replace(/\\\\/g, '\\')
- .replace(/\\\//g, '/')
- .replace(/\\:/g, ':')
- .replace(/\\"/g, '"')
- .replace(/\\0/g, '\0')
- .replace(/\\t/g, '\t')
- .replace(/\\n/g, '\n')
- .replace(/\\r/g, '\r');
- }
- }
- module.exports = PassportService;
|