1- const configure = ( ) => {
2- const passport = require ( 'passport' ) ;
3- const ActiveDirectoryStrategy = require ( 'passport-activedirectory' ) ;
4- const config = require ( '../../config' ) . getAuthentication ( ) ;
5- const adConfig = config . adConfig ;
1+ const ActiveDirectoryStrategy = require ( 'passport-activedirectory' ) ;
2+ const ldaphelper = require ( './ldaphelper' ) ;
3+
4+ const configure = ( passport ) => {
65 const db = require ( '../../db' ) ;
7- const userGroup = config . userGroup ;
8- const adminGroup = config . adminGroup ;
9- const domain = config . domain ;
6+
7+ // We can refactor this by normalizing auth strategy config and pass it directly into the configure() function,
8+ // ideally when we convert this to TS.
9+ const authMethods = require ( '../../config' ) . getAuthMethods ( ) ;
10+ const config = authMethods . find ( ( method ) => method . type . toLowerCase ( ) === "activeDirectory" ) ;
11+ const adConfig = config . adConfig ;
12+
13+ const { userGroup, adminGroup, domain } = config ;
1014
1115 console . log ( `AD User Group: ${ userGroup } , AD Admin Group: ${ adminGroup } ` ) ;
1216
13- const ldaphelper = require ( './ldaphelper' ) ;
1417 passport . use (
1518 new ActiveDirectoryStrategy (
1619 {
@@ -19,42 +22,47 @@ const configure = () => {
1922 ldap : adConfig ,
2023 } ,
2124 async function ( req , profile , ad , done ) {
22- profile . username = profile . _json . sAMAccountName . toLowerCase ( ) ;
23- profile . email = profile . _json . mail ;
24- profile . id = profile . username ;
25- req . user = profile ;
26-
27- console . log (
28- `passport.activeDirectory: resolved login ${
29- profile . _json . userPrincipalName
30- } , profile=${ JSON . stringify ( profile ) } `,
31- ) ;
32- // First check to see if the user is in the usergroups
33- const isUser = await ldaphelper . isUserInAdGroup ( profile . username , domain , userGroup ) ;
34-
35- if ( ! isUser ) {
36- const message = `User it not a member of ${ userGroup } ` ;
37- return done ( message , null ) ;
38- }
25+ try {
26+ profile . username = profile . _json . sAMAccountName ?. toLowerCase ( ) ;
27+ profile . email = profile . _json . mail ;
28+ profile . id = profile . username ;
29+ req . user = profile ;
3930
40- // Now check if the user is an admin
41- const isAdmin = await ldaphelper . isUserInAdGroup ( profile . username , domain , adminGroup ) ;
31+ console . log (
32+ `passport.activeDirectory: resolved login ${
33+ profile . _json . userPrincipalName
34+ } , profile=${ JSON . stringify ( profile ) } `,
35+ ) ;
36+ // First check to see if the user is in the usergroups
37+ const isUser = await ldaphelper . isUserInAdGroup ( profile . username , domain , userGroup ) ;
4238
43- profile . admin = isAdmin ;
44- console . log ( `passport.activeDirectory: ${ profile . username } admin=${ isAdmin } ` ) ;
39+ if ( ! isUser ) {
40+ const message = `User it not a member of ${ userGroup } ` ;
41+ return done ( message , null ) ;
42+ }
4543
46- const user = {
47- username : profile . username ,
48- admin : isAdmin ,
49- email : profile . _json . mail ,
50- displayName : profile . displayName ,
51- title : profile . _json . title ,
52- } ;
44+ // Now check if the user is an admin
45+ const isAdmin = await ldaphelper . isUserInAdGroup ( profile . username , domain , adminGroup ) ;
5346
54- await db . updateUser ( user ) ;
47+ profile . admin = isAdmin ;
48+ console . log ( `passport.activeDirectory: ${ profile . username } admin=${ isAdmin } ` ) ;
5549
56- return done ( null , user ) ;
57- } ,
50+ const user = {
51+ username : profile . username ,
52+ admin : isAdmin ,
53+ email : profile . _json . mail ,
54+ displayName : profile . displayName ,
55+ title : profile . _json . title ,
56+ } ;
57+
58+ await db . updateUser ( user ) ;
59+
60+ return done ( null , user ) ;
61+ } catch ( err ) {
62+ console . log ( `Error authenticating AD user: ${ err . message } ` ) ;
63+ return done ( err , null ) ;
64+ }
65+ }
5866 ) ,
5967 ) ;
6068
@@ -69,4 +77,4 @@ const configure = () => {
6977 return passport ;
7078} ;
7179
72- module . exports . configure = configure ;
80+ module . exports = { configure } ;
0 commit comments