forked from jenkinsci/github-oauth-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GithubAuthenticationToken.java
186 lines (148 loc) · 5.31 KB
/
GithubAuthenticationToken.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
The MIT License
Copyright (c) 2011 Michael O'Cleirigh
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package org.jenkinsci.plugins;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import hudson.security.SecurityRealm;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.providers.AbstractAuthenticationToken;
import org.kohsuke.github.GHOrganization;
import org.kohsuke.github.GHTeam;
import org.kohsuke.github.GHUser;
import org.kohsuke.github.GitHub;
/**
* @author mocleiri
*
* to hold the authentication token from the github oauth process.
*
*/
public class GithubAuthenticationToken extends AbstractAuthenticationToken {
/**
*
*/
private static final long serialVersionUID = 1L;
private final String accessToken;
private final String userName;
private final GitHub gh;
/**
* Cache for faster organization based security
*/
private static final Cache<String, Set<String>> userOrganizationCache =
CacheBuilder.newBuilder().expireAfterWrite(1,TimeUnit.HOURS).build();
private final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
public GithubAuthenticationToken(String accessToken, String githubServer) throws IOException {
super(new GrantedAuthority[] {});
this.accessToken = accessToken;
this.gh = GitHub.connectUsingOAuth(githubServer, accessToken);
GHUser me = gh.getMyself();
assert me!=null;
setAuthenticated(true);
this.userName = me.getLogin();
authorities.add(SecurityRealm.AUTHENTICATED_AUTHORITY);
for (String name : gh.getMyOrganizations().keySet())
authorities.add(new GrantedAuthorityImpl(name));
}
/**
* Gets the OAuth access token, so that it can be persisted and used elsewhere.
*/
public String getAccessToken() {
return accessToken;
}
public GitHub getGitHub() {
return gh;
}
@Override
public GrantedAuthority[] getAuthorities() {
return authorities.toArray(new GrantedAuthority[authorities.size()]);
}
public Object getCredentials() {
return ""; // do not expose the credential
}
/**
* Returns the login name in GitHub.
*/
public String getPrincipal() {
return this.userName;
}
/**
* For some reason I can't get the github api to tell me for the current
* user the groups to which he belongs.
*
* So this is a slightly larger consideration. If the authenticated user is
* part of any team within the organization then they have permission.
*
* It caches user organizations for 24 hours for faster web navigation.
*
* @param candidateName
* @param organization
* @return
*/
public boolean hasOrganizationPermission(String candidateName,
String organization) {
try {
Set<String> v = userOrganizationCache.get(candidateName,new Callable<Set<String>>() {
@Override
public Set<String> call() throws Exception {
return gh.getMyOrganizations().keySet();
}
});
return v.contains(organization);
} catch (ExecutionException e) {
throw new RuntimeException("authorization failed for user = "
+ candidateName, e);
}
}
private static final Logger LOGGER = Logger
.getLogger(GithubAuthenticationToken.class.getName());
public GHUser loadUser(String username) throws IOException {
if (gh != null && isAuthenticated())
return gh.getUser(username);
else
return null;
}
public GHOrganization loadOrganization(String organization)
throws IOException {
if (gh != null && isAuthenticated())
return gh.getOrganization(organization);
else
return null;
}
public GHTeam loadTeam(String organization, String team) throws IOException {
if (gh != null && isAuthenticated()) {
GHOrganization org = gh.getOrganization(organization);
if (org != null) {
Map<String, GHTeam> teamMap = org.getTeams();
return teamMap.get(team);
} else
return null;
} else
return null;
}
}