Skip to content

Commit

Permalink
add LastAuthActivity to StitchUser (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkaye407 authored and adamchel committed Feb 22, 2019
1 parent b296762 commit ba56841
Show file tree
Hide file tree
Showing 20 changed files with 209 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.mongodb.stitch.core.auth.internal.StitchUserFactory;
import com.mongodb.stitch.core.auth.internal.StitchUserProfileImpl;

import java.util.Date;

public final class StitchUserFactoryImpl implements StitchUserFactory<StitchUser> {

private final StitchAuthImpl auth;
Expand All @@ -35,8 +37,9 @@ public StitchUser makeUser(
final String loggedInProviderType,
final String loggedInProviderName,
final StitchUserProfileImpl userProfile,
final boolean isLoggedIn) {
return new StitchUserImpl(
id, deviceId, loggedInProviderType, loggedInProviderName, userProfile, auth, isLoggedIn);
final boolean isLoggedIn,
final Date lastAuthActivity) {
return new StitchUserImpl(id, deviceId, loggedInProviderType, loggedInProviderName,
userProfile, auth, isLoggedIn, lastAuthActivity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.mongodb.stitch.core.auth.internal.CoreStitchUserImpl;
import com.mongodb.stitch.core.auth.internal.StitchUserProfileImpl;

import java.util.Date;

public final class StitchUserImpl extends CoreStitchUserImpl implements StitchUser {
private final StitchAuthImpl auth;

Expand All @@ -32,8 +34,10 @@ public StitchUserImpl(
final String loggedInProviderName,
final StitchUserProfileImpl profile,
final StitchAuthImpl auth,
final boolean isLoggedIn) {
super(id, deviceId, loggedInProviderType, loggedInProviderName, profile, isLoggedIn);
final boolean isLoggedIn,
final Date lastAuthActivity) {
super(id, deviceId, loggedInProviderType, loggedInProviderName,
profile, isLoggedIn, lastAuthActivity);
this.auth = auth;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class StitchAppClientIntTests : BaseStitchAndroidIntTest() {
// check storage
assertTrue(client.auth.isLoggedIn)
assertEquals(anonUser.loggedInProviderType, AnonymousAuthProvider.TYPE)
assertNotNull(client.auth.user!!.lastAuthActivity)

// login anonymously again and make sure user ID is the same
assertEquals(
Expand Down Expand Up @@ -152,6 +153,7 @@ class StitchAppClientIntTests : BaseStitchAndroidIntTest() {
assertTrue(client.auth.isLoggedIn)
assertEquals(client.auth.user!!.loggedInProviderType, UserPasswordAuthProvider.TYPE)
assertEquals(client.auth.user?.id, id2)
assertNotNull(client.auth.user!!.lastAuthActivity)

// verify ordering is preserved
assertEquals(client.auth.listUsers().size, 3)
Expand Down
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ subprojects {
name "Adam Chelminski"
email "[email protected]"
}
developer {
id "tkaye407"
name "Tyler Kaye"
email "[email protected]"
}
developer {
id "dkaminsky"
name "Doug Kaminsky"
email "[email protected]"
}
}
scm {
url "https://github.com/mongodb/stitch-android-sdk"
Expand Down
2 changes: 1 addition & 1 deletion contrib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ The general publishing flow can be followed using `major` as the bump type in `b
* You must enable clear text traffic in the core Android application locally (**do not commit this change**)
* In file *android/core/src/main/AndroidManifest.xml*, change the ```application``` XML tag as follows:
```<application android:usesCleartextTraffic="true">```
* You must run at least one ```mongod``` instance with replica sets initiated or a ```mongos``` instance with same locally on port 27000
* You must run at least one ```mongod``` instance with replica sets initiated or a ```mongos``` instance with same locally on port 26000
* You must run the Stitch server locally using the Android-specific configuration:
```--configFile ./etc/configs/test_config_sdk_base.json --configFile ./etc/configs/test_config_sdk_android.json```
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ class StitchAdminAuth(
loggedInProviderType,
loggedInProviderName,
userProfile,
isLoggedIn ->
isLoggedIn,
lastAuthActivity ->
StitchAdminUser(
id,
deviceId,
loggedInProviderType,
loggedInProviderName,
userProfile,
isLoggedIn
isLoggedIn,
lastAuthActivity
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import com.mongodb.stitch.core.auth.UserType
import com.mongodb.stitch.core.auth.internal.CoreStitchUser
import com.mongodb.stitch.core.auth.internal.StitchUserProfileImpl

import java.util.Date

class StitchAdminUser(
private val id: String,
private val deviceId: String,
private val loggedInProviderType: String,
private val loggedInProviderName: String,
private val profile: StitchUserProfileImpl?,
private val isLoggedIn: Boolean
private val isLoggedIn: Boolean,
private val lastAuthActivity: Date
) : CoreStitchUser {

override fun getLoggedInProviderType(): String {
Expand Down Expand Up @@ -46,4 +49,8 @@ class StitchAdminUser(
override fun isLoggedIn(): Boolean {
return isLoggedIn
}

override fun getLastAuthActivity(): Date {
return lastAuthActivity
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

Expand All @@ -40,6 +41,7 @@ public class AuthInfo {
private final String loggedInProviderType;
private final String loggedInProviderName;
private final StitchUserProfileImpl userProfile;
private final Date lastAuthActivity;

/**
* Constructs a new AuthInfo that's fully specified.
Expand All @@ -51,6 +53,8 @@ public class AuthInfo {
* @param loggedInProviderType the type of auth provider the current user logged in with.
* @param loggedInProviderName the name of the auth provider the current user logged in with.
* @param userProfile the profile information about the currently logged in user.
* @param lastAuthActivity the time since the Unix Epoch since this user was logged into,
* logged out of, switched to, or switched from.
*/
public AuthInfo(
final String userId,
Expand All @@ -59,18 +63,20 @@ public AuthInfo(
final String refreshToken,
final String loggedInProviderType,
final String loggedInProviderName,
final StitchUserProfileImpl userProfile) {
final StitchUserProfileImpl userProfile,
final Date lastAuthActivity) {
this.userId = userId;
this.deviceId = deviceId;
this.accessToken = accessToken;
this.refreshToken = refreshToken;
this.loggedInProviderType = loggedInProviderType;
this.loggedInProviderName = loggedInProviderName;
this.userProfile = userProfile;
this.lastAuthActivity = lastAuthActivity == null ? null : new Date(lastAuthActivity.getTime());
}

static AuthInfo empty() {
return new AuthInfo(null, null, null, null, null, null, null);
return new AuthInfo(null, null, null, null, null, null, null, null);
}

static AuthInfo readFromApi(final InputStream is) throws IOException {
Expand Down Expand Up @@ -109,7 +115,8 @@ static void writeActiveUserAuthInfoToStorage(final AuthInfo authInfo,
authInfo.refreshToken,
authInfo.loggedInProviderType,
authInfo.loggedInProviderName,
authInfo.userProfile
authInfo.userProfile,
authInfo.lastAuthActivity
);

final String rawInfo = StitchObjectMapper.getInstance().writeValueAsString(info);
Expand All @@ -129,7 +136,8 @@ static void writeCurrentUsersToStorage(
authInfo.refreshToken,
authInfo.loggedInProviderType,
authInfo.loggedInProviderName,
authInfo.userProfile));
authInfo.userProfile,
authInfo.lastAuthActivity));
}

final String rawInfo = StitchObjectMapper.getInstance().writeValueAsString(authInfos);
Expand All @@ -138,20 +146,27 @@ static void writeCurrentUsersToStorage(

AuthInfo loggedOut() {
return new AuthInfo(
userId, deviceId, null, null, loggedInProviderType, loggedInProviderName, userProfile);
userId, deviceId, null, null, loggedInProviderType,
loggedInProviderName, userProfile, new Date());
}

AuthInfo withNewAuthActivityTime() {
return new AuthInfo(
userId, deviceId, accessToken, refreshToken, loggedInProviderType,
loggedInProviderName, userProfile, new Date());
}

AuthInfo withClearedUser() {
return new AuthInfo(
null, deviceId, null, null, null, null, null);
null, deviceId, null, null, null, null, null, null);
}

AuthInfo withAuthProvider(
final String providerType,
final String providerName
) {
return new AuthInfo(
userId, deviceId, accessToken, refreshToken, providerType, providerName, userProfile);
return new AuthInfo(userId, deviceId, accessToken, refreshToken, providerType,
providerName, userProfile, new Date());
}

AuthInfo merge(final AuthInfo newInfo) {
Expand All @@ -162,7 +177,8 @@ AuthInfo merge(final AuthInfo newInfo) {
newInfo.refreshToken == null ? refreshToken : newInfo.refreshToken,
newInfo.loggedInProviderType == null ? loggedInProviderType : newInfo.loggedInProviderType,
newInfo.loggedInProviderName == null ? loggedInProviderName : newInfo.loggedInProviderName,
newInfo.userProfile == null ? userProfile : newInfo.userProfile);
newInfo.userProfile == null ? userProfile : newInfo.userProfile,
new Date());
}

public String getUserId() {
Expand Down Expand Up @@ -193,6 +209,10 @@ public StitchUserProfileImpl getUserProfile() {
return userProfile;
}

public Date getLastAuthActivity() {
return lastAuthActivity == null ? null : new Date(lastAuthActivity.getTime());
}

public boolean isLoggedIn() {
return accessToken != null && refreshToken != null;
}
Expand Down
Loading

0 comments on commit ba56841

Please sign in to comment.