-
Notifications
You must be signed in to change notification settings - Fork 195
/
PsGoogle.java
243 lines (222 loc) · 7.13 KB
/
PsGoogle.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
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.facets.auth.social;
import com.jcabi.http.request.JdkRequest;
import com.jcabi.http.response.JsonResponse;
import com.jcabi.http.response.RestResponse;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.json.JsonObject;
import lombok.EqualsAndHashCode;
import org.takes.HttpException;
import org.takes.Request;
import org.takes.Response;
import org.takes.facets.auth.Identity;
import org.takes.facets.auth.Pass;
import org.takes.misc.Href;
import org.takes.misc.Opt;
import org.takes.rq.RqHref;
/**
* Google OAuth landing/callback page.
*
* <p>The class is immutable and thread-safe.
*
* @since 0.9
* @checkstyle MultipleStringLiteralsCheck (500 lines)
*/
@EqualsAndHashCode(of = { "app", "key", "redir" })
public final class PsGoogle implements Pass {
/**
* Error.
*/
private static final String ERROR = "error";
/**
* Picture.
*/
private static final String PICTURE = "picture";
/**
* Display name.
*/
private static final String DISPLAY_NAME = "displayName";
/**
* Access token.
*/
private static final String ACCESS_TOKEN = "access_token";
/**
* Name.
*/
private static final String NAME = "name";
/**
* Code.
*/
private static final String CODE = "code";
/**
* App name.
*/
private final String app;
/**
* Key.
*/
private final String key;
/**
* Redirect URI.
*/
private final String redir;
/**
* Google OAuth url.
*/
private final String gauth;
/**
* Google API url.
*/
private final String gapi;
/**
* Ctor.
* @param gapp Google app
* @param gkey Google key
* @param uri Redirect URI (exactly as registered in Google console)
*/
public PsGoogle(final String gapp, final String gkey,
final String uri) {
this(
gapp,
gkey,
uri,
"https://accounts.google.com",
"https://www.googleapis.com"
);
}
/**
* Ctor.
* @param gapp Google app
* @param gkey Google key
* @param uri Redirect URI (exactly as registered in Google console)
* @param gurl Google OAuth url
* @param aurl Google API url
* @checkstyle ParameterNumberCheck (2 lines)
*/
PsGoogle(final String gapp, final String gkey,
final String uri, final String gurl, final String aurl) {
this.app = gapp;
this.key = gkey;
this.redir = uri;
this.gauth = gurl;
this.gapi = aurl;
}
@Override
public Opt<Identity> enter(final Request request)
throws IOException {
final Href href = new RqHref.Base(request).href();
final Iterator<String> code = href.param(PsGoogle.CODE).iterator();
if (!code.hasNext()) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
"code is not provided by Google, probably some mistake"
);
}
return new Opt.Single<>(this.fetch(this.token(code.next())));
}
@Override
public Response exit(final Response response,
final Identity identity) {
return response;
}
/**
* Get user name from Google, with the token provided.
* @param token Google access token
* @return The user found in Google
* @throws IOException If fails
*/
private Identity fetch(final String token) throws IOException {
// @checkstyle LineLength (1 line)
final String uri = new Href(this.gapi).path("plus").path("v1")
.path("people")
.path("me")
.with(PsGoogle.ACCESS_TOKEN, token)
.toString();
final JsonObject json = new JdkRequest(uri).fetch()
.as(JsonResponse.class).json()
.readObject();
if (json.containsKey(PsGoogle.ERROR)) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format(
"could not retrieve id from Google, possible cause: %s.",
json.getJsonObject(PsGoogle.ERROR).get("message")
)
);
}
return PsGoogle.parse(json);
}
/**
* Retrieve Google access token.
* @param code Google "authorization code"
* @return The token
* @throws IOException If failed
*/
private String token(final String code) throws IOException {
return new JdkRequest(
new Href(this.gauth).path("o").path("oauth2").path("token")
.toString()
).body()
.formParam("client_id", this.app)
.formParam("redirect_uri", this.redir)
.formParam("client_secret", this.key)
.formParam("grant_type", "authorization_code")
.formParam(PsGoogle.CODE, code)
.back()
.header("Content-Type", "application/x-www-form-urlencoded")
.method(com.jcabi.http.Request.POST)
.fetch().as(RestResponse.class)
.assertStatus(HttpURLConnection.HTTP_OK)
.as(JsonResponse.class).json()
.readObject()
.getString(PsGoogle.ACCESS_TOKEN);
}
/**
* Make identity from JSON object.
* @param json JSON received from Google
* @return Identity found
*/
private static Identity parse(final JsonObject json) {
final Map<String, String> props = new HashMap<>(json.size());
final Opt<JsonObject> image = new Opt.Single<>(
json.getJsonObject("image")
);
if (image.has()) {
props.put(PsGoogle.PICTURE, image.get().getString("url", "#"));
} else {
props.put(PsGoogle.PICTURE, "#");
}
props.put(
PsGoogle.NAME, json.getString(PsGoogle.DISPLAY_NAME, "unknown")
);
return new Identity.Simple(
String.format("urn:google:%s", json.getString("id")), props
);
}
}