Skip to content

Commit 3f36599

Browse files
committed
add lastRefreshTimestamp
1 parent 48c8ac7 commit 3f36599

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

src/main/java/com/google/firebase/auth/UserMetadata.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ public class UserMetadata {
2323

2424
private final long creationTimestamp;
2525
private final long lastSignInTimestamp;
26+
private final long lastRefreshTimestamp;
2627

2728
public UserMetadata(long creationTimestamp) {
28-
this(creationTimestamp, 0L);
29+
this(creationTimestamp, 0L, 0L);
2930
}
3031

31-
public UserMetadata(long creationTimestamp, long lastSignInTimestamp) {
32+
public UserMetadata(long creationTimestamp, long lastSignInTimestamp, long lastRefreshTimestamp) {
3233
this.creationTimestamp = creationTimestamp;
3334
this.lastSignInTimestamp = lastSignInTimestamp;
35+
this.lastRefreshTimestamp = lastRefreshTimestamp;
3436
}
3537

3638
/**
@@ -50,4 +52,13 @@ public long getCreationTimestamp() {
5052
public long getLastSignInTimestamp() {
5153
return lastSignInTimestamp;
5254
}
55+
56+
/**
57+
* Returns the time at which the user was last active (ID token refreshed).
58+
59+
* @return Milliseconds since epoch timestamp, or 0 if the user was never active.
60+
*/
61+
public long getLastRefreshTimestamp() {
62+
return lastRefreshTimestamp;
63+
}
5364
}

src/main/java/com/google/firebase/auth/UserRecord.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121

2222
import com.google.api.client.json.JsonFactory;
23+
import com.google.api.client.util.DateTime;
2324
import com.google.common.base.Strings;
2425
import com.google.common.collect.ImmutableList;
2526
import com.google.common.collect.ImmutableMap;
@@ -80,7 +81,15 @@ public class UserRecord implements UserInfo {
8081
}
8182
}
8283
this.tokensValidAfterTimestamp = response.getValidSince() * 1000;
83-
this.userMetadata = new UserMetadata(response.getCreatedAt(), response.getLastLoginAt());
84+
85+
String lastRefreshAtRfc3339 = response.getLastRefreshAt();
86+
long lastRefreshAtMillis = 0;
87+
if (lastRefreshAtRfc3339 != null && !lastRefreshAtRfc3339.isEmpty()) {
88+
lastRefreshAtMillis = DateTime.parseRfc3339(lastRefreshAtRfc3339).getValue();
89+
}
90+
91+
this.userMetadata = new UserMetadata(
92+
response.getCreatedAt(), response.getLastLoginAt(), lastRefreshAtMillis);
8493
this.customClaims = parseCustomClaims(response.getCustomClaims(), jsonFactory);
8594
}
8695

src/main/java/com/google/firebase/auth/internal/GetAccountInfoResponse.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public static class User {
7373
@Key("lastLoginAt")
7474
private long lastLoginAt;
7575

76+
@Key("lastRefreshAt")
77+
private String lastRefreshAt;
78+
7679
@Key("validSince")
7780
private long validSince;
7881

@@ -119,6 +122,10 @@ public long getLastLoginAt() {
119122
return lastLoginAt;
120123
}
121124

125+
public String getLastRefreshAt() {
126+
return lastRefreshAt;
127+
}
128+
122129
public long getValidSince() {
123130
return validSince;
124131
}

src/test/java/com/google/firebase/auth/FirebaseAuthIT.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,35 @@ public void testUserLifecycle() throws Exception {
248248
}
249249
}
250250

251+
@Test
252+
public void testLastRefreshTime() throws Exception {
253+
RandomUser user = RandomUser.create();
254+
UserRecord newUserRecord = auth.createUser(new CreateRequest()
255+
.setUid(user.uid)
256+
.setEmail(user.email)
257+
.setEmailVerified(false)
258+
.setPassword("password"));
259+
260+
try {
261+
// New users should not have a lastRefreshTimestamp set.
262+
assertEquals(0, newUserRecord.getUserMetadata().getLastRefreshTimestamp());
263+
264+
// Login to cause the lastRefreshTimestamp to be set.
265+
signInWithPassword(newUserRecord.getEmail(), "password");
266+
267+
UserRecord userRecord = auth.getUser(newUserRecord.getUid());
268+
269+
// Ensure the lastRefreshTimestamp is approximately "now" (with a tollerance of 30 minutes).
270+
long now = System.currentTimeMillis();
271+
long tollerance = TimeUnit.MINUTES.toMillis(30);
272+
long lastRefreshTimestamp = userRecord.getUserMetadata().getLastRefreshTimestamp();
273+
assertTrue(now - tollerance <= lastRefreshTimestamp);
274+
assertTrue(lastRefreshTimestamp <= now + tollerance);
275+
} finally {
276+
auth.deleteUser(newUserRecord.getUid());
277+
}
278+
}
279+
251280
@Test
252281
public void testListUsers() throws Exception {
253282
final List<String> uids = new ArrayList<>();
@@ -637,7 +666,7 @@ private String signInWithPassword(String email, String password) throws IOExcept
637666
GenericUrl url = new GenericUrl(VERIFY_PASSWORD_URL + "?key="
638667
+ IntegrationTestUtils.getApiKey());
639668
Map<String, Object> content = ImmutableMap.<String, Object>of(
640-
"email", email, "password", password);
669+
"email", email, "password", password, "returnSecureToken", true);
641670
HttpRequest request = transport.createRequestFactory().buildPostRequest(url,
642671
new JsonHttpContent(jsonFactory, content));
643672
request.setParser(new JsonObjectParser(jsonFactory));

src/test/java/com/google/firebase/auth/ImportUserRecordTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void testAllProperties() throws IOException {
6262
.setDisplayName("Test User")
6363
.setPhotoUrl("https://test.com/user.png")
6464
.setPhoneNumber("+1234567890")
65-
.setUserMetadata(new UserMetadata(date.getTime(), date.getTime()))
65+
.setUserMetadata(new UserMetadata(date.getTime(), date.getTime(), date.getTime()))
6666
.setDisabled(false)
6767
.setEmailVerified(true)
6868
.setPasswordHash("password".getBytes())

0 commit comments

Comments
 (0)