forked from membrane/api-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuth2TrustedClient.java
More file actions
94 lines (72 loc) · 3.54 KB
/
OAuth2TrustedClient.java
File metadata and controls
94 lines (72 loc) · 3.54 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
/*
* Copyright 2016 predic8 GmbH, www.predic8.com
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Pattern;
public class OAuth2TrustedClient {
static String clientId = "abc";
static String clientSecret = "def";
static String tokenEndpoint = "http://localhost:700//oauth2/token";
static String target = "http://localhost:2000";
static String username = "john";
static String password = "password";
public static void main(String[] args) throws Exception {
System.out.println(sendRequestToTarget(getToken()));
}
private static String sendRequestToTarget(String authorizationHeaderValue) throws Exception {
HttpURLConnection connection = (HttpURLConnection) new URL(target).openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Authorization", authorizationHeaderValue);
return connection.getResponseMessage();
}
private static String getToken() throws Exception {
return parseTokenRequestResponse(getTokenRequestResponse());
}
private static String parseTokenRequestResponse(String tokenRequestResponse) {
// the parsing is done by removing unnecessary parts from the left side until token and token_type is reached
String temp = tokenRequestResponse.replaceFirst(Pattern.quote("{\"access_token\":\""),"");
String token = temp.split(Pattern.quote("\""))[0];
temp = temp.replaceFirst(Pattern.quote(token + "\",\"token_type\":\""),"");
String tokenType = temp.split(Pattern.quote("\""))[0];
return tokenType + " " + token;
}
private static String getTokenRequestResponse() throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(tokenEndpoint).openConnection();
connection.setRequestMethod("POST");
sendPostData(connection, createTokenRequestParameters());
return readResponse(connection);
}
private static String readResponse(HttpURLConnection connection) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while((line = in.readLine()) != null)
response.append(line);
in.close();
return response.toString();
}
private static void sendPostData(HttpURLConnection connection, String urlParameters) throws IOException {
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
}
private static String createTokenRequestParameters() {
return "grant_type=password&username=" + username + "&password=" + password + "&client_id=" + clientId + "&client_secret=" + clientSecret;
}
}