Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# JWTs for Java

This is not an officially supported Google product

[![CircleCI](https://img.shields.io/circleci/project/github/auth0/java-jwt.svg?style=flat-square)](https://circleci.com/gh/auth0/java-jwt/tree/master)
Expand Down
8 changes: 8 additions & 0 deletions lib/src/main/java/oicclient/exceptions/WebFingerError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package oicclient.exceptions;

public class WebFingerError extends Exception{

public WebFingerError(String message) {
super(message);
}
}
20 changes: 20 additions & 0 deletions lib/src/main/java/oicclient/tuples/Tuple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package oicclient.tuples;

public class Tuple {

private Object a;
private Object b;

public Tuple(Object a, Object b) {
this.a = a;
this.b = b;
}

public Object getA() {
return a;
}

public Object getB() {
return b;
}
}
48 changes: 48 additions & 0 deletions lib/src/main/java/oicclient/webfinger/URINormalizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package oicclient.webfinger;

public class URINormalizer {

private static boolean hasScheme(String path) {
if (path.contains("://")) {
return true;
} else {
String authority = path.replace("/", "#")
.replace("?", "#").split("#")[0];

String hostOrPort;
if (authority.contains(":")) {
hostOrPort = authority.split(":", 1)[1];
if (hostOrPort.matches("^\\d+$")) {
return false;
}
} else {
return false;
}
}

return true;
}

private static boolean isAccountSchemeAssumed(String path) {
String[] arr;
if (path.contains("@")) {
arr = path.split("@");
String host = arr[arr.length - 1];
return !(host.contains(":") || host.contains("/") || host.contains("?"));
} else {
return false;
}
}

String normalize(String path) {
if (!hasScheme(path)) {
if (isAccountSchemeAssumed(path)) {
path = "acct:" + path;
} else {
path = "https://" + path;
}
}

return path.split("#")[0];
}
}
97 changes: 97 additions & 0 deletions lib/src/main/java/oicclient/webfinger/WebFinger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package oicclient.webfinger;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Strings;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import oicclient.exceptions.WebFingerError;
import oicclient.tuples.Tuple;

public class WebFinger {
public String defaultRelt;
private JRD jrd;
private List<Map<String, Object>> events;
private static final String WF_URL = "https://%s/.well-known/webfinger";

public WebFinger(String defaultRelt) {
this.defaultRelt = defaultRelt;
this.jrd = null;
this.events = new ArrayList<>();
}

public String query(String resource, List<String> rel) throws URISyntaxException, WebFingerError {
resource = new URINormalizer().normalize(resource);
List<Tuple> queryParamsTuple = new ArrayList<>(Arrays.asList(new Tuple("resource", resource)));

if (rel == null) {
if (!Strings.isNullOrEmpty(this.defaultRelt)) {
queryParamsTuple.add(new Tuple("rel", this.defaultRelt));
}
} else {
for (String index : rel) {
queryParamsTuple.add(new Tuple("rel", index));
}
}

String host;
if (resource.startsWith("http")) {
URI uri = new URI(resource);
host = uri.getHost();
int port = uri.getPort();
if (port != -1) {
host += ":" + port;
}
} else if (resource.startsWith("acct:")) {
String[] arr = resource.split("@");
host = arr[arr.length - 1];
arr = host.replace("/", "#").replace("?", "#").split("#");
host = arr[0];
} else if (resource.startsWith("device:")) {
String[] arr = resource.split(":");
host = arr[1];
} else {
throw new WebFingerError("Unknown schema");
}

String queryParams = "";
for (int i = 0; i < queryParamsTuple.size(); i++) {
queryParams += queryParamsTuple.get(i).getA() + "=" + queryParamsTuple.get(i).getB();
if (i != queryParamsTuple.size() - 1) {
queryParams += "&";
}
}

return String.format(WF_URL, host) + "?" + URLEncoder.encode(queryParams);
}

public String query(String resource) throws URISyntaxException, WebFingerError {
return query(resource, null);
}

public Map<String, Object> httpArgs(JRD jrd) throws JsonProcessingException {
if (jrd == null) {
if (this.jrd != null) {
jrd = this.jrd;
} else {
return null;
}
}

Map<String, String> hMap = new HashMap<String, String>() {{
put("Access-Control-Allow-Origin", "*");
put("Content-Type", "application/json; charset=UTF-8");
}};

Map<String, Object> headersAndBody = new HashMap<>();
headersAndBody.put("headers", hMap);
headersAndBody.put("body", jrd.toJSON());

return headersAndBody;
}
}