forked from jesperfj/force-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auth.java
245 lines (226 loc) · 9.72 KB
/
Auth.java
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package com.force.api;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.force.api.http.Http;
import com.force.api.http.HttpRequest;
import com.force.api.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Auth {
private static final ObjectMapper jsonMapper = new ObjectMapper();
private static final Logger logger = LoggerFactory.getLogger(Auth.class);
static public final ApiSession oauthLoginPasswordFlow(ApiConfig c) {
if(c.getClientId()==null) throw new IllegalStateException("clientId cannot be null");
if(c.getClientSecret()==null) throw new IllegalStateException("clientSecret cannot be null");
if(c.getUsername()==null) throw new IllegalStateException("username cannot be null");
if(c.getPassword()==null) throw new IllegalStateException("password cannot be null");
try {
@SuppressWarnings("unchecked")
HttpResponse r = Http.send(HttpRequest.formPost()
.url(c.getLoginEndpoint()+"/services/oauth2/token")
.header("Accept","application/json")
.param("grant_type","password")
.param("client_id",c.getClientId())
.param("client_secret", c.getClientSecret())
.param("username",c.getUsername())
.param("password",c.getPassword())
);
if(r.getResponseCode()!=200) {
throw new AuthException(r.getResponseCode(),"Auth.oauthLoginPasswordFlow failed: "+r.getString());
} else {
Map<String, Object> resp = jsonMapper.readValue(r.getStream(), Map.class);
return new ApiSession((String) resp.get("access_token"), (String) resp.get("instance_url"));
}
} catch (JsonParseException e) {
throw new RuntimeException(e);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static private final String escapeXml(String s) {
StringBuffer sb = new StringBuffer();
for(int i=0;i<s.length();i++) {
char c = s.charAt(i);
switch(c) {
case '&': sb.append("&"); break;
case '>': sb.append(">"); break;
case '<': sb.append("<"); break;
case '\'': sb.append("'"); break;
case '\"': sb.append("""); break;
default: sb.append((char) c);
}
}
return sb.toString();
}
static public final ApiSession soaploginPasswordFlow(ApiConfig c) {
if(c.getUsername()==null) throw new IllegalStateException("username cannot be null");
if(c.getPassword()==null) throw new IllegalStateException("password cannot be null");
try {
URL url = new URL(c.getLoginEndpoint()+"/services/Soap/u/33.0");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.addRequestProperty("Content-Type", "text/xml");
conn.addRequestProperty("SOAPAction", "login");
OutputStream out = conn.getOutputStream();
byte[] msg = new String(
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"+
"<env:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"+
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"+
" xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"+
" <env:Body>\n"+
" <n1:login xmlns:n1=\"urn:partner.soap.sforce.com\">\n"+
" <n1:username>"+escapeXml(c.getUsername())+"</n1:username>\n"+
" <n1:password>"+escapeXml(c.getPassword())+"</n1:password>\n"+
" </n1:login>\n"+
" </env:Body>\n"+
"</env:Envelope>\n").getBytes("UTF-8");
out.write(msg);
out.flush();
if(conn.getResponseCode()!=200) {
throw new AuthException(conn.getResponseCode(), "soapLoginPasswordFlow failed");
}
InputStream in = conn.getInputStream();
StringBuilder b = new StringBuilder();
byte[] buf = new byte[2000];
int n = 0;
while((n=in.read(buf))!=-1) {
b.append(new String(buf,0,n));
}
String s = b.toString();
//System.out.println(s);
String accessToken = s.replaceAll("^.*<sessionId>(.*)</sessionId>.*$","$1").trim();
String apiEndpoint = "https://"+s.replaceAll("^.*<serverUrl>.*https://([^/]*)/.*</serverUrl>.*$","$1").trim();
//String organizationId = s.replaceAll("^.*<organizationId>(.*)</organizationId>.*$","$1").trim();
//String userId = s.replaceAll("^.*<userId>(.*)</userId>.*$","$1").trim();
//System.out.println("accessToken:"+accessToken);
//System.out.println("apiEndpoint: "+apiEndpoint);
//System.out.println("userId: "+userId);
//System.out.println("organizationId: "+organizationId);
return new ApiSession(accessToken, apiEndpoint);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static public final String startOAuthWebServerFlow(AuthorizationRequest req) {
if(req.apiConfig.getClientId()==null) throw new IllegalStateException("clientId cannot be null");
if(req.apiConfig.getRedirectURI()==null) throw new IllegalStateException("redirectURI cannot be null");
try {
return req.apiConfig.getLoginEndpoint()+
"/services/oauth2/authorize"+
"?response_type=code"+
"&client_id="+URLEncoder.encode(req.apiConfig.getClientId(),"UTF-8")+
(req.scope!=null ? "&scope="+URLEncoder.encode(req.scope.toString(),"UTF-8") : "") +
"&redirect_uri="+URLEncoder.encode(req.apiConfig.getRedirectURI(),"UTF-8")+
(req.state!=null ? "&state="+URLEncoder.encode(req.state,"UTF-8") : "") +
(req.immediate ? "&immediate=true" : "") +
(req.display!=null ? "&display="+req.display : "");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
static public final ApiSession completeOAuthWebServerFlow(AuthorizationResponse res) {
if(res.apiConfig.getClientId()==null) throw new IllegalStateException("clientId cannot be null");
if(res.apiConfig.getClientSecret()==null) throw new IllegalStateException("clientSecret cannot be null");
if(res.apiConfig.getRedirectURI()==null) throw new IllegalStateException("redirectURI cannot be null");
if(res.code==null) throw new IllegalStateException("code cannot be null");
try {
HttpResponse r = Http.send(HttpRequest.formPost()
.url(res.apiConfig.getLoginEndpoint()+"/services/oauth2/token")
.header("Accept","application/json")
.param("grant_type","authorization_code")
.param("client_id",res.apiConfig.getClientId())
.param("client_secret", res.apiConfig.getClientSecret())
.param("redirect_uri",res.apiConfig.getRedirectURI())
.preEncodedParam("code",res.code)
);
if(r.getResponseCode()!=200) {
throw new AuthException(r.getResponseCode(),r.getString());
} else {
Map<?, ?> resp = jsonMapper.readValue(r.getStream(), Map.class);
return new ApiSession()
.setRefreshToken((String) resp.get("refresh_token"))
.setAccessToken((String) resp.get("access_token"))
.setApiEndpoint((String) resp.get("instance_url"));
}
} catch (JsonParseException e) {
throw new RuntimeException(e);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static public final ApiSession refreshOauthTokenFlow(ApiConfig config, String refreshToken) {
if(config.getClientId()==null) throw new IllegalStateException("clientId cannot be null");
if(config.getClientSecret()==null) throw new IllegalStateException("clientSecret cannot be null");
try {
HttpResponse r = Http.send(HttpRequest.formPost()
.url(config.getLoginEndpoint()+"/services/oauth2/token")
.header("Accept","application/json")
.param("grant_type","refresh_token")
.param("client_id",config.getClientId())
.param("client_secret", config.getClientSecret())
.param("refresh_token", refreshToken)
);
if(r.getResponseCode()!=200) {
throw new AuthException(r.getResponseCode(),r.getString());
} else {
Map<?, ?> resp = jsonMapper.readValue(r.getStream(), Map.class);
return new ApiSession()
.setAccessToken((String) resp.get("access_token"))
.setApiEndpoint((String) resp.get("instance_url"))
.setRefreshToken(refreshToken);
}
} catch (JsonParseException e) {
throw new RuntimeException(e);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* revokes a token. Works with both access token and refresh token
* @param config appropriate ApiConfig
* @param token either an access token or a refresh token
*/
static public void revokeToken(ApiConfig config, String token) {
try {
Http.send(HttpRequest.formPost()
.header("Accept","*/*")
.url(config.getLoginEndpoint()+"/services/oauth2/revoke")
.param("token", token));
} catch(Throwable t) {
// Looks like revoke endpoint closes stream when trying to revoke
// an already revoked token. It doesn't return an error code. So
// we'll have to just catch it here and fake the code.
throw new AuthException(404, "Token could not be revoked. Most likely it has already expired or been revoked.");
}
}
static public final ApiSession authenticate(ApiConfig c) {
if(c.getUsername()!=null && c.getPassword()!=null && c.getClientId()!=null && c.getClientSecret()!=null) {
// username/password oauth flow
return oauthLoginPasswordFlow(c);
}
else if(c.getUsername()!=null && c.getPassword()!=null) {
return soaploginPasswordFlow(c);
}
return null;
}
}