Skip to content

Commit 32d5230

Browse files
committed
Issue 827 getEmailVerified now reads 'email_verified' field
https://codereview.appspot.com/13431043/
1 parent b4be2bd commit 32d5230

File tree

3 files changed

+133
-6
lines changed

3 files changed

+133
-6
lines changed

findbugs-exclude.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<FindBugsFilter>
44
<!-- http://findbugs.sourceforge.net/bugDescriptions.html -->
5+
<!-- http://findbugs.sourceforge.net/manual/filter.html -->
56
<LastVersion value="-1" relOp="NEQ"/>
67
<!-- The following have not yet been investigated -->
78

@@ -60,4 +61,8 @@
6061
<Bug pattern="BETA_METHOD_USAGE"/>
6162
<Class name="com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow$Builder"/>
6263
</And>
64+
<And>
65+
<Bug pattern="NP_BOOLEAN_RETURN_NULL"/>
66+
<!-- Method with Boolean return type returns explicit null -->
67+
</And>
6368
</FindBugsFilter>

google-api-client/src/main/java/com/google/api/client/googleapis/auth/oauth2/GoogleIdToken.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,14 @@ public static class Payload extends IdToken.Payload {
9898
@Key("email")
9999
private String email;
100100

101-
/** {@code true} if the email is verified. */
102-
@Key("verified_email")
103-
private boolean emailVerified;
101+
/**
102+
* {@code true} if the email is verified.
103+
* TODO(mwan): change the type of the field to Boolean and the handling in
104+
* {@link #getEmailVerified()} accordingly after Google OpenID Connect endpoint fixes the
105+
* type of the field in ID Token.
106+
*/
107+
@Key("email_verified")
108+
private Object emailVerified;
104109

105110
public Payload() {
106111
}
@@ -198,9 +203,23 @@ public Payload setEmail(String email) {
198203
* </p>
199204
*
200205
* @since 1.10
206+
*
207+
* <p>
208+
* Upgrade warning: in prior version 1.16 this method accessed {@code "verified_email"}
209+
* and returns a boolean, but starting with verison 1.17, it now accesses
210+
* {@code "email_verified"} and returns a Boolean. Previously, if this value was not
211+
* specified, this method would return {@code false}, but now it returns {@code null}.
212+
* </p>
201213
*/
202-
public boolean getEmailVerified() {
203-
return emailVerified;
214+
public Boolean getEmailVerified() {
215+
if (emailVerified == null) {
216+
return null;
217+
}
218+
if (emailVerified instanceof Boolean) {
219+
return (Boolean) emailVerified;
220+
}
221+
222+
return Boolean.valueOf((String) emailVerified);
204223
}
205224

206225
/**
@@ -211,8 +230,14 @@ public boolean getEmailVerified() {
211230
* </p>
212231
*
213232
* @since 1.10
233+
*
234+
* <p>
235+
* Upgrade warning: in prior version 1.16 this method accessed {@code "verified_email"} and
236+
* required a boolean parameter, but starting with verison 1.17, it now accesses
237+
* {@code "email_verified"} and requires a Boolean parameter.
238+
* </p>
214239
*/
215-
public Payload setEmailVerified(boolean emailVerified) {
240+
public Payload setEmailVerified(Boolean emailVerified) {
216241
this.emailVerified = emailVerified;
217242
return this;
218243
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2013 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.googleapis.auth.oauth2;
16+
17+
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload;
18+
19+
import junit.framework.TestCase;
20+
21+
/** Tests for {@link GoogleIdToken}.*/
22+
23+
public class GoogleIdTokenTest extends TestCase {
24+
private static final String USER_ID = "1234567890";
25+
private static final String ANOTHER_USER_ID = "2345678901";
26+
private static final String CLIENT_ID = "myClientId";
27+
private static final String ANOTHER_CLIENT_ID = "anotherClientId";
28+
private static final String EMAIL_VERIFIED_KEY = "email_verified";
29+
30+
private static Payload newPayload(String userId, String clientId) {
31+
Payload payload = new Payload();
32+
payload.setIssuer("accounts.google.com");
33+
payload.setAudience(clientId);
34+
payload.setAuthorizedParty(clientId);
35+
payload.setSubject(userId);
36+
payload.setExpirationTimeSeconds(100L);
37+
payload.setIssuedAtTimeSeconds(0L);
38+
return payload;
39+
}
40+
41+
public void testDeprecatedMethods() {
42+
Payload payload = newPayload(USER_ID, CLIENT_ID);
43+
assertEquals(USER_ID, payload.getUserId());
44+
assertEquals(CLIENT_ID, payload.getIssuee());
45+
46+
payload.setUserId(ANOTHER_USER_ID);
47+
payload.setIssuee(ANOTHER_CLIENT_ID);
48+
assertEquals(ANOTHER_USER_ID, payload.getUserId());
49+
assertEquals(ANOTHER_CLIENT_ID, payload.getIssuee());
50+
assertEquals(ANOTHER_USER_ID, payload.getSubject());
51+
assertEquals(ANOTHER_CLIENT_ID, payload.getAuthorizedParty());
52+
}
53+
54+
public void testEmailVerified() {
55+
Payload payload = newPayload(USER_ID, CLIENT_ID);
56+
assertNull(payload.getEmailVerified());
57+
58+
payload.setEmailVerified(true);
59+
assertTrue(payload.getEmailVerified());
60+
61+
payload.setEmailVerified(false);
62+
assertFalse(payload.getEmailVerified());
63+
64+
payload.setEmailVerified(null);
65+
assertNull(payload.getEmailVerified());
66+
67+
payload.set(EMAIL_VERIFIED_KEY, "true");
68+
assertTrue(payload.getEmailVerified());
69+
70+
payload.set(EMAIL_VERIFIED_KEY, true);
71+
assertTrue(payload.getEmailVerified());
72+
73+
payload.set(EMAIL_VERIFIED_KEY, "false");
74+
assertFalse(payload.getEmailVerified());
75+
76+
payload.set(EMAIL_VERIFIED_KEY, false);
77+
assertFalse(payload.getEmailVerified());
78+
79+
payload.set(EMAIL_VERIFIED_KEY, "RandomString");
80+
assertFalse(payload.getEmailVerified());
81+
82+
payload.set(EMAIL_VERIFIED_KEY, "");
83+
assertFalse(payload.getEmailVerified());
84+
85+
payload.set(EMAIL_VERIFIED_KEY, null);
86+
assertNull(payload.getEmailVerified());
87+
88+
// Wrong type.
89+
payload.set(EMAIL_VERIFIED_KEY, new Integer(5));
90+
try {
91+
payload.getEmailVerified();
92+
fail();
93+
} catch (ClassCastException e) {
94+
// Expected.
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)