Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
* Copyright (c) 2019 ForgeRock. All rights reserved.
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package org.forgerock.android.auth;
import android.content.Context;
import androidx.annotation.WorkerThread;
import org.forgerock.android.auth.exception.AlreadyAuthenticatedException;
import org.forgerock.android.auth.exception.AuthenticationRequiredException;
import java.util.Collection;
public class FRUserModified {
//Hold the current login user.
private static FRUserModified current;
private static String subscriptionId;
private SessionManager sessionManager;
private FRUserModified() {
sessionManager = Config.getInstance().getSessionManager();
}
/**
* Retrieve the existing FRUser instance,
* <p>
* If user session does not exist return null, otherwise return the existing FRUser with the associated user session,
* this cannot guarantee the existing user session is valid.
*
* @return The existing FRUser instance, or null if there is no user session.
*/
public static FRUserModified getCurrentUser() {
if (current != null) {
return current;
}
FRUserModified user = new FRUserModified();
if (user.sessionManager.hasSession()) {
current = user;
}
return current;
}
/**
* Logout the user
*/
public void logout() {
sessionManager.close();
}
public void clear() {
sessionManager.getTokenManager().clear();
sessionManager.getSingleSignOnManager().clear();
}
public static void clearCurrentUser() {
current = null;
subscriptionId = null;
}
public static void persist(AccessToken accessToken, SSOToken ssoToken, Collection<String> cookies) {
current = new FRUserModified();
current.persist(accessToken);
current.persist(ssoToken, cookies);
}
private void persist(AccessToken accessToken) {
sessionManager.getTokenManager().persist(accessToken);
}
private void persist(SSOToken ssoToken, Collection<String> cookies) {
sessionManager.getSingleSignOnManager().persist(ssoToken);
sessionManager.getSingleSignOnManager().persist(cookies);
}
public Collection<String> getCookies() {
return sessionManager.getSingleSignOnManager().getCookies();
}
public static void setSubscriptionId(String subsId) {
subscriptionId = subsId;
}
public static String getSubscriptionId() {
return subscriptionId;
}
/**
* Retrieve the {@link AccessToken} asynchronously,
*
* <p>
* If the stored {@link AccessToken} is expired, auto refresh the token.
*
* @param listener Listener to listen get Access Token event.
*/
public void getAccessToken(FRListener<AccessToken> listener) {
sessionManager.getAccessToken(listener);
}
@WorkerThread
public AccessToken getAccessToken() throws AuthenticationRequiredException {
return sessionManager.getAccessToken();
}
/**
* Handles REST requests to the OpenId Connect userinfo endpoint for retrieving information about the user who granted
* the authorization for the token.
*
* @param listener Listener to listen get UserInfo event.
*/
public void getUserInfo(final FRListener<UserInfo> listener) {
UserService.builder()
.serverConfig(Config.getInstance().getServerConfig())
.build()
.userinfo(new FRListener<UserInfo>() {
@Override
public void onSuccess(UserInfo result) {
Listener.onSuccess(listener, result);
}
@Override
public void onException(Exception e) {
Listener.onException(listener, e);
}
});
}
/**
* Trigger the user login process, the login service name is defined under <b>string.xml</b> file with
* <b>forgerock_auth_service</b>
*
* @param context The Application Context
* @param listener Listener to listen login event.
* <b> Throw {@link AlreadyAuthenticatedException} user session already exists.
*/
@SuppressWarnings("deprecation")
public static void login(Context context, String serviceName, final NodeListener<FRUserModified> listener) {
SessionManager sessionManager = Config.getInstance().getSessionManager();
if (sessionManager.hasSession()) {
Listener.onException(listener, new AlreadyAuthenticatedException("User is already authenticated"));
return;
}
createFRAuth(context, serviceName, sessionManager)
.next(context, listener);
}
private static FRAuth createFRAuth(Context context, String serviceName, SessionManager sessionManager) {
return FRAuth.builder()
.serviceName(serviceName)
.context(context)
.serverConfig(Config.getInstance().getServerConfig())
.sessionManager(sessionManager)
.interceptor(new OAuthInterceptor(sessionManager.getTokenManager()))
.interceptor(new AccessTokenStoreInterceptor(sessionManager.getTokenManager()))
.interceptor(new UserInterceptor())
.build();
}
/**
* Trigger the user registration process, the registration service name is defined under <b>string.xml</b> file with
* <b>forgerock_registration_service</b>
*
* @param context The Application Context
* @param listener Listener to listen register event.
* <b> Throw {@link AlreadyAuthenticatedException} user session already exists.
*/
@SuppressWarnings("deprecation")
public static void register(Context context, NodeListener<FRUserModified> listener) {
SessionManager sessionManager = Config.getInstance().getSessionManager();
if (sessionManager.hasSession()) {
Listener.onException(listener, new AlreadyAuthenticatedException("User is already authenticated"));
return;
}
createFRAuth(context, context.getString(R.string.forgerock_registration_service), sessionManager)
.next(context, listener);
}
private static class UserInterceptor implements Interceptor<Object> {
@Override
public void intercept(Chain chain, Object any) {
chain.proceed(getCurrentUser());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment