@@ -26,27 +26,27 @@ of this software and associated documentation files (the "Software"), to deal
26
26
*/
27
27
package org .jenkinsci .plugins ;
28
28
29
+ import com .google .common .cache .Cache ;
30
+ import com .google .common .cache .CacheBuilder ;
31
+ import hudson .security .SecurityRealm ;
29
32
import java .io .IOException ;
30
33
import java .util .ArrayList ;
31
- import java .util .List ;
32
- import java .util .Map ;
33
- import java .util .Set ;
34
+ import java .util .Arrays ;
35
+ import java .util .Collection ;
34
36
import java .util .concurrent .Callable ;
35
37
import java .util .concurrent .ExecutionException ;
36
38
import java .util .concurrent .TimeUnit ;
37
39
import java .util .HashSet ;
38
- import java .util .logging . Logger ;
40
+ import java .util .List ;
39
41
import java .util .logging .Level ;
40
-
41
- import com .google .common .cache .Cache ;
42
- import com .google .common .cache .CacheBuilder ;
43
- import hudson .security .SecurityRealm ;
44
- import java .util .Collection ;
45
-
46
- import org .jenkinsci .plugins .GithubOAuthUserDetails ;
42
+ import java .util .logging .Logger ;
43
+ import java .util .Map ;
44
+ import java .util .Set ;
45
+ import jenkins .model .Jenkins ;
47
46
import org .acegisecurity .GrantedAuthority ;
48
47
import org .acegisecurity .GrantedAuthorityImpl ;
49
48
import org .acegisecurity .providers .AbstractAuthenticationToken ;
49
+ import org .jenkinsci .plugins .GithubOAuthUserDetails ;
50
50
import org .kohsuke .github .GHMyself ;
51
51
import org .kohsuke .github .GHOrganization ;
52
52
import org .kohsuke .github .GHPersonSet ;
@@ -72,6 +72,7 @@ public class GithubAuthenticationToken extends AbstractAuthenticationToken {
72
72
private final String userName ;
73
73
private final GitHub gh ;
74
74
private final GHMyself me ;
75
+ private GithubSecurityRealm myRealm = null ;
75
76
76
77
/**
77
78
* Cache for faster organization based security
@@ -103,13 +104,23 @@ public GithubAuthenticationToken(String accessToken, String githubServer) throws
103
104
104
105
this .userName = this .me .getLogin ();
105
106
authorities .add (SecurityRealm .AUTHENTICATED_AUTHORITY );
106
- Map <String , Set <GHTeam >> myTeams = gh .getMyTeams ();
107
- for (String orgLogin : myTeams .keySet ()) {
108
- LOGGER .log (Level .FINE , "Fetch teams for user " + userName + " in organization " + orgLogin );
109
- authorities .add (new GrantedAuthorityImpl (orgLogin ));
110
- for (GHTeam team : myTeams .get (orgLogin )) {
111
- authorities .add (new GrantedAuthorityImpl (orgLogin + GithubOAuthGroupDetails .ORG_TEAM_SEPARATOR
112
- + team .getName ()));
107
+ if (Jenkins .getInstance ().getSecurityRealm () instanceof GithubSecurityRealm ) {
108
+ if (myRealm == null ) {
109
+ myRealm = (GithubSecurityRealm ) Jenkins .getInstance ().getSecurityRealm ();
110
+ }
111
+ //Search for scopes that allow fetching team membership. This is documented online.
112
+ //https://developer.github.com/v3/orgs/#list-your-organizations
113
+ //https://developer.github.com/v3/orgs/teams/#list-user-teams
114
+ if (myRealm .hasScope ("read:org" ) || myRealm .hasScope ("admin:org" ) || myRealm .hasScope ("user" ) || myRealm .hasScope ("repo" )) {
115
+ Map <String , Set <GHTeam >> myTeams = gh .getMyTeams ();
116
+ for (String orgLogin : myTeams .keySet ()) {
117
+ LOGGER .log (Level .FINE , "Fetch teams for user " + userName + " in organization " + orgLogin );
118
+ authorities .add (new GrantedAuthorityImpl (orgLogin ));
119
+ for (GHTeam team : myTeams .get (orgLogin )) {
120
+ authorities .add (new GrantedAuthorityImpl (orgLogin + GithubOAuthGroupDetails .ORG_TEAM_SEPARATOR
121
+ + team .getName ()));
122
+ }
123
+ }
113
124
}
114
125
}
115
126
}
0 commit comments