@@ -93,7 +93,6 @@ public class ToOidcSynchronizer {
93
93
public static final String PKCE_TYPE_NONE = "none" ;
94
94
public static final String PKCE_TYPE_PLAIN = "plain code challenge" ;
95
95
public static final String PKCE_TYPE_SHA256 = "SHA256 code challenge" ;
96
-
97
96
private final PerunAdapter perunAdapter ;
98
97
private final String proxyIdentifier ;
99
98
private final String proxyIdentifierValue ;
@@ -152,7 +151,8 @@ public SyncResult syncToOidc(boolean interactiveMode) {
152
151
log .info ("Removing old clients" );
153
152
deleteClients (foundClientIds , res );
154
153
} else {
155
- log .warn ("Script has disabled removing of old clients. This might be due to Peruns unreachability! Check previous logs for more info." );
154
+ log .warn ("Script has disabled removing of old clients. " +
155
+ "This might be due to Peruns unreachability! Check previous logs for more info." );
156
156
}
157
157
return res ;
158
158
}
@@ -242,7 +242,9 @@ private void createClient(Map<String, PerunAttributeValue> attrs, SyncResult res
242
242
}
243
243
}
244
244
245
- private void updateClient (MitreidClient original , Map <String , PerunAttributeValue > attrs , SyncResult res )
245
+ private void updateClient (MitreidClient original ,
246
+ Map <String , PerunAttributeValue > attrs ,
247
+ SyncResult res )
246
248
throws BadPaddingException , InvalidKeyException , IllegalBlockSizeException
247
249
{
248
250
if (actionsProperties .getToOidc ().isUpdate ()) {
@@ -329,69 +331,82 @@ private void deleteClients(Set<String> foundClientIds, SyncResult res) {
329
331
}
330
332
}
331
333
} else {
332
- log .warn ("Deleting of clients is disabled. Following clientIDs would be deleted: {}" , clientsToDelete );
334
+ log .warn ("Deleting of clients is disabled. Following clientIDs would be deleted: {}" ,
335
+ clientsToDelete );
333
336
}
334
337
}
335
338
336
339
private void setClientFields (MitreidClient c , Map <String , PerunAttributeValue > attrs )
337
340
throws BadPaddingException , InvalidKeyException , IllegalBlockSizeException
338
341
{
339
- c .setClientId (attrs .get (perunAttrNames .getClientId ()).valueAsString ());
340
- c .setClientSecret (Utils .decrypt (
341
- attrs .get (perunAttrNames .getClientSecret ()).valueAsString (), cipher , secretKeySpec ));
342
- c .setClientName (attrs .get (perunAttrNames .getName ()).valueAsMap ().get ("en" ));
343
- c .setClientDescription (attrs .get (perunAttrNames .getDescription ()).valueAsMap ().get ("en" ));
344
- c .setRedirectUris (new HashSet <>(attrs .get (perunAttrNames .getRedirectUris ()).valueAsList ()));
345
- c .setAllowIntrospection (attrs .get (perunAttrNames .getIntrospection ()).valueAsBoolean ());
346
- c .setPostLogoutRedirectUris (new HashSet <>(attrs .get (perunAttrNames .getPostLogoutRedirectUris ()).valueAsList ()));
347
- c .setScope (new HashSet <>(attrs .get (perunAttrNames .getScopes ()).valueAsList ()));
348
- setPolicyUri (c , attrs );
349
- setContacts (c , attrs );
350
- setClientUri (c , attrs );
342
+ setClientId (c , attrs );
343
+ setClientSecret (c , attrs );
344
+ setClientName (c , attrs );
345
+ setClientDescription (c , attrs );
346
+ setRedirectUris (c , attrs );
347
+ setIntrospection (c , attrs );
348
+ setPostLogoutRedirectUris (c , attrs );
349
+ setScopes (c , attrs );
351
350
setGrantAndResponseTypes (c , attrs );
351
+ setPKCEOptions (c , attrs );
352
+ setTokenEndpointAuthentication (c , attrs );
352
353
setRefreshTokens (c , attrs );
353
354
setTokenTimeouts (c , attrs );
355
+ setPolicyUri (c , attrs );
356
+ setContacts (c , attrs );
357
+ setClientUri (c , attrs );
354
358
}
355
359
356
- private void setRefreshTokens (MitreidClient c , Map <String , PerunAttributeValue > attrs ) {
357
- Set <String > grantTypes = c .getGrantTypes ();
358
- if (grantTypes == null ) {
359
- grantTypes = new HashSet <>();
360
- }
361
- if (grantAllowsRefreshTokens (grantTypes )) {
362
- boolean requestedViaAttr = attrs .containsKey (perunAttrNames .getIssueRefreshTokens ())
363
- && attrs .get (perunAttrNames .getIssueRefreshTokens ()).valueAsBoolean ();
364
- boolean requestedViaScopes = c .getScope ().contains (OFFLINE_ACCESS );
365
- log .debug ("Refresh tokens requested via: attr({}), scopes({})" , requestedViaAttr , requestedViaScopes );
366
- if (requestedViaAttr || requestedViaScopes ) {
367
- setUpRefreshTokens (c , attrs );
368
- }
369
- }
360
+ private void setClientId (MitreidClient c , Map <String , PerunAttributeValue > attrs ) {
361
+ String clientId = attrs .get (perunAttrNames .getClientId ()).valueAsString ();
362
+ c .setClientId (clientId );
370
363
}
371
364
372
- private void setUpRefreshTokens (MitreidClient c , Map <String , PerunAttributeValue > attrs ) {
373
- c .getScope ().add (OFFLINE_ACCESS );
374
- c .getGrantTypes ().add (GRANT_REFRESH_TOKEN );
375
- c .setClearAccessTokensOnRefresh (true );
376
- c .setReuseRefreshToken (false );
377
- PerunAttributeValue reuseTokens = attrs .getOrDefault (perunAttrNames .getReuseRefreshTokens (), null );
378
- if (reuseTokens != null ) {
379
- c .setReuseRefreshToken (reuseTokens .valueAsBoolean ());
380
- }
365
+ private void setClientSecret (MitreidClient c , Map <String , PerunAttributeValue > attrs )
366
+ throws IllegalBlockSizeException , BadPaddingException , InvalidKeyException
367
+ {
368
+ String encryptedClientSecret = attrs .get (perunAttrNames .getClientSecret ()).valueAsString ();
369
+ String clientSecret = Utils .decrypt (encryptedClientSecret , cipher , secretKeySpec );
370
+ c .setClientSecret (clientSecret );
381
371
}
382
372
383
- private boolean grantAllowsRefreshTokens (Set <String > grantTypes ) {
384
- boolean res = !grantTypes .isEmpty ()
385
- && (grantTypes .contains (GRANT_DEVICE )
386
- || grantTypes .contains (GRANT_AUTHORIZATION_CODE )
387
- || grantTypes .contains (GRANT_HYBRID ));
388
- log .debug ("Grants '{}' {} issuing refresh tokens" , grantTypes , res ? "allow" : "disallow" );
389
- return res ;
373
+ private void setClientName (MitreidClient c , Map <String , PerunAttributeValue > attrs ) {
374
+ String clientName = attrs .get (perunAttrNames .getName ()).valueAsMap ().get ("en" );
375
+ c .setClientName (clientName );
376
+ }
377
+
378
+ private void setClientDescription (MitreidClient c , Map <String , PerunAttributeValue > attrs ) {
379
+ String clientDescription = attrs .get (perunAttrNames .getDescription ()).valueAsMap ().get ("en" );
380
+ c .setClientDescription (clientDescription );
381
+ }
382
+
383
+ private void setRedirectUris (MitreidClient c , Map <String , PerunAttributeValue > attrs ) {
384
+ Set <String > redirectUris = new HashSet <>(
385
+ attrs .get (perunAttrNames .getRedirectUris ()).valueAsList ());
386
+ c .setRedirectUris (redirectUris );
387
+ }
388
+
389
+ private void setIntrospection (MitreidClient c , Map <String , PerunAttributeValue > attrs ) {
390
+ boolean introspectionAllowed = attrs .get (perunAttrNames .getIntrospection ()).valueAsBoolean ();
391
+ c .setAllowIntrospection (introspectionAllowed );
392
+ }
393
+
394
+ private void setPostLogoutRedirectUris (MitreidClient c ,
395
+ Map <String , PerunAttributeValue > attrs )
396
+ {
397
+ Set <String > postLogoutRedirectUris = new HashSet <>(
398
+ attrs .get (perunAttrNames .getPostLogoutRedirectUris ()).valueAsList ());
399
+ c .setPostLogoutRedirectUris (postLogoutRedirectUris );
400
+ }
401
+
402
+ private void setScopes (MitreidClient c , Map <String , PerunAttributeValue > attrs ) {
403
+ Set <String > scopes = new HashSet <>(attrs .get (perunAttrNames .getScopes ()).valueAsList ());
404
+ c .setScope (scopes );
390
405
}
391
406
392
407
private void setGrantAndResponseTypes (MitreidClient c , Map <String , PerunAttributeValue > attrs ) {
393
408
List <String > grantTypesAttrValue = attrs .get (perunAttrNames .getGrantTypes ()).valueAsList ().stream ()
394
- .map (String ::toLowerCase ).collect (Collectors .toList ());
409
+ .map (String ::toLowerCase ).collect (Collectors .toList ());
395
410
396
411
Set <String > grantTypes = new HashSet <>();
397
412
Set <String > responseTypes = new HashSet <>();
@@ -413,20 +428,14 @@ private void setGrantAndResponseTypes(MitreidClient c, Map<String, PerunAttribut
413
428
grantTypes .add (GRANT_AUTHORIZATION_CODE );
414
429
responseTypes .addAll (Arrays .asList (RESPONSE_TYPE_HYBRID ));
415
430
log .debug ("Added grants '{} {}' with response types '{}'" , GRANT_HYBRID , GRANT_AUTHORIZATION_CODE ,
416
- RESPONSE_TYPE_HYBRID );
431
+ RESPONSE_TYPE_HYBRID );
417
432
}
418
433
419
434
if (grantTypesAttrValue .contains (DEVICE )) {
420
435
grantTypes .add (GRANT_DEVICE );
421
436
log .debug ("Added grant '{}'" , GRANT_DEVICE );
422
437
}
423
438
424
- if (grantTypes .contains (GRANT_AUTHORIZATION_CODE )
425
- || grantTypes .contains (GRANT_DEVICE ))
426
- {
427
- setPKCEOptions (c , attrs );
428
- }
429
-
430
439
c .setGrantTypes (grantTypes );
431
440
c .setResponseTypes (responseTypes );
432
441
}
@@ -439,19 +448,66 @@ private void setPKCEOptions(MitreidClient c, Map<String, PerunAttributeValue> at
439
448
log .debug ("Code challenge requested is not equal to '{}'" , PKCE_TYPE_NONE );
440
449
if (PKCE_TYPE_PLAIN .equalsIgnoreCase (codeChallengeType )) {
441
450
log .debug ("Preparing for PKCE with challenge '{}'" , PKCE_TYPE_PLAIN );
442
- preparePkce (c );
443
451
c .setCodeChallengeMethod (PKCEAlgorithm .plain );
444
452
} else if (PKCE_TYPE_SHA256 .equalsIgnoreCase (codeChallengeType )) {
445
453
log .debug ("Preparing for PKCE with challenge '{}'" , PKCE_TYPE_SHA256 );
446
- preparePkce (c );
447
454
c .setCodeChallengeMethod (PKCEAlgorithm .S256 );
448
455
}
449
456
}
450
457
}
451
458
452
- private void preparePkce (MitreidClient c ) {
453
- c .setClientSecret (null );
454
- c .setTokenEndpointAuthMethod (MitreidClient .AuthMethod .NONE );
459
+ private void setTokenEndpointAuthentication (MitreidClient c ,
460
+ Map <String , PerunAttributeValue > attrs )
461
+ {
462
+ String authMethodAttrValue = attrs .get (perunAttrNames .getTokenEndpointAuthenticationMethod ())
463
+ .valueAsString ();
464
+ MitreidClient .AuthMethod authMethod = MitreidClient .AuthMethod .getByValue (authMethodAttrValue );
465
+ if (authMethod == null ) {
466
+ log .debug ("Failed to parse token endpoint authentication method." +
467
+ " Using client_secret_basic as default value." );
468
+ authMethod = MitreidClient .AuthMethod .SECRET_BASIC ;
469
+ }
470
+ c .setTokenEndpointAuthMethod (authMethod );
471
+ if (MitreidClient .AuthMethod .NONE .equals (authMethod )) {
472
+ log .debug ("NONE used as token endpoint authentication method. Removing client_secret" );
473
+ c .setClientSecret (null );
474
+ }
475
+ }
476
+
477
+ private void setRefreshTokens (MitreidClient c , Map <String , PerunAttributeValue > attrs ) {
478
+ Set <String > grantTypes = c .getGrantTypes ();
479
+ if (grantTypes == null ) {
480
+ grantTypes = new HashSet <>();
481
+ }
482
+ if (grantAllowsRefreshTokens (grantTypes )) {
483
+ boolean requestedViaAttr = attrs .containsKey (perunAttrNames .getIssueRefreshTokens ())
484
+ && attrs .get (perunAttrNames .getIssueRefreshTokens ()).valueAsBoolean ();
485
+ boolean requestedViaScopes = c .getScope ().contains (OFFLINE_ACCESS );
486
+ log .debug ("Refresh tokens requested via: attr({}), scopes({})" , requestedViaAttr , requestedViaScopes );
487
+ if (requestedViaAttr || requestedViaScopes ) {
488
+ setUpRefreshTokens (c , attrs );
489
+ }
490
+ }
491
+ }
492
+
493
+ private void setUpRefreshTokens (MitreidClient c , Map <String , PerunAttributeValue > attrs ) {
494
+ c .getScope ().add (OFFLINE_ACCESS );
495
+ c .getGrantTypes ().add (GRANT_REFRESH_TOKEN );
496
+ c .setClearAccessTokensOnRefresh (true );
497
+ c .setReuseRefreshToken (false );
498
+ PerunAttributeValue reuseTokens = attrs .getOrDefault (perunAttrNames .getReuseRefreshTokens (), null );
499
+ if (reuseTokens != null ) {
500
+ c .setReuseRefreshToken (reuseTokens .valueAsBoolean ());
501
+ }
502
+ }
503
+
504
+ private boolean grantAllowsRefreshTokens (Set <String > grantTypes ) {
505
+ boolean res = !grantTypes .isEmpty ()
506
+ && (grantTypes .contains (GRANT_DEVICE )
507
+ || grantTypes .contains (GRANT_AUTHORIZATION_CODE )
508
+ || grantTypes .contains (GRANT_HYBRID ));
509
+ log .debug ("Grants '{}' {} issuing refresh tokens" , grantTypes , res ? "allow" : "disallow" );
510
+ return res ;
455
511
}
456
512
457
513
private void setTokenTimeouts (MitreidClient c , Map <String , PerunAttributeValue > attrs ) {
0 commit comments