11const _ = require ( 'lodash' )
22const { verifyJwt } = require ( '../../../helpers/jwt' )
3-
43/* global WIKI */
54
65// ------------------------------------
@@ -10,71 +9,81 @@ const { verifyJwt } = require('../../../helpers/jwt')
109const OpenIDConnectStrategy = require ( 'passport-openidconnect' ) . Strategy
1110
1211module . exports = {
13- async init ( passport , conf ) {
12+ async init ( passport , conf ) {
1413 try {
15- const response = await fetch ( conf . wellKnownURL )
16- if ( ! response . ok ) throw new Error ( `Failed to fetch well-known config: ${ response . statusText } ` )
17- const wellKnown = await response . json ( )
18-
19- passport . use ( conf . key ,
20- new OpenIDConnectStrategy ( {
21- issuer : wellKnown . issuer ,
22- authorizationURL : wellKnown . authorization_endpoint ,
23- tokenURL : wellKnown . token_endpoint ,
24- userInfoURL : wellKnown . userinfo_endpoint ,
25- clientID : conf . clientId ,
26- clientSecret : conf . clientSecret ,
27- callbackURL : conf . callbackURL ,
28- scope : conf . scope ,
29- passReqToCallback : true ,
30- skipUserProfile : conf . skipUserProfile ,
31- acrValues : conf . acrValues
32- } , async ( req , iss , uiProfile , idProfile , context , idToken , accessToken , refreshToken , params , cb ) => {
33- let idTokenClaims = { }
34- if ( conf . mergeIdTokenClaims && idToken ) {
35- idTokenClaims = await verifyJwt ( idToken , {
36- issuer : wellKnown . issuer ,
37- clientId : conf . clientId ,
38- jwksUri : wellKnown . jwks_uri ,
39- algorithms : wellKnown . id_token_signing_alg_values_supported
40- } )
41- }
42- // Merge claims from ID token and profile, with idProfile taking precedence
43- const profile = { ...idTokenClaims , ...idProfile }
44- try {
45- const user = await WIKI . models . users . processProfile ( {
46- providerKey : req . params . strategy ,
47- profile : {
48- ...profile ,
49- id : _ . get ( profile , conf . userIdClaim ) ,
50- displayName : _ . get ( profile , conf . displayNameClaim , '???' ) ,
51- email : _ . get ( profile , conf . emailClaim ) ,
14+ let oidcConfig = {
15+ issuer : conf . issuer ,
16+ authorizationURL : conf . authorizationURL ,
17+ tokenURL : conf . tokenURL ,
18+ userInfoURL : conf . userInfoURL ,
19+ clientID : conf . clientId ,
20+ clientSecret : conf . clientSecret ,
21+ callbackURL : conf . callbackURL ,
22+ scope : conf . scope ,
23+ passReqToCallback : true ,
24+ skipUserProfile : conf . skipUserProfile ,
25+ acrValues : conf . acrValues
26+ }
27+ if ( conf . wellKnownURL ) {
28+ try {
29+ const response = await fetch ( conf . wellKnownURL )
30+ if ( ! response . ok ) throw new Error ( response . statusText )
31+ const wellKnown = await response . json ( )
32+ if ( ! oidcConfig . issuer ) oidcConfig . issuer = wellKnown . issuer
33+ if ( ! oidcConfig . authorizationURL ) oidcConfig . authorizationURL = wellKnown . authorization_endpoint
34+ if ( ! oidcConfig . tokenURL ) oidcConfig . tokenURL = wellKnown . token_endpoint
35+ if ( ! oidcConfig . userInfoURL ) oidcConfig . userInfoURL = wellKnown . userinfo_endpoint
36+ oidcConfig . jwksUri = wellKnown . jwks_uri
37+ oidcConfig . idTokenSigningAlgValuesSupported = wellKnown . id_token_signing_alg_values_supported
38+ } catch ( error ) {
39+ WIKI . logger . error ( 'Error fetching OIDC well-known configuration:' , error )
40+ }
41+ }
42+ passport . use ( conf . key , new OpenIDConnectStrategy ( oidcConfig , async ( req , iss , uiProfile , idProfile , context , idToken , accessToken , refreshToken , params , cb ) => {
43+ let idTokenClaims = { }
44+ if ( conf . mergeIdTokenClaims && idToken ) {
45+ idTokenClaims = await verifyJwt ( idToken , {
46+ issuer : oidcConfig . issuer ,
47+ clientId : oidcConfig . clientID ,
48+ jwksUri : oidcConfig . jwksUri ,
49+ algorithms : oidcConfig . idTokenSigningAlgValuesSupported
50+ } )
51+ }
52+ // Merge claims from ID token and profile, with idProfile taking precedence
53+ const profile = { ...idTokenClaims , ...idProfile }
54+ try {
55+ const user = await WIKI . models . users . processProfile ( {
56+ providerKey : req . params . strategy ,
57+ profile : {
58+ ...profile ,
59+ id : _ . get ( profile , conf . userIdClaim ) ,
60+ displayName : _ . get ( profile , conf . displayNameClaim , 'Unknown User' ) ,
61+ email : _ . get ( profile , conf . emailClaim )
62+ }
63+ } )
64+ if ( conf . mapGroups ) {
65+ const groups = _ . get ( profile , conf . groupsClaim )
66+ if ( groups && _ . isArray ( groups ) ) {
67+ const currentGroups = ( await user . $relatedQuery ( 'groups' ) . select ( 'groups.id' ) ) . map ( g => g . id )
68+ const expectedGroups = Object . values ( WIKI . auth . groups ) . filter ( g => groups . includes ( g . name ) ) . map ( g => g . id )
69+ for ( const groupId of _ . difference ( expectedGroups , currentGroups ) ) {
70+ await user . $relatedQuery ( 'groups' ) . relate ( groupId )
5271 }
53- } )
54- if ( conf . mapGroups ) {
55- const groups = _ . get ( profile , conf . groupsClaim )
56- if ( groups && _ . isArray ( groups ) ) {
57- const currentGroups = ( await user . $relatedQuery ( 'groups' ) . select ( 'groups.id' ) ) . map ( g => g . id )
58- const expectedGroups = Object . values ( WIKI . auth . groups ) . filter ( g => groups . includes ( g . name ) ) . map ( g => g . id )
59- for ( const groupId of _ . difference ( expectedGroups , currentGroups ) ) {
60- await user . $relatedQuery ( 'groups' ) . relate ( groupId )
61- }
62- for ( const groupId of _ . difference ( currentGroups , expectedGroups ) ) {
63- await user . $relatedQuery ( 'groups' ) . unrelate ( ) . where ( 'groupId' , groupId )
64- }
72+ for ( const groupId of _ . difference ( currentGroups , expectedGroups ) ) {
73+ await user . $relatedQuery ( 'groups' ) . unrelate ( ) . where ( 'groupId' , groupId )
6574 }
6675 }
67- cb ( null , user )
68- } catch ( err ) {
69- cb ( err , null )
7076 }
71- } )
72- )
73- } catch ( error ) {
74- console . error ( 'Error initializing OpenID Connect strategy:' , error )
77+ cb ( null , user )
78+ } catch ( err ) {
79+ cb ( err , null )
80+ }
81+ } ) )
82+ } catch ( err ) {
83+ WIKI . logger . error ( `Error initializing OpenID Connect strategy: ${ err } ` )
7584 }
7685 } ,
77- logout ( conf ) {
86+ logout ( conf ) {
7887 return conf . logoutURL || '/'
7988 }
8089}
0 commit comments