/** This is where the user comes back to at the end of the OpenID redirect ping-pong. */ public HttpResponse doFinishLogin(StaplerRequest request) throws IOException { String code = request.getParameter("code"); if (code == null || code.trim().length() == 0) { Log.info("doFinishLogin: missing code."); return HttpResponses.redirectToContextRoot(); } Log.info("test"); HttpPost httpost = new HttpPost( githubUri + "/login/oauth/access_token?" + "client_id=" + clientID + "&" + "client_secret=" + clientSecret + "&" + "code=" + code); DefaultHttpClient httpclient = new DefaultHttpClient(); org.apache.http.HttpResponse response = httpclient.execute(httpost); HttpEntity entity = response.getEntity(); String content = EntityUtils.toString(entity); // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); String accessToken = extractToken(content); if (accessToken != null && accessToken.trim().length() > 0) { String githubServer = githubUri.replaceFirst("http.*\\/\\/", ""); // only set the access token if it exists. GithubAuthenticationToken auth = new GithubAuthenticationToken(accessToken, githubServer); SecurityContextHolder.getContext().setAuthentication(auth); GHUser self = auth.getGitHub().getMyself(); User u = User.current(); u.setFullName(self.getName()); u.addProperty(new Mailer.UserProperty(self.getEmail())); } else { Log.info("Github did not return an access token."); } String referer = (String) request.getSession().getAttribute(REFERER_ATTRIBUTE); if (referer != null) return HttpResponses.redirectTo(referer); return HttpResponses .redirectToContextRoot(); // referer should be always there, but be defensive }
/** * @param username * @return * @throws UsernameNotFoundException * @throws DataAccessException */ @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { GHUser user = null; GithubAuthenticationToken authToken = (GithubAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); if (authToken == null) throw new UsernameNotFoundException("No known user: "******"user(" + username + ") is also an organization"); } user = authToken.loadUser(username); if (user != null) return new GithubOAuthUserDetails(user); else throw new UsernameNotFoundException("No known user: "******"loadUserByUsername (username="******")", e); } }
/** * @param groupName * @return * @throws UsernameNotFoundException * @throws DataAccessException */ @Override public GroupDetails loadGroupByGroupname(String groupName) throws UsernameNotFoundException, DataAccessException { GithubAuthenticationToken authToken = (GithubAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); if (authToken == null) throw new UsernameNotFoundException("No known group: " + groupName); try { GHOrganization org = authToken.loadOrganization(groupName); if (org != null) return new GithubOAuthGroupDetails(org); else throw new UsernameNotFoundException("No known group: " + groupName); } catch (IOException e) { throw new DataRetrievalFailureException( "loadGroupByGroupname (groupname=" + groupName + ")", e); } }
@Override protected void tearDown() throws Exception { super.tearDown(); GithubAuthenticationToken.clearCaches(); }