@@ -118,6 +118,104 @@ class OAuthClientInformationFull(OAuthClientMetadata):
118118auth_manager_config .OAUTH_UPDATE_PICTURE_ON_LOGIN = OAUTH_UPDATE_PICTURE_ON_LOGIN
119119
120120
121+ # PATCH OIDC
122+ def set_aak_groups (user_data : UserInfo ) -> UserInfo :
123+ """
124+ Set AAK groups based on AAK claims. AAK groups need to be parsed from a collection of AAK claims,
125+ so we cannot rely on Open WebUI's claims mapping. Parses the relevant AAK claims and adds them
126+ to the "groups" list. This enables us to rely on Open WebUI's role management for user role assignment.
127+
128+ To ensure unique group names, they are constructed as "<aak_department_name> (<aak_department_id>)".
129+
130+ Example claims:
131+
132+ "companyname": [
133+ "Aarhus Kommune"
134+ ],
135+ "division": [
136+ "Kultur og Borgerservice"
137+ ],
138+ "department": [
139+ "Borgerservice og Biblioteker"
140+ ],
141+ "extensionAttribute12": [
142+ "ITK"
143+ ],
144+ "Office": [
145+ "ITK Development"
146+ ],
147+ "extensionAttribute7": [
148+ "1001;1004;1012;1103;6530"
149+ ]
150+
151+ The ID's for the departments are given sequentially in "extensionAttribute7". Users in management postitions will
152+ not have five levels of AAK groups. This will show in the length of "extensionAttribute7" but will not show in the
153+ other claims. In the above example a manager will still have the "Office" claim, but it will repeat the value from
154+ "extensionAttribute12" and "extensionAttribute7 will only contain "1001;1004;1012;1103"
155+
156+ Note: ENABLE_OAUTH_GROUP_MANAGEMENT and ENABLE_OAUTH_GROUP_CREATION must be set to 'true'
157+
158+ Args:
159+ user_data (dict): The decoded OIDC token
160+
161+ Returns:
162+ The decoded OIDC token with the AAK group names added to the "groups" list.
163+ """
164+
165+ log .debug ("Running AAK Group management" )
166+ log .debug (user_data )
167+
168+ user_data ['groups' ] = []
169+
170+ dept_ids = user_data .get ("extensionAttribute7" , "" ).split (";" )
171+ dept_depth = len (dept_ids )
172+
173+ if "companyname" in user_data and dept_depth >= 1 :
174+ user_data ['groups' ].append (user_data .get ("companyname" , "" ) + " (" + dept_ids [0 ] + ")" )
175+ if "division" in user_data and dept_depth >= 2 :
176+ user_data ['groups' ].append (user_data .get ("division" , "" ) + " (" + dept_ids [1 ] + ")" )
177+ if "department" in user_data and dept_depth >= 3 :
178+ user_data ['groups' ].append (user_data .get ("department" , "" ) + " (" + dept_ids [2 ] + ")" )
179+ if "extensionAttribute12" in user_data and dept_depth >= 4 :
180+ user_data ['groups' ].append (user_data .get ("extensionAttribute12" , "" ) + " (" + dept_ids [3 ] + ")" )
181+ if "Office" in user_data and dept_depth >= 5 :
182+ user_data ['groups' ].append (user_data .get ("Office" , "" ) + " (" + dept_ids [4 ] + ")" )
183+
184+ log .debug (f"Using groups { user_data .get ('groups' , '' )} ." )
185+
186+ return user_data
187+
188+
189+ def set_aak_role (user_data : UserInfo ) -> UserInfo :
190+ """
191+ Set the AAK role based on AAK claims. For "builders" we cannot map to a native Open WebUI role.
192+ Instead, we add the role "Builder" to the list of groups.
193+
194+ Note: ENABLE_OAUTH_GROUP_MANAGEMENT and ENABLE_OAUTH_GROUP_CREATION must be set to 'true'
195+
196+ Args:
197+ user_data (dict): The decoded OIDC token
198+
199+ Returns:
200+ The decoded OIDC token with the AAK role added to the "groups" list.
201+ """
202+
203+ log .debug ("Running AAK Role management" )
204+ log .debug (user_data )
205+
206+ claims_roles = user_data .get ("role" , "" )
207+
208+ log .debug (f"Using aak_claims_role { claims_roles } ." )
209+
210+ if "builder" in claims_roles :
211+ user_data ['groups' ].append ("Builder" )
212+
213+ log .debug (f"Using role-groups { user_data .get ('groups' , '' )} ." )
214+
215+ return user_data
216+ # //PATCH OIDC
217+
218+
121219FERNET = None
122220
123221if len (OAUTH_CLIENT_INFO_ENCRYPTION_KEY ) != 44 :
@@ -1254,6 +1352,12 @@ async def handle_callback(self, request, provider, response):
12541352 log .warning (f"OAuth callback failed, user data is missing: { token } " )
12551353 raise HTTPException (400 , detail = ERROR_MESSAGES .INVALID_CRED )
12561354
1355+ # PATCH OIDC
1356+ # Set AAK role and groups
1357+ user_data = set_aak_groups (user_data = user_data )
1358+ user_data = set_aak_role (user_data = user_data )
1359+ # //PATCH OIDC
1360+
12571361 # Extract the "sub" claim, using custom claim if configured
12581362 if auth_manager_config .OAUTH_SUB_CLAIM :
12591363 sub = user_data .get (auth_manager_config .OAUTH_SUB_CLAIM )
0 commit comments