mirror of
https://github.com/plankanban/planka.git
synced 2025-07-19 13:19:44 +02:00
fix: Lazy initialize OIDC client (#947)
This commit is contained in:
parent
2109fea800
commit
480c280ab3
4 changed files with 58 additions and 23 deletions
|
@ -3,6 +3,9 @@ const { v4: uuid } = require('uuid');
|
||||||
const { getRemoteAddress } = require('../../../utils/remoteAddress');
|
const { getRemoteAddress } = require('../../../utils/remoteAddress');
|
||||||
|
|
||||||
const Errors = {
|
const Errors = {
|
||||||
|
INVALID_OIDC_CONFIGURATION: {
|
||||||
|
invalidOIDCConfiguration: 'Invalid OIDC configuration',
|
||||||
|
},
|
||||||
INVALID_CODE_OR_NONCE: {
|
INVALID_CODE_OR_NONCE: {
|
||||||
invalidCodeOrNonce: 'Invalid code or nonce',
|
invalidCodeOrNonce: 'Invalid code or nonce',
|
||||||
},
|
},
|
||||||
|
@ -37,6 +40,9 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
exits: {
|
exits: {
|
||||||
|
invalidOIDCConfiguration: {
|
||||||
|
responseType: 'serverError',
|
||||||
|
},
|
||||||
invalidCodeOrNonce: {
|
invalidCodeOrNonce: {
|
||||||
responseType: 'unauthorized',
|
responseType: 'unauthorized',
|
||||||
},
|
},
|
||||||
|
@ -63,6 +69,7 @@ module.exports = {
|
||||||
sails.log.warn(`Invalid code or nonce! (IP: ${remoteAddress})`);
|
sails.log.warn(`Invalid code or nonce! (IP: ${remoteAddress})`);
|
||||||
return Errors.INVALID_CODE_OR_NONCE;
|
return Errors.INVALID_CODE_OR_NONCE;
|
||||||
})
|
})
|
||||||
|
.intercept('invalidOIDCConfiguration', () => Errors.INVALID_OIDC_CONFIGURATION)
|
||||||
.intercept('invalidUserinfoConfiguration', () => Errors.INVALID_USERINFO_CONFIGURATION)
|
.intercept('invalidUserinfoConfiguration', () => Errors.INVALID_USERINFO_CONFIGURATION)
|
||||||
.intercept('emailAlreadyInUse', () => Errors.EMAIL_ALREADY_IN_USE)
|
.intercept('emailAlreadyInUse', () => Errors.EMAIL_ALREADY_IN_USE)
|
||||||
.intercept('usernameAlreadyInUse', () => Errors.USERNAME_ALREADY_IN_USE)
|
.intercept('usernameAlreadyInUse', () => Errors.USERNAME_ALREADY_IN_USE)
|
||||||
|
|
|
@ -1,8 +1,26 @@
|
||||||
|
const Errors = {
|
||||||
|
INVALID_OIDC_CONFIGURATION: {
|
||||||
|
invalidOidcConfiguration: 'Invalid OIDC configuration',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
fn() {
|
exits: {
|
||||||
|
invalidOidcConfiguration: {
|
||||||
|
responseType: 'serverError',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
async fn() {
|
||||||
let oidc = null;
|
let oidc = null;
|
||||||
if (sails.hooks.oidc.isActive()) {
|
if (sails.hooks.oidc.isActive()) {
|
||||||
const oidcClient = sails.hooks.oidc.getClient();
|
let oidcClient;
|
||||||
|
try {
|
||||||
|
oidcClient = await sails.hooks.oidc.getClient();
|
||||||
|
} catch (error) {
|
||||||
|
sails.log.warn(`Error while initializing OIDC client: ${error}`);
|
||||||
|
throw Errors.INVALID_OIDC_CONFIGURATION;
|
||||||
|
}
|
||||||
|
|
||||||
const authorizationUrlParams = {
|
const authorizationUrlParams = {
|
||||||
scope: sails.config.custom.oidcScopes,
|
scope: sails.config.custom.oidcScopes,
|
||||||
|
|
|
@ -11,6 +11,7 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
exits: {
|
exits: {
|
||||||
|
invalidOIDCConfiguration: {},
|
||||||
invalidCodeOrNonce: {},
|
invalidCodeOrNonce: {},
|
||||||
invalidUserinfoConfiguration: {},
|
invalidUserinfoConfiguration: {},
|
||||||
missingValues: {},
|
missingValues: {},
|
||||||
|
@ -19,7 +20,13 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
async fn(inputs) {
|
async fn(inputs) {
|
||||||
const client = sails.hooks.oidc.getClient();
|
let client;
|
||||||
|
try {
|
||||||
|
client = await sails.hooks.oidc.getClient();
|
||||||
|
} catch (error) {
|
||||||
|
sails.log.warn(`Error while initializing OIDC client: ${error}`);
|
||||||
|
throw 'invalidOIDCConfiguration';
|
||||||
|
}
|
||||||
|
|
||||||
let tokenSet;
|
let tokenSet;
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -15,13 +15,17 @@ module.exports = function defineOidcHook(sails) {
|
||||||
/**
|
/**
|
||||||
* Runs when this Sails app loads/lifts.
|
* Runs when this Sails app loads/lifts.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
async initialize() {
|
async initialize() {
|
||||||
if (!sails.config.custom.oidcIssuer) {
|
if (!this.isActive()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
sails.log.info('Initializing custom hook (`oidc`)');
|
sails.log.info('Initializing custom hook (`oidc`)');
|
||||||
|
},
|
||||||
|
|
||||||
|
async getClient() {
|
||||||
|
if (client === null && this.isActive()) {
|
||||||
|
sails.log.info('Initializing OIDC client');
|
||||||
|
|
||||||
const issuer = await openidClient.Issuer.discover(sails.config.custom.oidcIssuer);
|
const issuer = await openidClient.Issuer.discover(sails.config.custom.oidcIssuer);
|
||||||
|
|
||||||
|
@ -38,14 +42,13 @@ module.exports = function defineOidcHook(sails) {
|
||||||
}
|
}
|
||||||
|
|
||||||
client = new issuer.Client(metadata);
|
client = new issuer.Client(metadata);
|
||||||
},
|
}
|
||||||
|
|
||||||
getClient() {
|
|
||||||
return client;
|
return client;
|
||||||
},
|
},
|
||||||
|
|
||||||
isActive() {
|
isActive() {
|
||||||
return client !== null;
|
return sails.config.custom.oidcIssuer !== undefined;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue