-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAuthorization.java
More file actions
139 lines (118 loc) · 4.96 KB
/
Authorization.java
File metadata and controls
139 lines (118 loc) · 4.96 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
package oiccli.service;
import com.auth0.jwt.creators.Message;
import com.google.common.base.Strings;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import oiccli.AuthorizationResponse;
import oiccli.StringUtil;
import oiccli.Tuple;
import oiccli.Utils;
import oiccli.client_info.ClientInfo;
public class Authorization extends service.Authorization {
private AuthorizationRequest authorizationRequest;
private AuthorizationResponse authorizationResponse;
private AuthorizationErrorResponse authorizationErrorResponse;
private Map<String, List<String>> defaultRequestArgs;
private List<>
public Authorization(Object httpLib, Object keyJar, Map<String, Class> clientAuthenticationMethod) {
super(httpLib, keyJar, clientAuthenticationMethod);
this.defaultRequestArgs = new HashMap() {{
put("scope", Arrays.asList("openId"));
}};
/*self.pre_construct = [self.oic_pre_construct]
self.post_construct = [self.oic_post_construct]*/
}
public List<Map<String, ?>> oicPreConstruct(ClientInfo clientInfo, Map<String, Object> requestArgs, Map<String, String> args) {
if (requestArgs != null) {
String responseType = (String) requestArgs.get("responseType");
if (responseType == null) {
requestArgs.put("responseType", clientInfo.getBehavior().get("responseTypes").get(0));
}
if (responseType.contains("token") || responseType.contains("idToken")) {
if (!requestArgs.containsKey("nonce")) {
requestArgs.put("nonce", StringUtil.generateRandomString(32));
}
}
} else {
requestArgs = new HashMap() {{
put("nonce", StringUtil.generateRandomString(32));
}};
}
Map<String, String> postArgs = new HashMap<>();
for (String attribute : Arrays.asList("requestObjectSigningAlg", "algorithm", "sigKid")) {
postArgs.put(attribute, args.get(attribute));
args.remove(attribute);
}
if (args.containsKey("requestMethod")) {
if (args.get("requestMethod").equals("reference")) {
postArgs.put("requestParam", "requestUri");
} else {
postArgs.put("requestParam", "request");
}
args.remove("requestMethod");
}
List<String> responseMode = clientInfo.getBehavior().get("responseMode");
if (responseMode.contains("formPost")) {
requestArgs.put("responseMode", responseMode);
}
if (!requestArgs.containsKey("state")) {
requestArgs.put("state", clientInfo.getStateDb().createState(clientInfo.getIssuer(), requestArgs));
}
return Arrays.asList(requestArgs, postArgs);
}
public Map<String, String> oicPostConstruct(ClientInfo clientInfo, Map<String, String> req, Map<String, String> args) {
String requestParam = args.get("requestParam");
args.remove("requestParam");
String algorithm = null;
for (String argument : Arrays.asList("requestObjectSigningAlg", "algorithm")) {
algorithm = args.get(argument);
}
if (algorithm == null) {
algorithm = clientInfo.getBehavior().get("requestObjectSigningAlg");
if (algorithm == null) {
algorithm = "RS256";
}
}
args.put("requestObjectSigningAlg", algorithm);
if (!args.containsKey("keys") && algorithm != null && !algorithm.equals("none")) {
String kty = StringUtil.alg2keytype(algorithm);
String kid = args.get("sigKid");
if (kid == null) {
kid = clientInfo.getKid().get("sig").get(kty);
}
args.put("keys", clientInfo.getKeyJar().getSigningKey(kty, kid));
}
/*
_req = make_openid_request(req, **kwargs)
*/
Message _req = Utils.requestObjectEncryption(_req, clientInfo, args);
String webName;
String fileName;
if (requestParam.equals("request")) {
req.put("request", _req);
} else {
webName = clientInfo.getRegistrationResponse().get("requestUris").get(0);
fileName = clientInfo.filenameFromWebname(webName);
if(Strings.isNullOrEmpty(fileName)) {
Tuple tuple = Utils.constructRequestUri(null, null, args);
webName = tuple.getA();
fileName = tuple.getB();
}
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(fileName));
writer.write(_req);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
req.put("requestUri", webName);
}
return req;
}
}