Page MenuHomec4science

PhabricatorAuthenticationToken.java
No OneTemporary

File Metadata

Created
Thu, Mar 28, 17:45

PhabricatorAuthenticationToken.java

/**
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 jenkins.model.Jenkins;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.providers.AbstractAuthenticationToken;
import org.acegisecurity.BadCredentialsException;
import org.json.JSONObject;
import org.json.JSONException;
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<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
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;
}
private static final Logger LOGGER = Logger
.getLogger(PhabricatorAuthenticationToken.class.getName());
}

Event Timeline