@@ -7,6 +7,8 @@ const winston = module.parent.require('winston');
77const db = require . main . require ( './src/database' ) ;
88const user = require . main . require ( './src/user' ) ;
99const plugins = require . main . require ( './src/plugins' ) ;
10+ const meta = require . main . require ( './src/meta' ) ;
11+ const groups = require . main . require ( './src/groups' ) ;
1012const authenticationController = require . main . require ( './src/controllers/authentication' ) ;
1113const routeHelpers = require . main . require ( './src/routes/helpers' ) ;
1214
@@ -101,6 +103,7 @@ OAuth.loadStrategies = async (strategies) => {
101103
102104 winston . verbose ( `[plugin/sso-oauth2-multiple] Successful login to uid ${ user . uid } via ${ name } (remote id ${ id } )` ) ;
103105 authenticationController . onSuccessfulLogin ( req , user . uid ) ;
106+ OAuth . assignGroups ( { provider : name , user, profile } ) ;
104107 done ( null , user ) ;
105108
106109 plugins . hooks . fire ( 'action:oauth2.login' , { name, user, profile } ) ;
@@ -155,14 +158,15 @@ OAuth.getUserProfile = function (name, userRoute, accessToken, done) {
155158OAuth . parseUserReturn = async ( provider , profile ) => {
156159 const {
157160 id, sub, name, nickname, preferred_username, picture,
158- email, /* , email_verified */
161+ roles , email, /* , email_verified */
159162 } = profile ;
160163 const { usernameViaEmail, idKey } = await OAuth . getStrategy ( provider ) ;
161164 const normalized = {
162165 provider,
163166 id : profile [ idKey ] || id || sub ,
164167 displayName : nickname || preferred_username || name ,
165168 picture,
169+ roles,
166170 email,
167171 } ;
168172
@@ -173,6 +177,19 @@ OAuth.parseUserReturn = async (provider, profile) => {
173177 return normalized ;
174178} ;
175179
180+ OAuth . getAssociations = async ( ) => {
181+ let { roles, groups } = await meta . settings . get ( 'sso-oauth2-multiple' ) ;
182+ if ( ! roles || ! groups ) {
183+ return [ ] ;
184+ }
185+
186+ groups = groups . split ( ',' ) ;
187+ return roles . split ( ',' ) . map ( ( role , idx ) => ( {
188+ role,
189+ group : groups [ idx ] ,
190+ } ) ) ;
191+ } ;
192+
176193OAuth . login = async ( payload ) => {
177194 let uid = await OAuth . getUidByOAuthid ( payload . name , payload . oAuthid ) ;
178195 if ( uid !== null ) {
@@ -202,6 +219,30 @@ OAuth.login = async (payload) => {
202219 return { uid } ;
203220} ;
204221
222+ OAuth . assignGroups = async ( { user, profile } ) => {
223+ if ( ! profile . roles || ! Array . isArray ( profile . roles ) ) {
224+ return ;
225+ }
226+
227+ const { uid } = user ;
228+ const associations = await OAuth . getAssociations ( ) ;
229+ const { toJoin, toLeave } = associations . reduce ( ( memo , { role, group } ) => {
230+ if ( profile . roles . includes ( role ) ) {
231+ memo . toJoin . push ( group ) ;
232+ } else {
233+ memo . toLeave . push ( group ) ;
234+ }
235+
236+ return memo ;
237+ } , { toJoin : [ ] , toLeave : [ ] } ) ;
238+ if ( toLeave . length ) {
239+ winston . verbose ( `[plugins/sso-auth0] uid ${ uid } now leaving ${ toLeave . length } these user groups: ${ toLeave . join ( ', ' ) } ` ) ;
240+ }
241+ await groups . leave ( toLeave , uid ) ;
242+ await groups . join ( toJoin , uid ) ;
243+ winston . verbose ( `[plugins/sso-auth0] uid ${ uid } now a part of ${ toJoin . length } these user groups: ${ toJoin . join ( ', ' ) } ` ) ;
244+ } ;
245+
205246OAuth . getUidByOAuthid = async ( name , oAuthid ) => db . getObjectField ( `${ name } Id:uid` , oAuthid ) ;
206247
207248OAuth . deleteUserData = async ( data ) => {
0 commit comments