-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa.java
More file actions
142 lines (111 loc) · 4.42 KB
/
Copy pathrsa.java
File metadata and controls
142 lines (111 loc) · 4.42 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
import org.apache.commons.io.IOUtils
import java.nio.charset.*
import java.security.SignatureException
import java.security.Signature
import java.security.KeyFactory
import java.security.NoSuchAlgorithmException
import java.security.PrivateKey
import java.security.spec.InvalidKeySpecException
import java.security.spec.PKCS8EncodedKeySpec
import java.security.spec.EncodedKeySpec
import java.util.Base64
import java.net.URLEncoder;
//import java.io.*
def flowFile = session.get()
if (!flowFile) return
def static rsa(String data, String privateKeyString) throws java.security.SignatureException
{
String result
try {
if(privateKeyString == null){
throw new Exception("PrivateKey should not be null ");
}
byte[] privateBytes = privateKeyString.decodeBase64();
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privateKey = kf.generatePrivate(keySpec);
Signature signer = Signature.getInstance("SHA1withRSA");
signer.initSign(privateKey);
signer.update(data.getBytes("UTF-8"));
byte[] rawRsa = signer.sign();
result= rawRsa.encodeBase64();
} catch (Exception e) {
throw new SignatureException("Failed to generate RSA error : " + e.getMessage());
}
return result
}
def static baseUrlString(String baseUrl, String urlArguments, String method, TreeMap data ){
String result
try{
TreeMap map = data;
if(urlArguments!=null){
// retrieve arguments of the target and split arguments
def arguments = urlArguments.tokenize('&');
for (String item : arguments) {
def (key, value) = item.tokenize('=')
map.put(key, value);
}
}
String parameterString = map.collect { String key, String value ->
"${key}=${URLEncoder.encode(value)}"
}.join("&");
String baseInfo = ""
baseInfo += method.toUpperCase()
baseInfo += '&'
baseInfo += URLEncoder.encode(baseUrl, "UTF-8");
baseInfo += '&'
baseInfo += URLEncoder.encode(parameterString, "UTF-8");
//encode the base string
result = baseInfo;
}catch(Exception e){
println("Error: "+e.getMessage());
}
return result;
}
def static authorizationHeader(TreeMap data ){
String result
try{
TreeMap map = data;
// String headerString = map.collect { String key, String value ->
// "${key}=${value}"}
// }.join(", ");
String parameterString="";
map.each { key, value ->
parameterString += key
parameterString += '="'
parameterString += URLEncoder.encode(value, "UTF-8")
parameterString += '", '
}
String oauthHeader = "OAuth "+parameterString;
//encode the base string
result = oauthHeader;
}catch(Exception e){
println("Error: "+e.getMessage());
}
return result;
}
def attributes = flowFile.getAttributes()
def method = attributes.method
def base_url = attributes.base_url
def arguments = attributes.arguments
def privateKeyString = attributes.privateKey
TreeMap map = [:]
map.put("oauth_consumer_key", attributes.oauth_consumer_key)
map.put("oauth_token", attributes.oauth_token)
map.put("oauth_signature_method", attributes.oauth_signature_method)
map.put("oauth_timestamp", attributes.oauth_timestamp)
map.put("oauth_nonce", attributes.oauth_nonce)
map.put("oauth_version", attributes.oauth_version)
map.put("oauth_verifier", attributes.oauth_verifier)
//call url base string
def baseInfo = baseUrlString(base_url, arguments, method, map);
//get signature key
String oauthSignature = rsa(baseInfo, privateKeyString)
//add signature key on authorization header
map.put("oauth_signature", oauthSignature);
//call authorization api
def oauth = authorizationHeader(map);
flowFile = session.putAttribute(flowFile, 'oauth_signature', oauthSignature)
flowFile = session.putAttribute(flowFile, "baseInfo", baseInfo)
flowFile = session.putAttribute(flowFile, "Authorization", oauth)
session.transfer(flowFile, REL_SUCCESS)