diff --git a/pom.xml b/pom.xml index 5fc8425..bb51529 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.30 + 0.32 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/PhabricatorAuthenticationToken.java b/src/main/java/org/jenkinsci/plugins/PhabricatorAuthenticationToken.java index 6eae194..a084e45 100644 --- a/src/main/java/org/jenkinsci/plugins/PhabricatorAuthenticationToken.java +++ b/src/main/java/org/jenkinsci/plugins/PhabricatorAuthenticationToken.java @@ -1,126 +1,130 @@ /** 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.security.SecurityRealm; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.logging.Logger; import java.util.logging.Level; +import java.util.logging.Logger; import jenkins.model.Jenkins; +import org.acegisecurity.BadCredentialsException; import org.acegisecurity.GrantedAuthority; import org.acegisecurity.providers.AbstractAuthenticationToken; -import org.acegisecurity.BadCredentialsException; - -import org.json.JSONObject; import org.json.JSONException; +import org.json.JSONObject; public class PhabricatorAuthenticationToken extends AbstractAuthenticationToken { private static final long serialVersionUID = 1L; private final String accessToken; private PhabricatorUser user; private final String userName; private PhabricatorSecurityRealm myRealm = null; private final List authorities = new ArrayList(); public PhabricatorAuthenticationToken(String accessToken) throws IOException { super(new GrantedAuthority[] {}); this.accessToken = accessToken; authorities.add(SecurityRealm.AUTHENTICATED_AUTHORITY); if (Jenkins.getInstance().getSecurityRealm() instanceof PhabricatorSecurityRealm) { if (myRealm == null) { myRealm = (PhabricatorSecurityRealm) Jenkins.getInstance() .getSecurityRealm(); } } user = authUsingToken(); if (user == null) { throw new BadCredentialsException("Unexpected authentication type"); } userName = user.getUsername(); setAuthenticated(true); } protected PhabricatorUser getUser() { if (user == null && accessToken != null) { try { user = authUsingToken(); } catch (IOException e) { LOGGER.log(Level.WARNING, e.getMessage()); } } return user; } protected PhabricatorUser authUsingToken() throws IOException { String serverURL = myRealm.getServerURL(); String result = myRealm.getUrlContent(serverURL + PhabricatorSecurityRealm.PHAB_API + "?access_token=" + accessToken); PhabricatorUser user = null; try { JSONObject jsonObject = new JSONObject(result); JSONObject jsonResult = jsonObject.getJSONObject("result"); user = new PhabricatorUser(jsonResult.getString("userName"), jsonResult.getString("realName"), jsonResult.getString("primaryEmail"), jsonResult.getString("image")); } catch (JSONException e) { LOGGER.log(Level.WARNING, e.getMessage()); } return user; } public String getAccessToken() { return accessToken; } public Object getCredentials() { return ""; // do not expose the credential } public String getPrincipal() { return this.userName; } + @Override + public GrantedAuthority[] getAuthorities() { + return authorities.toArray(new GrantedAuthority[authorities.size()]); + } + private static final Logger LOGGER = Logger .getLogger(PhabricatorAuthenticationToken.class.getName()); }