forked from GJWT/javaOIDCMsg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
80 lines (65 loc) · 2.72 KB
/
Utils.java
File metadata and controls
80 lines (65 loc) · 2.72 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
package oiccli;
import com.auth0.jwt.creators.Message;
import com.google.common.base.Strings;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import oiccli.client_info.ClientInfo;
import oiccli.exceptions.MissingRequiredAttribute;
public class Utils {
public static Message requestObjectEncryption(Message message, ClientInfo clientInfo, Map<String,Object> args) throws MissingRequiredAttribute {
String encryptionAlg = (String) args.get("requestObjectEncryptionAlg");
if(Strings.isNullOrEmpty(encryptionAlg)) {
List<String> listOfAlgs = clientInfo.getBehavior().get("requestObjectEncryptionAlg");
if(listOfAlgs != null || !listOfAlgs.isEmpty()) {
encryptionAlg = listOfAlgs.get(0);
}
if(encryptionAlg == null) {
return message;
}
}
String encryptionEnc = (String) args.get("requestObjectEncryptionEnc");
if(Strings.isNullOrEmpty(encryptionEnc)) {
List<String> listOfAlgs = clientInfo.getBehavior().get("requestObjectEncryptionEnc");
if(listOfAlgs != null || !listOfAlgs.isEmpty()) {
encryptionEnc = listOfAlgs.get(0);
}
if(encryptionEnc == null) {
throw new MissingRequiredAttribute("No requestObjectEncryptionEnc specified");
}
}
JWE jwe = new JWE(message, encryptionAlg, encryptionEnc);
String keyType = StringUtil.alg2keytype(encryptionAlg);
String kid = (String) args.get("encKid");
if(Strings.isNullOrEmpty(kid)) {
kid = "";
}
if(!args.containsKey("target")) {
throw new MissingRequiredAttribute("No target specified");
}
List<Key> keys;
if(!Strings.isNullOrEmpty(kid)) {
keys = clientInfo.getKeyJar().getEncryptKey(keyType, args.get("target"), kid);
jwe.setKid(kid);
} else {
keys = clientInfo.getKeyJar().getEncryptKey(keyType, args.get("target"));
}
return jwe.encrypt(keys);
}
public static Tuple constructRequestUri(String localDir, String basePath, Map<String,String> args) {
File file = new File(localDir);
if(!file.isDirectory()) {
file.mkdirs();
}
String name = StringUtil.generateRandomString(10) + ".jwt";
File fileExists = Paths.get(localDir, name).toFile();
while(fileExists.exists()) {
name = StringUtil.generateRandomString(10);
fileExists = Paths.get(localDir, name).toFile();
}
String webName = basePath + name;
return new Tuple(fileExists.toString(), webName);
}
}