1
0
Fork 0

reworked credentials for easier extension, and we're now sending creds with each request per https://developer.atlassian.com/display/JIRADEV/JIRA+REST+API+Example+-+Basic+Authentication

master
Bob Carroll 2013-05-31 18:11:46 -07:00
parent bef149873c
commit 4fb1849039
5 changed files with 102 additions and 20 deletions

View File

@ -56,11 +56,7 @@ public class Example {
public static void main(String[] args) {
BasicCredentials creds = new BasicCredentials();
creds.host = "jira.example.com"; /* this isn't required, but is good for security */
creds.username = "batman";
creds.password = "pow! pow!";
BasicCredentials creds = new BasicCredentials("batman", "pow! pow!");
JiraClient jira = new JiraClient("https://jira.example.com/jira", creds);
try {

View File

@ -19,12 +19,48 @@
package net.rcarz.jiraclient;
import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.auth.BasicScheme;
/**
* Basic HTTP authentication credentials structure.
* Basic HTTP authentication credentials.
*/
public class BasicCredentials {
public String host;
public String username;
public String password;
public class BasicCredentials implements ICredentials {
private String username;
private String password;
/**
* Creates new basic HTTP credentials.
*
* @param username
* @param password
*/
public BasicCredentials(String username, String password) {
this.username = username;
this.password = password;
}
/**
* Sets the Authorization header for the given request.
*
* @param req HTTP request to authenticate
*/
public void authenticate(HttpRequest req) {
Credentials creds = new UsernamePasswordCredentials(username, password);
req.addHeader(new BasicScheme().authenticate(creds, "utf-8", false));
}
/**
* Gets the logon name representing these credentials.
*
* @return logon name as a string
*/
public String getLogonName() {
return username;
}
}

View File

@ -0,0 +1,40 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient;
import org.apache.http.HttpRequest;
public interface ICredentials {
/**
* Sets the Authorization header for the given request.
*
* @param req HTTP request to authenticate
*/
void authenticate(HttpRequest req);
/**
* Gets the logon name representing these credentials.
*
* @return logon name as a string
*/
String getLogonName();
}

View File

@ -21,8 +21,6 @@ package net.rcarz.jiraclient;
import java.net.URI;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
@ -49,17 +47,13 @@ public class JiraClient {
* @param uri Base URI of the JIRA server
* @param creds Credentials to authenticate with
*/
public JiraClient(String uri, BasicCredentials creds) {
public JiraClient(String uri, ICredentials creds) {
DefaultHttpClient httpclient = new DefaultHttpClient();
if (creds != null && creds.username != null && !creds.username.equals("")) {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(creds.host, 443),
new UsernamePasswordCredentials(creds.username, creds.password));
}
restclient = new RestClient(httpclient, creds, URI.create(uri));
restclient = new RestClient(httpclient, URI.create(uri));
username = creds.username;
if (creds != null)
username = creds.getLogonName();
}
/**

View File

@ -51,6 +51,7 @@ import net.sf.json.JSONSerializer;
public class RestClient {
private HttpClient httpclient = null;
private ICredentials creds = null;
private URI uri = null;
/**
@ -60,7 +61,19 @@ public class RestClient {
* @param uri Base URI of the remote REST service
*/
public RestClient(HttpClient httpclient, URI uri) {
this(httpclient, null, uri);
}
/**
* Creates an authenticated REST client instance with a URI.
*
* @param httpclient Underlying HTTP client to use
* @param creds Credentials to send with each request
* @param uri Base URI of the remote REST service
*/
public RestClient(HttpClient httpclient, ICredentials creds, URI uri) {
this.httpclient = httpclient;
this.creds = creds;
this.uri = uri;
}
@ -102,6 +115,9 @@ public class RestClient {
private JSON request(HttpRequestBase req) throws RestException, IOException {
req.addHeader("Accept", "application/json");
if (creds != null)
creds.authenticate(req);
HttpResponse resp = httpclient.execute(req);
HttpEntity ent = resp.getEntity();
StringBuilder result = new StringBuilder();