Skip to content

Commit ec1a552

Browse files
committed
Webfinger
1 parent b58d4ba commit ec1a552

4 files changed

Lines changed: 173 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package oicclient.exceptions;
2+
3+
public class WebFingerError extends Exception{
4+
5+
public WebFingerError(String message) {
6+
super(message);
7+
}
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package oicclient.tuples;
2+
3+
public class Tuple {
4+
5+
private Object a;
6+
private Object b;
7+
8+
public Tuple(Object a, Object b) {
9+
this.a = a;
10+
this.b = b;
11+
}
12+
13+
public Object getA() {
14+
return a;
15+
}
16+
17+
public Object getB() {
18+
return b;
19+
}
20+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package oicclient.webfinger;
2+
3+
public class URINormalizer {
4+
5+
private static boolean hasScheme(String path) {
6+
if (path.contains("://")) {
7+
return true;
8+
} else {
9+
String authority = path.replace("/", "#")
10+
.replace("?", "#").split("#")[0];
11+
12+
String hostOrPort;
13+
if (authority.contains(":")) {
14+
hostOrPort = authority.split(":", 1)[1];
15+
if (hostOrPort.matches("^\\d+$")) {
16+
return false;
17+
}
18+
} else {
19+
return false;
20+
}
21+
}
22+
23+
return true;
24+
}
25+
26+
private static boolean isAccountSchemeAssumed(String path) {
27+
String[] arr;
28+
if (path.contains("@")) {
29+
arr = path.split("@");
30+
String host = arr[arr.length - 1];
31+
return !(host.contains(":") || host.contains("/") || host.contains("?"));
32+
} else {
33+
return false;
34+
}
35+
}
36+
37+
String normalize(String path) {
38+
if (!hasScheme(path)) {
39+
if (isAccountSchemeAssumed(path)) {
40+
path = "acct:" + path;
41+
} else {
42+
path = "https://" + path;
43+
}
44+
}
45+
46+
return path.split("#")[0];
47+
}
48+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package oicclient.webfinger;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.google.common.base.Strings;
5+
import java.net.URI;
6+
import java.net.URISyntaxException;
7+
import java.net.URLEncoder;
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
import oicclient.exceptions.WebFingerError;
14+
import oicclient.tuples.Tuple;
15+
16+
public class WebFinger {
17+
public String defaultRelt;
18+
private JRD jrd;
19+
private List<Map<String, Object>> events;
20+
private static final String WF_URL = "https://%s/.well-known/webfinger";
21+
22+
public WebFinger(String defaultRelt) {
23+
this.defaultRelt = defaultRelt;
24+
this.jrd = null;
25+
this.events = new ArrayList<>();
26+
}
27+
28+
public String query(String resource, List<String> rel) throws URISyntaxException, WebFingerError {
29+
resource = new URINormalizer().normalize(resource);
30+
List<Tuple> queryParamsTuple = new ArrayList<>(Arrays.asList(new Tuple("resource", resource)));
31+
32+
if (rel == null) {
33+
if (!Strings.isNullOrEmpty(this.defaultRelt)) {
34+
queryParamsTuple.add(new Tuple("rel", this.defaultRelt));
35+
}
36+
} else {
37+
for (String index : rel) {
38+
queryParamsTuple.add(new Tuple("rel", index));
39+
}
40+
}
41+
42+
String host;
43+
if (resource.startsWith("http")) {
44+
URI uri = new URI(resource);
45+
host = uri.getHost();
46+
int port = uri.getPort();
47+
if (port != -1) {
48+
host += ":" + port;
49+
}
50+
} else if (resource.startsWith("acct:")) {
51+
String[] arr = resource.split("@");
52+
host = arr[arr.length - 1];
53+
arr = host.replace("/", "#").replace("?", "#").split("#");
54+
host = arr[0];
55+
} else if (resource.startsWith("device:")) {
56+
String[] arr = resource.split(":");
57+
host = arr[1];
58+
} else {
59+
throw new WebFingerError("Unknown schema");
60+
}
61+
62+
String queryParams = "";
63+
for (int i = 0; i < queryParamsTuple.size(); i++) {
64+
queryParams += queryParamsTuple.get(i).getA() + "=" + queryParamsTuple.get(i).getB();
65+
if (i != queryParamsTuple.size() - 1) {
66+
queryParams += "&";
67+
}
68+
}
69+
70+
return String.format(WF_URL, host) + "?" + URLEncoder.encode(queryParams);
71+
}
72+
73+
public String query(String resource) throws URISyntaxException, WebFingerError {
74+
return query(resource, null);
75+
}
76+
77+
public Map<String, Object> httpArgs(JRD jrd) throws JsonProcessingException {
78+
if (jrd == null) {
79+
if (this.jrd != null) {
80+
jrd = this.jrd;
81+
} else {
82+
return null;
83+
}
84+
}
85+
86+
Map<String, String> hMap = new HashMap<String, String>() {{
87+
put("Access-Control-Allow-Origin", "*");
88+
put("Content-Type", "application/json; charset=UTF-8");
89+
}};
90+
91+
Map<String, Object> headersAndBody = new HashMap<>();
92+
headersAndBody.put("headers", hMap);
93+
headersAndBody.put("body", jrd.toJSON());
94+
95+
return headersAndBody;
96+
}
97+
}

0 commit comments

Comments
 (0)