Skip to content

Commit

Permalink
Support for authentication realms if the underlying connection URL is…
Browse files Browse the repository at this point in the history
… not delivering requestingURL to the Authenticator class

(fix for issue beders#7)
  • Loading branch information
beders committed Feb 28, 2012
1 parent e0db3df commit 9881b96
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 16 deletions.
22 changes: 22 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Changes
-------

In Development:

- support for security realms (Resty.authenticateForRealm()) in case the regular authentication is not working because java is not able to determine the URL


Since 0.3.0:

- Option to ignore SSL certificate errors: Resty.ignoreAllCerts (global switch for now)
- New constructor to specify options: new Resty(Option.timeout(3000)); (sets the socket connect timeout)
- Create your own Options (see Resty.Option.Timeout or Resty.Option.Proxy for example)
- Fixed scala naming issue
- enhanced syntax for JSON queries
- bugfixes from my contributors. Thank you!
- Proxy support. Thank you, Gabriel. r.setProxy(...) for object r or new Resty(Option.proxy(...)) to carry proxy settings over when traversing paths
- convenient location header: new Resty().bytes(url, put(someContent)).location(); // gets Location header as URI

Since 0.2.0:

- Support for PUT, DELETE, Support for application/multipart-formdata
13 changes: 13 additions & 0 deletions src/main/java/us/monoid/web/Resty.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public void authenticate(URI aSite, String aLogin, char[] aPwd) {
}

/**
*
* @see Resty#authenticate(URI, String, char[])
*
* @param string
Expand All @@ -148,6 +149,18 @@ public void authenticate(URI aSite, String aLogin, char[] aPwd) {
public void authenticate(String string, String aLogin, char[] charArray) {
authenticate(URI.create(string), aLogin, charArray);
}

/**
* Register a login password for the realm returned by the authorization challenge.
* Use this method instead of authenticate in case the URL is not made available to the java.net.Authenticator class
*
* @param realm the realm (see rfc2617, section 1.2)
* @param aLogin
* @param charArray
*/
public void authenticateForRealm(String realm, String aLogin, char[] charArray) {
rath.addRealm(realm, aLogin, charArray);
}

/**
* Sets the User-Agent to identify as Mozilla/Firefox. Otherwise a Resty specific User-Agent is used
Expand Down
59 changes: 43 additions & 16 deletions src/main/java/us/monoid/web/auth/RestyAuthenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,51 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Authenticator used for HTTP based authentication.
* This class is not used directly, but through adding credentials using a Resty instance.
* Also, this class is not useable for hundreds or thousands of credentials.
* Authenticator used for HTTP based authentication. This class is not used directly, but through adding credentials using a Resty instance. Also, this class is not useable for hundreds or thousands
* of credentials.
*
* @author beders
*
*
*/
public class RestyAuthenticator extends Authenticator {
List<Site> sites = Collections.synchronizedList(new ArrayList<Site>());
Map<String, Realm> realms = new ConcurrentHashMap<String, Realm>();

@Override
protected PasswordAuthentication getPasswordAuthentication() {
URL requestURL = getRequestingURL();
try {
String uri = requestURL.toURI().normalize().toString();
Site site = findBestMatch(uri);
if (site != null) {
return new PasswordAuthentication(site.login, site.pwd);
if (requestURL != null) {
try {
String uri = requestURL.toURI().normalize().toString();
Site site = findBestMatch(uri);
if (site != null) {
return new PasswordAuthentication(site.login, site.pwd);
}
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
String realm = getRequestingPrompt();
Realm r = findRealm(realm);
if (r != null) {
return new PasswordAuthentication(r.login, r.pwd);
}
return null;
}

/** Add or replace an authentication for a root URL aka site.

/**
* Add or replace an authentication for a root URL aka site.
*
* @param aRootUrl
* @param login
* @param pwd
*/

public void addSite(URI aRootUrl, String login, char[] pwd) {
String rootUri = aRootUrl.normalize().toString();
boolean replaced = false;
Expand All @@ -66,7 +76,7 @@ public void addSite(URI aRootUrl, String login, char[] pwd) {
sites.add(s);
}
}

private Site findBestMatch(String uri) {
int max = 0;
Site bestSite = null;
Expand All @@ -80,15 +90,32 @@ private Site findBestMatch(String uri) {
return bestSite;
}

public void addRealm(String aRealm, String aLogin, char[] charArray) {
Realm r = new Realm(aLogin, charArray);
realms.put(aRealm, r);
}

Realm findRealm(String aRealm) {
return realms.get(aRealm);
}

static class Realm {
String login;
char[] pwd;
public Realm(String aLogin, char[] charArray) { login = aLogin; pwd = charArray; }
}

static class Site {
String root;
String login;
char[] pwd;

public int match(String uri) { // probably not correct. TODO look up better host matching
if (uri.startsWith(root)) {
return root.length();
}
return 0;
}
}

}
19 changes: 19 additions & 0 deletions src/test/java/example/BartStations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package example;

import us.monoid.web.Resty;


public class BartStations {

/**
* @param args
* @throws Exception
* @throws
*/
public static void main(String[] args) throws Exception {
Resty r = new Resty();
String result = r.xml("https://bart.gov/dev/eta/bart_eta.xml").get("/root/station/eta/estimate[../../name/text()='Powell St.' and ../destination/text()='SF Airport']", String.class);
System.out.println("Next train to SFO from Powell St.:" + result);
}

}

0 comments on commit 9881b96

Please sign in to comment.