@@ -91,11 +91,22 @@ public class GithubAuthenticationToken extends AbstractAuthenticationToken {
91
91
private static final Cache <String , Boolean > publicRepositoryCache =
92
92
CacheBuilder .newBuilder ().expireAfterWrite (1 , CACHE_EXPIRY ).build ();
93
93
94
- private static final Cache <String , GHUser > usersByIdCache =
94
+ private static final Cache <String , GithubUser > usersByIdCache =
95
95
CacheBuilder .newBuilder ().expireAfterWrite (1 , CACHE_EXPIRY ).build ();
96
96
97
97
private final List <GrantedAuthority > authorities = new ArrayList <GrantedAuthority >();
98
98
99
+ private static final GithubUser UNKNOWN_USER = new GithubUser (null );
100
+
101
+ /** Wrapper for cache **/
102
+ static class GithubUser {
103
+ public final GHUser user ;
104
+
105
+ public GithubUser (GHUser user ) {
106
+ this .user = user ;
107
+ }
108
+ }
109
+
99
110
public GithubAuthenticationToken (final String accessToken , final String githubServer ) throws IOException {
100
111
super (new GrantedAuthority [] {});
101
112
@@ -300,18 +311,20 @@ public Boolean call() throws Exception {
300
311
.getLogger (GithubAuthenticationToken .class .getName ());
301
312
302
313
public GHUser loadUser (String username ) throws IOException {
303
- GHUser user ;
314
+ GithubUser user ;
304
315
try {
305
316
user = usersByIdCache .get (username );
306
317
if (gh != null && user == null && isAuthenticated ()) {
307
- user = getGitHub ().getUser (username );
308
- usersByIdCache .put (user .getLogin (), user );
318
+ GHUser ghUser = getGitHub ().getUser (username );
319
+ user = new GithubUser (ghUser );
320
+ usersByIdCache .put (username , user );
309
321
}
310
322
} catch (IOException | ExecutionException e ) {
311
323
LOGGER .log (Level .FINEST , e .getMessage (), e );
312
- user = null ;
324
+ user = UNKNOWN_USER ;
325
+ usersByIdCache .put (username , UNKNOWN_USER );
313
326
}
314
- return user ;
327
+ return user != null ? user . user : null ;
315
328
}
316
329
317
330
public GHOrganization loadOrganization (String organization ) {
@@ -352,51 +365,55 @@ public GHTeam loadTeam(String organization, String team) {
352
365
public GithubOAuthUserDetails getUserDetails (String username ) throws IOException {
353
366
GHUser user = loadUser (username );
354
367
if (user != null ) {
355
- List <GrantedAuthority > groups = new ArrayList <GrantedAuthority >();
356
- try {
357
- GHPersonSet <GHOrganization > orgs ;
358
- if (myRealm == null ) {
359
- Jenkins jenkins = Jenkins .getInstance ();
360
- if (jenkins == null ) {
361
- throw new IllegalStateException ("Jenkins not started" );
362
- }
363
- myRealm = (GithubSecurityRealm ) jenkins .getSecurityRealm ();
364
- }
365
- //Search for scopes that allow fetching team membership. This is documented online.
366
- //https://developer.github.com/v3/orgs/#list-your-organizations
367
- //https://developer.github.com/v3/orgs/teams/#list-user-teams
368
- if (this .userName .equals (username ) && (myRealm .hasScope ("read:org" ) || myRealm .hasScope ("admin:org" ) || myRealm .hasScope ("user" ) || myRealm .hasScope ("repo" ))) {
369
- //This allows us to search for private organization membership.
370
- orgs = getMyself ().getAllOrganizations ();
371
- } else {
372
- //This searches for public organization membership.
373
- orgs = user .getOrganizations ();
368
+ return new GithubOAuthUserDetails (user .getLogin (), this );
369
+ }
370
+ return null ;
371
+ }
372
+
373
+ public GrantedAuthority [] getGrantedAuthorities (GHUser user ) {
374
+ List <GrantedAuthority > groups = new ArrayList <GrantedAuthority >();
375
+ try {
376
+ GHPersonSet <GHOrganization > orgs ;
377
+ if (myRealm == null ) {
378
+ Jenkins jenkins = Jenkins .getInstance ();
379
+ if (jenkins == null ) {
380
+ throw new IllegalStateException ("Jenkins not started" );
374
381
}
375
- for (GHOrganization ghOrganization : orgs ) {
376
- String orgLogin = ghOrganization .getLogin ();
377
- LOGGER .log (Level .FINE , "Fetch teams for user " + username + " in organization " + orgLogin );
378
- groups .add (new GrantedAuthorityImpl (orgLogin ));
379
- try {
380
- if (!getMyself ().isMemberOf (ghOrganization )) {
381
- continue ;
382
- }
383
- Map <String , GHTeam > teams = ghOrganization .getTeams ();
384
- for (Map .Entry <String , GHTeam > entry : teams .entrySet ()) {
385
- GHTeam team = entry .getValue ();
386
- if (team .hasMember (user )) {
387
- groups .add (new GrantedAuthorityImpl (orgLogin + GithubOAuthGroupDetails .ORG_TEAM_SEPARATOR
388
- + team ));
389
- }
382
+ myRealm = (GithubSecurityRealm ) jenkins .getSecurityRealm ();
383
+ }
384
+ //Search for scopes that allow fetching team membership. This is documented online.
385
+ //https://developer.github.com/v3/orgs/#list-your-organizations
386
+ //https://developer.github.com/v3/orgs/teams/#list-user-teams
387
+ if (this .userName .equals (user .getLogin ()) && (myRealm .hasScope ("read:org" ) || myRealm .hasScope ("admin:org" ) || myRealm .hasScope ("user" ) || myRealm .hasScope ("repo" ))) {
388
+ //This allows us to search for private organization membership.
389
+ orgs = getMyself ().getAllOrganizations ();
390
+ } else {
391
+ //This searches for public organization membership.
392
+ orgs = user .getOrganizations ();
393
+ }
394
+ for (GHOrganization ghOrganization : orgs ) {
395
+ String orgLogin = ghOrganization .getLogin ();
396
+ LOGGER .log (Level .FINE , "Fetch teams for user " + user .getLogin () + " in organization " + orgLogin );
397
+ groups .add (new GrantedAuthorityImpl (orgLogin ));
398
+ try {
399
+ if (!getMyself ().isMemberOf (ghOrganization )) {
400
+ continue ;
401
+ }
402
+ Map <String , GHTeam > teams = ghOrganization .getTeams ();
403
+ for (Map .Entry <String , GHTeam > entry : teams .entrySet ()) {
404
+ GHTeam team = entry .getValue ();
405
+ if (team .hasMember (user )) {
406
+ groups .add (new GrantedAuthorityImpl (orgLogin + GithubOAuthGroupDetails .ORG_TEAM_SEPARATOR
407
+ + team ));
390
408
}
391
- } catch (IOException | Error ignore ) {
392
- LOGGER .log (Level .FINEST , "not enough rights to list teams from " + orgLogin , ignore );
393
409
}
410
+ } catch (IOException | Error ignore ) {
411
+ LOGGER .log (Level .FINEST , "not enough rights to list teams from " + orgLogin , ignore );
394
412
}
395
- } catch (IOException e ) {
396
- LOGGER .log (Level .FINE , e .getMessage (), e );
397
413
}
398
- return new GithubOAuthUserDetails (user , groups .toArray (new GrantedAuthority [groups .size ()]));
414
+ } catch (IOException e ) {
415
+ LOGGER .log (Level .FINE , e .getMessage (), e );
399
416
}
400
- return null ;
417
+ return groups . toArray ( new GrantedAuthority [ groups . size ()]) ;
401
418
}
402
419
}
0 commit comments