diff --git a/pom.xml b/pom.xml index 3480d85..e75efa8 100644 --- a/pom.xml +++ b/pom.xml @@ -1,255 +1,255 @@ 4.0.0 org.jenkins-ci.plugins plugin 1.586 org.jenkins-ci.plugins phab-oauth - 0.35 + 0.37 Phabricator Authentication Plugin TODO hpi MIT license All source code is under the MIT license. rorist Jean-Baptiste Aubort jean-baptiste.aubort@epfl.ch https://github.com/rorist maintainer Europe/Zurich scm:git:ssh://github.com/rorist/phab-oauth-plugin.git scm:git:ssh://git@github.com/rorist/phab-oauth-plugin.git https://github.com/rorist/phab-oauth-plugin HEAD repo.jenkins-ci.org http://repo.jenkins-ci.org/public/ repo.jenkins-ci.org http://repo.jenkins-ci.org/public/ org.jenkins-ci.plugins mailer 1.4 org.apache.httpcomponents httpclient 4.1 org.json json 20090211 org.jenkins-ci.plugins git 2.0.3 jar org.apache.maven.plugins maven-compiler-plugin 1.6 1.6 true org.jenkins-ci.tools maven-hpi-plugin 1.109 true 1.93 org.eclipse.m2e lifecycle-mapping 1.0.0 org.kohsuke access-modifier-checker [1.0,) enforce com.cloudbees maven-license-plugin [1.4,) process org.jvnet.hudson.tools maven-hpi-plugin [2.1.1,) org.apache.maven.plugins maven-enforcer-plugin [1.0,) display-info org.codehaus.gmaven gmaven-plugin [1.3,) generateTestStubs org.jvnet.localizer maven-localizer-plugin [1.12,) generate org.jenkins-ci.tools maven-hpi-plugin [1.74,) UTF-8 UTF-8 diff --git a/src/main/java/org/jenkinsci/plugins/PhabricatorOAuthUserDetails.java b/src/main/java/org/jenkinsci/plugins/PhabricatorOAuthUserDetails.java index 27717d9..6808b1e 100644 --- a/src/main/java/org/jenkinsci/plugins/PhabricatorOAuthUserDetails.java +++ b/src/main/java/org/jenkinsci/plugins/PhabricatorOAuthUserDetails.java @@ -1,39 +1,39 @@ /** The MIT License Copyright (c) 2016 Jean-Baptiste Aubort 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 org.acegisecurity.userdetails.User; import org.acegisecurity.userdetails.UserDetails; import org.acegisecurity.GrantedAuthority; public class PhabricatorOAuthUserDetails extends User implements UserDetails { private static final long serialVersionUID = 1L; public PhabricatorOAuthUserDetails(String user, GrantedAuthority[] authorities) { - super(user, "", true, true, true, true, authorities); + super(user, "nopassword", true, true, true, true, authorities); } } diff --git a/src/main/java/org/jenkinsci/plugins/PhabricatorSecurityRealm.java b/src/main/java/org/jenkinsci/plugins/PhabricatorSecurityRealm.java index 4338c57..1123e3f 100644 --- a/src/main/java/org/jenkinsci/plugins/PhabricatorSecurityRealm.java +++ b/src/main/java/org/jenkinsci/plugins/PhabricatorSecurityRealm.java @@ -1,275 +1,279 @@ /** The MIT License Copyright (c) 2011 Michael O'Cleirigh Copyright (c) 2016 Jean-Baptiste Aubort 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 hudson.Extension; import hudson.Util; import hudson.model.Descriptor; import hudson.model.User; import hudson.security.SecurityRealm; import hudson.tasks.Mailer; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import jenkins.model.Jenkins; import org.acegisecurity.Authentication; import org.acegisecurity.AuthenticationException; import org.acegisecurity.AuthenticationManager; import org.acegisecurity.BadCredentialsException; import org.acegisecurity.GrantedAuthority; import org.acegisecurity.context.SecurityContextHolder; import org.acegisecurity.providers.UsernamePasswordAuthenticationToken; import org.acegisecurity.userdetails.UserDetails; import org.acegisecurity.userdetails.UserDetailsService; import org.acegisecurity.userdetails.UsernameNotFoundException; import org.apache.http.HttpEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.jfree.util.Log; import org.json.JSONException; import org.json.JSONObject; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.Header; import org.kohsuke.stapler.HttpRedirect; import org.kohsuke.stapler.HttpResponse; import org.kohsuke.stapler.HttpResponses; import org.kohsuke.stapler.StaplerRequest; import org.springframework.dao.DataAccessException; public class PhabricatorSecurityRealm extends SecurityRealm { private static final String OAUTH_SCOPES = ""; private static final String PHAB_OAUTH = "oauthserver"; protected static final String PHAB_API = "api/user.whoami"; private static final String REFERER_ATTRIBUTE = PhabricatorSecurityRealm.class .getName() + ".referer"; private static final Logger LOGGER = Logger .getLogger(PhabricatorSecurityRealm.class.getName()); private String clientID; private String clientSecret; private String serverURL; // private boolean trustAllCert; @DataBoundConstructor public PhabricatorSecurityRealm(String serverURL, String clientID, String clientSecret, boolean trustAllCert) { super(); this.serverURL = Util.fixEmptyAndTrim(serverURL); this.clientID = Util.fixEmptyAndTrim(clientID); this.clientSecret = Util.fixEmptyAndTrim(clientSecret); // this.trustAllCert = trustAllCert; } public PhabricatorSecurityRealm() { super(); LOGGER.log(Level.FINE, "PhabricatorSecurityRealm()"); } public String getClientID() { return clientID; } public void setClientID(String clientID) { this.clientID = clientID; } public String getClientSecret() { return clientSecret; } public void setClientSecret(String clientSecret) { this.clientSecret = clientSecret; } public String getServerURL() { return serverURL; } public void setServerURL(String serverURL) { this.serverURL = serverURL; } @Override public String getLoginUrl() { return "securityRealm/commenceLogin"; } public HttpResponse doCommenceLogin(StaplerRequest request, @Header("Referer") final String referer) throws IOException { LOGGER.log(Level.WARNING, "doCommenceLogin"); request.getSession().setAttribute(REFERER_ATTRIBUTE, referer); return new HttpRedirect(serverURL + PHAB_OAUTH + "/auth/?client_id=" + clientID + "&response_type=code&scope=" + OAUTH_SCOPES); } 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(); } String rootUrl = Jenkins.getInstance().getRootUrl() + "securityRealm/finishLogin"; String authUrl = serverURL + PHAB_OAUTH + "/token/?client_id=" + clientID + "&client_secret=" + clientSecret + "&code=" + code + "&grant_type=authorization_code&redirect_uri=" + rootUrl; String content = getUrlContent(authUrl); String accessToken; try { JSONObject jsonObject = new JSONObject(content); accessToken = jsonObject.getString("access_token"); LOGGER.log(Level.WARNING, "accessToken FOUND=" + accessToken); } catch (JSONException e) { LOGGER.log(Level.WARNING, "accessToken not found=" + e.getMessage()); accessToken = null; } if (accessToken != null && accessToken.trim().length() > 0) { PhabricatorAuthenticationToken auth = new PhabricatorAuthenticationToken( accessToken); SecurityContextHolder.getContext().setAuthentication(auth); PhabricatorUser user = auth.getUser(); User u = User.current(); u.setFullName(user.getRealname()); u.addProperty(new Mailer.UserProperty(user.getEmail())); // TODO: What is it for the u object ? } else { Log.info("Phabricator did not return an access token."); } String referer = (String) request.getSession().getAttribute( REFERER_ATTRIBUTE); if (referer != null) return HttpResponses.redirectTo(referer); return HttpResponses.redirectToContextRoot(); } protected String getUrlContent(String url) throws IOException { /* * FIXME if (trustAllCert) { LOGGER.log(Level.WARNING, * "Trust all certificates"); Protocol easyhttps = new Protocol("https", * new EasySSLProtocolSocketFactory(), 443); * Protocol.registerProtocol("https", easyhttps); } */ HttpGet httpGet = new HttpGet(url); DefaultHttpClient httpclient = new DefaultHttpClient(); org.apache.http.HttpResponse response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity(); String content = EntityUtils.toString(entity); httpclient.getConnectionManager().shutdown(); return content; } @Override public boolean allowsSignup() { return false; } @Override public SecurityComponents createSecurityComponents() { return new SecurityComponents(new AuthenticationManager() { public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (authentication instanceof PhabricatorAuthenticationToken) return authentication; if (authentication instanceof UsernamePasswordAuthenticationToken) try { UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication; PhabricatorAuthenticationToken auth = new PhabricatorAuthenticationToken( token.getCredentials().toString()); SecurityContextHolder.getContext().setAuthentication( auth); return auth; } catch (IOException e) { throw new RuntimeException(e); } throw new BadCredentialsException( "Unexpected authentication type: " + authentication); } }, new UserDetailsService() { public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { return PhabricatorSecurityRealm.this .loadUserByUsername(username); } }); } @Override public UserDetails loadUserByUsername(String username) { PhabricatorAuthenticationToken authToken = (PhabricatorAuthenticationToken) SecurityContextHolder .getContext().getAuthentication(); if (authToken == null) { throw new UsernameNotFoundException("Could not get auth token."); } + if (username == null) { + throw new UsernameNotFoundException("Could not get username."); + } + List authorities = new ArrayList(); authorities.add(SecurityRealm.AUTHENTICATED_AUTHORITY); PhabricatorOAuthUserDetails userDetails = new PhabricatorOAuthUserDetails( username, authorities.toArray(new GrantedAuthority[authorities .size()])); return userDetails; } @Extension public static final class DescriptorImpl extends Descriptor { @Override public String getHelpFile() { return "/plugin/phab-oauth/help/help-security-realm.html"; } @Override public String getDisplayName() { return "Phabricator OAuth Plugin"; } public DescriptorImpl() { super(); } public DescriptorImpl(Class clazz) { super(clazz); } } }