forked from GJWT/javaOIDCMsg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebFinger.java
More file actions
154 lines (136 loc) · 5.04 KB
/
WebFinger.java
File metadata and controls
154 lines (136 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package oiccli.webfinger;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Strings;
import com.google.gson.Gson;
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 oiccli.Tuple;
import oiccli.exceptions.MessageException;
import oiccli.exceptions.OicMsgError;
import oiccli.exceptions.WebFingerError;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class WebFinger {
public String defaultRelt;
public Object httpd;
private JRD jrd;
private List<Map<String, Object>> events;
private static final String WF_URL = "https://%s/.well-known/webfinger";
final private static Logger logger = LoggerFactory.getLogger(WebFinger.class);
private static final String OIC_ISSUER = "http://openid.net/specs/connect/1.0/issuer";
private static final Gson gson = new Gson();
public WebFinger(String defaultRelt, Object httpd) {
this.defaultRelt = defaultRelt;
this.httpd = httpd;
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 static JRD load(Map<String, Object> item) {
return new JRD(json.loads(item));
}
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;
}
public static Object linkDeser(Object val, String sFormat) {
if (val instanceof Map) {
return val;
} else if (sFormat.equals("dict") || sFormat.equals("json")) {
if (!(val instanceof String)) {
val = gson.toJson(val);
sFormat = "json";
}
}
return link().deserialize(val, sFormat);
}
public static Object messageSer(Object inst, String sFormat, int lev) throws MessageException, OicMsgError {
Object res;
if (sFormat.equals("urlencoded") || sFormat.equals("json")) {
if (inst instanceof Map) {
if (sFormat.equals("json")) {
res = gson.toJson(inst);
} else {
res = Base64.encodeBase64URLSafe()
}
}
//elif isinstance(inst, LINK):
//res = inst.serialize(sformat, lev)
else {
res = inst;
}
} else if (sFormat.equals("dict")) {
if (inst instanceof Map) {
res = inst.serialize(sFormat, lev);
} else if (inst instanceof String) {
res = inst;
} else {
throw new MessageException("Wrong type: " + inst.getClass());
}
} else {
throw new OicMsgError("Unknown sFormat" + inst);
}
return res;
}
}