-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBIDOauth2.java
More file actions
173 lines (134 loc) · 5.92 KB
/
Copy pathBIDOauth2.java
File metadata and controls
173 lines (134 loc) · 5.92 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
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
/**
* Copyright (c) 2018, 1Kosmos Inc. All rights reserved.
* Licensed under 1Kosmos Open Source Public License version 1.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of this license at
* https://github.com/1Kosmos/1Kosmos_License/blob/main/LICENSE.txt
*/
package com.bidsdk;
import com.bidsdk.model.*;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BIDOauth2 {
public static Map<String, String> getQueryMap(String query) {
String[] params = query.split("&");
Map<String, String> map = new HashMap<String, String>();
for (String param : params) {
String name = param.split("=")[0];
String value = param.split("=").length > 1 ? param.split("=")[1] : "";
map.put(name, value);
}
return map;
}
public static BIDAuthorizationCodeResponse requestAuthorizationCode(BIDTenantInfo tenantInfo,
String proofOfAuthenticationJwt, String clientId, String responseType, String scope, String redirectUri,
String stateOrNull, String nonceOrNull) {
BIDAuthorizationCodeResponse ret = new BIDAuthorizationCodeResponse();
try {
BIDCommunityInfo communityInfo = BIDTenant.getInstance().getCommunityInfo(tenantInfo);
BIDSD sd = BIDTenant.getInstance().getSD(tenantInfo);
String serviceUrl = sd.oauth2 + "/community/" + communityInfo.community.name + "/v1/authorize";
HttpClient httpclient = HttpClientBuilder.create().disableRedirectHandling().build();
URIBuilder builder = new URIBuilder(serviceUrl);
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
request.setHeader("charset", "utf-8");
request.setHeader("Accept", "application/json");
request.setHeader("Connection", "keep-alive");
List<NameValuePair> body = new ArrayList<NameValuePair>();
body.add(new BasicNameValuePair("client_id", clientId));
body.add(new BasicNameValuePair("response_type", responseType));
body.add(new BasicNameValuePair("scope", scope));
body.add(new BasicNameValuePair("redirect_uri", redirectUri));
body.add(new BasicNameValuePair("proof_of_authentication_jwt", proofOfAuthenticationJwt));
if (stateOrNull != null) {
body.add(new BasicNameValuePair("state", stateOrNull));
}
if (nonceOrNull != null) {
body.add(new BasicNameValuePair("nonce", nonceOrNull));
}
request.setEntity(new UrlEncodedFormEntity(body));
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
String responseStr = (entity != null) ? EntityUtils.toString(entity).trim() : null;
if (statusCode != 200 && statusCode != 303) {
ret = new Gson().fromJson(responseStr, BIDAuthorizationCodeResponse.class);
return ret;
}
String location = response.getFirstHeader("Location") != null
? response.getFirstHeader("Location").getValue()
: null;
String decodedUrl = URLDecoder.decode(location, "UTF-8");
URL locationUrl = new URL(decodedUrl);
Map<String, String> queryData = getQueryMap(locationUrl.getQuery());
if (queryData.get("error") != null) {
ret.statusCode = 400;
ret.message = queryData.get("error_description");
return ret;
}
ret.statusCode = statusCode;
ret.url = location;
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public static BIDTokenResponse requestToken(BIDTenantInfo tenantInfo, String clientId, String clientSecret,
String grantType, String redirectUri, String codeOrNull, String refreshTokenOrNull) {
BIDTokenResponse ret = null;
try {
BIDCommunityInfo communityInfo = BIDTenant.getInstance().getCommunityInfo(tenantInfo);
BIDSD sd = BIDTenant.getInstance().getSD(tenantInfo);
String serviceUrl = sd.oauth2 + "/community/" + communityInfo.community.name + "/v1/token";
HttpClient httpclient = HttpClientBuilder.create().disableRedirectHandling().build();
URIBuilder builder = new URIBuilder(serviceUrl);
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
String authString = clientId + ":" + clientSecret;
byte[] authBytes = authString.getBytes();
String authEncoded = Base64.getEncoder().encodeToString(authBytes);
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
request.setHeader("charset", "utf-8");
request.setHeader("Authorization", "Basic " + authEncoded);
request.setHeader("Connection", "keep-alive");
List<NameValuePair> body = new ArrayList<NameValuePair>();
body.add(new BasicNameValuePair("grant_type", grantType));
body.add(new BasicNameValuePair("redirect_uri", redirectUri));
if (codeOrNull != null) {
body.add(new BasicNameValuePair("code", codeOrNull));
}
if (refreshTokenOrNull != null) {
body.add(new BasicNameValuePair("refresh_token", refreshTokenOrNull));
}
request.setEntity(new UrlEncodedFormEntity(body));
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
String responseStr = (entity != null) ? EntityUtils.toString(entity).trim() : null;
ret = new Gson().fromJson(responseStr, BIDTokenResponse.class);
ret.status = statusCode;
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
}