Skip to content

Commit cea9297

Browse files
committed
Oicmsg
1 parent 861c4da commit cea9297

7 files changed

Lines changed: 121 additions & 45 deletions

File tree

lib/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ 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: 'com.google.guava', name: 'guava', version: 'r05'
4041
testCompile 'org.bouncycastle:bcprov-jdk15on:1.58'
4142
testCompile 'junit:junit:4.12'
4243
testCompile 'net.jodah:concurrentunit:0.4.3'
4344
testCompile 'org.hamcrest:java-hamcrest:2.0.0.0'
4445
testCompile 'org.mockito:mockito-core:2.11.0'
46+
testCompile group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.1'
4547
}
4648

4749
jacocoTestReport {

lib/src/main/java/com/auth0/jwt/oicmsg/Key.java

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.auth0.jwt.exceptions.oicmsg_exceptions.HeaderError;
44
import com.google.common.primitives.Bytes;
5+
import com.google.gson.Gson;
56
import com.nimbusds.jose.util.Base64;
7+
import org.bouncycastle.util.encoders.Base64;
68
import org.junit.Assert;
79
import org.slf4j.Logger;
810
import org.slf4j.LoggerFactory;
@@ -26,7 +28,7 @@ public class Key {
2628
private static Map<String, Object> longs = new HashMap<String, Object>();
2729
protected static Set<String> members = new HashSet<>(Arrays.asList("kty", "alg", "use", "kid", "x5c", "x5t", "x5u"));
2830
public static Set<String> publicMembers = new HashSet<>(Arrays.asList("kty", "alg", "use", "kid", "x5c", "x5t", "x5u"));
29-
protected static Set<String> required = new HashSet<>(Arrays.asList("kty"));
31+
protected static List<String> required = new ArrayList<>(Arrays.asList("kty"));
3032

3133
public Key(String kty, String alg, String use, String kid, String x5c, String x5t, String x5u, Key key, Map<String, String> args) {
3234
this.kty = kty;
@@ -121,7 +123,7 @@ public Map<String, String> toDict() {
121123
return hmap;
122124
}
123125

124-
public List<Key> serialize() {
126+
public Key serialize() {
125127
Map<String, String> hmap = common();
126128
this.key.
127129
//TODO
@@ -164,6 +166,7 @@ public boolean verify() throws HeaderError {
164166
}
165167

166168
if (item instanceof Bytes) {
169+
167170
//item = item.decode('utf-8') ???
168171
//TODO
169172
}
@@ -219,18 +222,60 @@ public List<String> getKeys() {
219222
return new ArrayList<>(this.toDict().keySet());
220223
}
221224

222-
public byte[] thumbprint(String hashFunction, List<String> members); //TODO
225+
public byte[] thumbprint(String hashFunction, List<String> members) {
226+
if(members == null || members.isEmpty()) {
227+
members = required;
228+
}
229+
230+
Collections.sort(members);
231+
Key key = this.serialize();
232+
String value = null;
233+
Map<String,String> hmap = new HashMap<>();
234+
for(String member : members) {
235+
try {
236+
value = key.getClass().getField(member).toString();
237+
} catch (NoSuchFieldException e) {
238+
logger.error(e.toString());
239+
}
240+
hmap.put(member, value);
241+
}
242+
243+
String json = new Gson().toJson(hmap);
244+
byte[] byteArr = null;
245+
switch (hashFunction) {
246+
case "SHA-256":
247+
byteArr = sha256_digest(json);
248+
break;
249+
case "SHA-384":
250+
byteArr = sha384_digest(json);
251+
break;
252+
case "SHA-512":
253+
byteArr = sha512_digest(json);
254+
break;
255+
default:
256+
throw new IllegalArgumentException("improper hash function");
257+
}
258+
259+
return byteArr;
260+
}
223261

224262
public byte[] thumbprint(String hashFunction) {
225263
thumbprint(hashFunction, null);
226264
}
227265

228266
public void addKid() {
229-
this.kid = Base64.encode(this.thumbprint("SHA-256")).decodeToString();
267+
this.kid = new String(Base64.encode(this.thumbprint("SHA-256")));
230268
}
231269

232270

233-
protected static void deser(Object item) {
271+
//https://stackoverflow.com/questions/5729806/encode-string-to-utf-8 can't encode string to utf-8
272+
/*protected static void deser(Object item) {
273+
if(item instanceof String) {
274+
item.en
275+
}
276+
277+
278+
234279
return base64ToLong(item);
235-
}
280+
}*/
236281
}

lib/src/main/java/com/auth0/jwt/oicmsg/KeyBundle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public KeyBundle(String source, String fileFormat, List<String> usage) throws Im
130130
public void doKeys(List<Key> keys) {
131131
for (Key keyIndex : keys) {
132132
final String kty = keyIndex.getKty();
133-
List<String> usage = harmonizeUsage(keyIndex.getUse());
133+
List<String> usage = harmonizeUsage(Arrays.asList(keyIndex.getUse()));
134134
keys.remove("use");
135135
boolean flag = false;
136136
for (String use : usage) {

lib/src/main/java/com/auth0/jwt/oicmsg/KeyJar.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.auth0.jwt.exceptions.oicmsg_exceptions.TypeError;
55
import com.auth0.jwt.impl.JWTParser;
66
import com.auth0.jwt.jwts.JWT;
7-
import com.google.common.base.Strings;
87
import org.junit.Assert;
98
import org.slf4j.LoggerFactory;
109

lib/src/main/java/com/auth0/jwt/oicmsg/RSAKey.java

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class RSAKey extends Key {
2121
private String dq;
2222
private String di;
2323
private String qi;
24-
private String key;
24+
private RSAKey key;
2525

2626
public RSAKey(String kty, String alg, String use, String kid, String x5c, String x5t, String x5u, Key key, String n,
2727
String e, String d, String p, String q, String dp, String dq, String di, String qi, Map<String, String> args) {
@@ -76,12 +76,12 @@ public void deserialize() throws DeserializationNotPossible {
7676
}
7777

7878
List<String> list = new ArrayList<>(Arrays.asList(this.n, this.e));
79-
if(this.d != null && !this.d.isEmpty()) {
79+
if (this.d != null && !this.d.isEmpty()) {
8080
list.add(this.d);
8181
}
82-
if(this.p != null && !this.p.isEmpty()) {
82+
if (this.p != null && !this.p.isEmpty()) {
8383
list.add(this.p);
84-
if(this.q != null && !this.q.isEmpty()) {
84+
if (this.q != null && !this.q.isEmpty()) {
8585
list.add(this.q);
8686
}
8787
this.key = RSA.construct(tuple(list)); //TODO
@@ -91,44 +91,44 @@ public void deserialize() throws DeserializationNotPossible {
9191
} else if (this.x5c != null) {
9292
Base64.decode((int) this.x5c.getBytes()[0]);
9393

94-
if(this.x5t != null) {
95-
if(Base64.decode() != )
94+
if (this.x5t != null) {
95+
if (Base64.decode() !=)
9696

9797
}
9898

9999
this.key =;
100100
this.split();
101-
if(this.x5c.length() > 1) {
101+
if (this.x5c.length() > 1) {
102102

103103
}
104104
} else {
105105
throw new DeserializationNotPossible();
106106
}
107107
}
108108

109-
public Map<String,String> serialize(boolean isPrivate) throws SerializationNotPossible {
110-
if(this.key == null) {
109+
public Map<String, String> serialize(boolean isPrivate) throws SerializationNotPossible {
110+
if (this.key == null) {
111111
throw new SerializationNotPossible();
112112
}
113113

114114
Map<String, String> args = common();
115115

116116
publicMembers.addAll(longs);
117117
List<String> publicLongs = new ArrayList<>(publicMembers);
118-
for(String param : publicLongs) {
118+
for (String param : publicLongs) {
119119
try {
120120
Object item = this.getClass().getField(param).get(this);
121-
if(item != null) {
121+
if (item != null) {
122122
args.put(param, longToBase64(item));
123123
}
124124
} catch (Exception e1) {
125125
logger.error("Field " + param + " doesn't exist");
126126
}
127127
}
128128

129-
if(isPrivate) {
130-
for(String param : longs) {
131-
if(!isPrivate && new ArrayList<>(Arrays.asList("d", "p", "q", "dp", "dq", "di",
129+
if (isPrivate) {
130+
for (String param : longs) {
131+
if (!isPrivate && new ArrayList<>(Arrays.asList("d", "p", "q", "dp", "dq", "di",
132132
"qi")).contains(param)) {
133133
continue;
134134
}
@@ -151,34 +151,31 @@ private void split() {
151151
this.n = this.key.n;
152152
this.e = this.key.e;
153153

154-
try {
155-
this.d = this.key.d;
156-
} catch (AttributeError e) {
157-
158-
} finally {
159-
Object item = null;
160-
for(String param : new ArrayList<>(Arrays.asList("p", "q"))) {
161-
try {
162-
item = this.getClass().getField(param).get(this);
163-
} catch (Exception e1) {
164-
logger.error("Field " + param + " doesn't exist");
165-
} finally {
166-
if(item != null) {
167-
//set attribute (which is in the form of a string) to a value
168-
}
154+
this.d = this.key.d;
155+
Object item = null;
156+
for (String param : new ArrayList<>(Arrays.asList("p", "q"))) {
157+
try {
158+
item = this.getClass().getField(param).get(this);
159+
} catch (Exception e1) {
160+
logger.error("Field " + param + " doesn't exist");
161+
} finally {
162+
if (item != null) {
163+
//set attribute (which is in the form of a string) to a value
169164
}
170165
}
171166
}
172167
}
173168

174-
public RSAKey loadKey(Key key) {
169+
}
170+
171+
public RSAKey loadKey(RSAKey key) {
175172
this.key = key;
176173
this.split();
177174
return key;
178175
}
179176

180177
public Key encryptionKey() {
181-
if(this.key == null) {
178+
if (this.key == null) {
182179
deserialize();
183180
}
184181

@@ -191,9 +188,4 @@ private String longToBase64(Object item) {
191188
public Map<String, String> serialize() {
192189
return serialize(false);
193190
}
194-
195-
private void split() {
196-
197-
}
198-
199191
}

lib/src/main/java/com/auth0/jwt/oicmsg/SYMKey.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.auth0.jwt.oicmsg;
22

33
import com.auth0.jwt.exceptions.oicmsg_exceptions.JWKException;
4+
import org.apache.commons.codec.Charsets;
5+
import org.apache.commons.codec.binary.Base64;
46
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
68

9+
import java.nio.charset.StandardCharsets;
710
import java.util.*;
811

912
public class SYMKey extends Key{
@@ -38,7 +41,8 @@ public void deserialize() {
3841

3942
public Map<String,String> serialize(boolean isPrivate) {
4043
Map<String,String> args = common();
41-
args.put("k", b64e(this.k.getBytes()).asUnicode());
44+
String k = Utils.urlSafeEncode(this.k);
45+
args.put("k", k);
4246
return args;
4347
}
4448

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.auth0.jwt.oicmsg;
2+
3+
import org.apache.commons.codec.binary.Base64;
4+
5+
public class Utils {
6+
7+
public static String urlSafeEncode(String value) {
8+
value = Base64.encodeBase64URLSafeString(value.getBytes());
9+
StringBuilder sb = new StringBuilder(value);
10+
for(int i = sb.length()-1; i >= 0; i--) {
11+
if(sb.charAt(i) == '=') {
12+
sb.deleteCharAt(i);
13+
} else {
14+
break;
15+
}
16+
}
17+
18+
return sb.toString();
19+
}
20+
21+
public static byte[] urlSafeDecode(String value) {
22+
byte[] stringToBytes = Base64.decodeBase64(value.getBytes());
23+
StringBuilder sb = new StringBuilder(new String(stringToBytes));
24+
for(int i = sb.length()-1; i >= 0; i--) {
25+
if(sb.charAt(i) == '=') {
26+
sb.deleteCharAt(i);
27+
} else {
28+
break;
29+
}
30+
}
31+
32+
return String.valueOf(sb).getBytes();
33+
}
34+
}

0 commit comments

Comments
 (0)