Skip to content

Commit a9ca962

Browse files
committed
Message/AuthorizationResponse/AuthorizationErrorResponse
1 parent b58d4ba commit a9ca962

27 files changed

Lines changed: 1025 additions & 0 deletions

lib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies {
3737
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.2'
3838
compile 'commons-codec:commons-codec:1.11'
3939
compile 'com.google.code.gson:gson:2.8.2'
40+
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5'
4041
testCompile 'org.bouncycastle:bcprov-jdk15on:1.58'
4142
testCompile 'junit:junit:4.12'
4243
testCompile 'net.jodah:concurrentunit:0.4.3'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.auth0.jwt.oicmsg;
2+
3+
public class JWEHeader {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.auth0.jwt.oicmsg;
2+
3+
public class JWSHeader {
4+
}
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
package com.auth0.jwt.oicmsg;
2+
3+
import com.auth0.jwt.jwts.JWT;
4+
import com.auth0.jwt.oicmsg.exceptions.MessageException;
5+
import com.auth0.jwt.oicmsg.exceptions.MissingRequiredAttribute;
6+
import com.auth0.jwt.oicmsg.exceptions.OicMsgError;
7+
import com.auth0.jwt.oicmsg.exceptions.TooManyValues;
8+
import com.google.common.base.Joiner;
9+
import java.io.UnsupportedEncodingException;
10+
import java.net.URI;
11+
import java.net.URISyntaxException;
12+
import java.net.URLEncoder;
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
15+
import java.util.Collection;
16+
import java.util.HashMap;
17+
import java.util.List;
18+
import java.util.Map;
19+
import org.omg.CORBA.NameValuePair;
20+
21+
public class Message {
22+
23+
private Map<String, Object> dict;
24+
private JWT jwt;
25+
private JWSHeader jwsHeader;
26+
private JWEHeader jweHeader;
27+
private boolean lax;
28+
private boolean shouldVerifySSL;
29+
public Map<String,Tuple5> claims;
30+
private Map<String,Object> cDefault;
31+
public Map<String,List> cAllowedValues;
32+
public static Tuple5 SINGLE_REQUIRED_STRING = new Tuple5(String.class, true, null, null, false);
33+
public static Tuple5 SINGLE_OPTIONAL_STRING = new Tuple5(String.class, false, null, null, false);
34+
public static Tuple5 REQUIRED_LIST_OF_STRINGS = new Tuple5(Arrays.asList(String.class), true, listSerializer, listDeserializer, false);
35+
public static Tuple5 OPTIONAL_LIST_OF_STRINGS = new Tuple5(Arrays.asList(String.class), false, listSerializer, listDeserializer, false);
36+
public static Tuple5 SINGLE_OPTIONAL_BOOLEAN = new Tuple5(Arrays.asList(Boolean.class), false, null, null, false);
37+
public static Tuple5 OPTIONAL_ADDRESS = new Tuple5(Message.class, false, msgSer, addressDer, false);
38+
public static Tuple5 OPTIONAL_MESSAGE = new Tuple5(Message.class, false, msgSer, msgDer, false);
39+
public static Tuple5 SINGLE_OPTIONAL_INT = new Tuple5(Integer.class, false, null, null, false);
40+
public static Tuple5 SINGLE_REQUIRED_INT = new Tuple5(Integer.class, true, null, null, false);
41+
public static Tuple5 OPTIONAL_LIST_OF_SP_SEP_STRINGS = new Tuple5(Arrays.asList(String.class), false, spSepListSerializer, spSepListDeserializer, false);
42+
43+
public Message(Map<String,Object> kwargs) {
44+
this.dict = new HashMap<>(cDefault);
45+
this.lax = false;
46+
this.jwt = null;
47+
this.jwsHeader = null;
48+
this.jweHeader = null;
49+
this.fromDict(kwargs);
50+
this.shouldVerifySSL = true;
51+
}
52+
53+
public Message() {
54+
}
55+
56+
public String toUrlEncoded(int lev) throws MissingRequiredAttribute, UnsupportedEncodingException {
57+
if(!this.lax) {
58+
Tuple5 tuple5;
59+
for(String key : this.claims.keySet()) {
60+
tuple5 = claims.get(key);
61+
if(tuple5.getVRequired() && !this.cDefault.containsKey(key)) {
62+
throw new MissingRequiredAttribute("missing attribute: " + key);
63+
}
64+
}
65+
}
66+
67+
List<Tuple> params = new ArrayList<>();
68+
String[] splitArgs;
69+
Tuple5 tuple5 = new Tuple5();
70+
for(String key : this.cDefault.keySet()) {
71+
if(!cDefault.containsKey(key)) {
72+
if(key.contains("#")) {
73+
splitArgs = key.split("#");
74+
tuple5 = claims.get(splitArgs[0]);
75+
} else if(key.contains("*")){
76+
tuple5 = claims.get("*");
77+
} else {
78+
tuple5.setVSer(null);
79+
tuple5.setVNullAllowed(false);
80+
}
81+
}
82+
83+
Object value = cDefault.get(key);
84+
if(value == null && !tuple5.getVNullAllowed()) {
85+
continue;
86+
} else if(value instanceof String) {
87+
params.add(new Tuple(key, (String) value));
88+
} else if(value instanceof List) {
89+
if(tuple5.getVSer() != null) {
90+
params.add(new Tuple(key, ser(value, "urlencoded", lev)));
91+
} else {
92+
List list = (List) value;
93+
for(Object item : list) {
94+
params.add(new Tuple(key, (String) item));
95+
}
96+
}
97+
} else if(value instanceof Message) {
98+
value = json.dumps(ser(value, "dict", lev+1));
99+
params.add(new Tuple(key, value));
100+
} else if(value == null) {
101+
params.add(new Tuple(key, value));
102+
} else {
103+
params.add(new Tuple(key, ser(value, lev)));
104+
}
105+
}
106+
return urlencode(params);
107+
}
108+
109+
public Message fromUrlEncoded(List<String> urlEncoded) throws URISyntaxException, TooManyValues {
110+
String urlEncodedString = urlEncoded.get(0);
111+
112+
List<NameValuePair> params = URLEncodedUtils.parse(new URI(urlEncodedString), "UTF-8");
113+
114+
Tuple5 tuple5;
115+
String[] splitArgs;
116+
String key;
117+
for(NameValuePair param : params) {
118+
key = param.id;
119+
tuple5 = this.claims.get(key);
120+
if(tuple5 == null) {
121+
if(key.contains("#")) {
122+
splitArgs = key.split("#");
123+
tuple5 = claims.get(splitArgs[0]);
124+
} else if(key.contains("*")){
125+
tuple5 = claims.get("*");
126+
} else {
127+
if(((List) param.value).size() == 1) {
128+
cDefault.put(key, ((List) param.value).get(0));
129+
continue;
130+
}
131+
}
132+
133+
}
134+
135+
if(tuple5.getvType() != null && tuple5.getvType() == List.class) {
136+
if(tuple5.getVDSer() != null) {
137+
this.cDefault.put(key, tuple5.getVDSer(((List) param.value).get(0), "urlencoded"));
138+
} else {
139+
this.cDefault.put(key, ((List) param.value));
140+
}
141+
} else {
142+
Object value = ((List) param.value).get(0);
143+
if(((List) param.value).size() == 1) {
144+
if(tuple5.getVDSer() != null) {
145+
this.cDefault.put(key, tuple5.getVDSer(value, "urlencoded"));
146+
} else if(value instanceof tuple5.getvType()) {
147+
this.cDefault.put(key, value);
148+
}
149+
} else {
150+
throw new TooManyValues(key);
151+
}
152+
}
153+
}
154+
155+
return this;
156+
}
157+
158+
public Object msgSer(Object inst, String sFormat, int lev) throws MessageException, OicMsgError {
159+
List<String> sFormats = Arrays.asList("urlencoded", "json");
160+
Object res;
161+
if(sFormats.contains(sFormat)) {
162+
if(inst instanceof Map) {
163+
if(sFormat.equals("json")) {
164+
res = json.dumps(inst);
165+
} else {
166+
Map<String,String> map = (Map<String, String>) inst;
167+
for(String key : map.keySet()) {
168+
map.put(URLEncoder.encode(key), URLEncoder.encode(map.get(key)));
169+
}
170+
res = map;
171+
}
172+
} else if(inst instanceof Message) {
173+
res = ((Message) inst).serialize(sFormat, lev);
174+
} else {
175+
res = inst;
176+
}
177+
} else if(sFormat.equals("dict")) {
178+
if(inst instanceof Message) {
179+
res = ((Message) inst).serialize(sFormat, lev);
180+
} else if(inst instanceof Map || inst instanceof String) {
181+
res = inst;
182+
} else {
183+
throw new MessageException("Wrong type: " + inst.getClass());
184+
}
185+
} else {
186+
throw new OicMsgError("Unknown sFormat: " + inst);
187+
}
188+
189+
return res;
190+
}
191+
192+
private String urlencode(List<Tuple> params) throws UnsupportedEncodingException {
193+
StringBuilder sb = new StringBuilder();
194+
for(Tuple param : params){
195+
if(sb.length() > 0){
196+
sb.append('&');
197+
}
198+
sb.append(URLEncoder.encode((String) param.getA(), "UTF-8")).append('=').append(URLEncoder.encode((String) param.getB(), "UTF-8"));
199+
}
200+
201+
return sb.toString();
202+
}
203+
204+
public Collection<Object> getValues() {
205+
return this.dict.values();
206+
}
207+
208+
public Map<String, String> serialize(String sFormat, int lev) {
209+
return null;
210+
}
211+
212+
public Map<String, Tuple5> getClaims() {
213+
return claims;
214+
}
215+
216+
public void updateClaims(Map<String, Tuple5> claims) {
217+
for(String key : claims.keySet()) {
218+
this.claims.put(key, claims.get(key));
219+
}
220+
}
221+
222+
public void setClaims(Map<String, Tuple5> claims) {
223+
this.claims = claims;
224+
}
225+
226+
public Map<String, List> getcAllowedValues() {
227+
return cAllowedValues;
228+
}
229+
230+
public void setcAllowedValues(Map<String, List> cAllowedValues) {
231+
this.cAllowedValues = cAllowedValues;
232+
}
233+
234+
public void updatecAllowedValues(Map<String, List> claims) {
235+
for(String key : claims.keySet()) {
236+
this.cAllowedValues.put(key, claims.get(key));
237+
}
238+
}
239+
240+
private void fromDict(Map<String, Object> kwargs) {
241+
}
242+
243+
public Map<String,Object> getDict() {
244+
return dict;
245+
}
246+
247+
public void addDict(Map<String, Object> claims) {
248+
for(String key : claims.keySet()) {
249+
this.dict.put(key, claims.get(key));
250+
}
251+
}
252+
253+
public void setDict(Map<String, Object> claims) {
254+
this.dict = claims;
255+
}
256+
257+
public static JWT toJWT(Key key, String algorithm, int lev) {
258+
}
259+
260+
public boolean verify(Map<String,Object> kwargs) throws Exception {
261+
262+
}
263+
264+
public String spSepListSerializer(List<String> vals) {
265+
if(vals != null && vals.size() == 1) {
266+
return vals.get(0);
267+
} else {
268+
Joiner joiner = Joiner.on(" ").skipNulls();
269+
return joiner.join(vals);
270+
}
271+
}
272+
273+
public List<String> spSepListDeserializer(List<String> vals) {
274+
if(vals != null && vals.size() == 1) {
275+
return Arrays.asList(vals.get(0).split(" "));
276+
} else {
277+
return vals;
278+
}
279+
}
280+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.auth0.jwt.oicmsg;
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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.auth0.jwt.oicmsg;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class Tuple5 {
7+
8+
private List<Class<String>> vTypeList;
9+
private Class<String> vType;
10+
private boolean vRequired;
11+
private Object vSer;
12+
private Object vDSer;
13+
private boolean vNullAllowed;
14+
15+
public Tuple5(List<Class<String>> vTypeList, boolean vRequired, Object vSer, Object vDSer, boolean vNullAllowed) {
16+
this.vTypeList = vTypeList;
17+
this.vRequired = vRequired;
18+
this.vSer = vSer;
19+
this.vDSer = vDSer;
20+
this.vNullAllowed = vNullAllowed;
21+
}
22+
23+
public Tuple5(Class<String> vType, boolean vRequired, Object vSer, Object vDSer, boolean vNullAllowed) {
24+
this(Arrays.asList((Class<String>) null), vRequired, vSer, vDSer, vNullAllowed);
25+
this.vType = vType;
26+
}
27+
28+
public List<Class<String>> getvTypeList() {
29+
return vTypeList;
30+
}
31+
32+
public void setvTypeList(List<Class<String>> vTypeList) {
33+
this.vTypeList = vTypeList;
34+
}
35+
36+
public Class<String> getvType() {
37+
return vType;
38+
}
39+
40+
public void setvType(Class<String> vType) {
41+
this.vType = vType;
42+
}
43+
44+
public boolean getVRequired() {
45+
return vRequired;
46+
}
47+
48+
public void setVRequired(boolean vRequired) {
49+
this.vRequired = vRequired;
50+
}
51+
52+
public Object getVSer() {
53+
return vSer;
54+
}
55+
56+
public void setVSer(Object vSer) {
57+
this.vSer = vSer;
58+
}
59+
60+
public Object getVDSer() {
61+
return vDSer;
62+
}
63+
64+
public void setVDSer(Object vDSer) {
65+
this.vDSer = vDSer;
66+
}
67+
68+
public boolean getVNullAllowed() {
69+
return vNullAllowed;
70+
}
71+
72+
public void setVNullAllowed(boolean vNullAllowed) {
73+
this.vNullAllowed = vNullAllowed;
74+
}
75+
}

0 commit comments

Comments
 (0)