Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into AGDROID-719
Browse files Browse the repository at this point in the history
  • Loading branch information
pb82 committed Feb 8, 2018
2 parents 3b6bcda + b71d208 commit ae0b6f2
Show file tree
Hide file tree
Showing 27 changed files with 666 additions and 195 deletions.
10 changes: 9 additions & 1 deletion auth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

testOptions {
unitTests {
includeAndroidResources = true
}
}
}

dependencies {
implementation project(path: ':core')
implementation 'net.openid:appauth'
implementation 'org.bitbucket.b_c:jose4j'

testImplementation "junit:junit"
testImplementation 'junit:junit'
testImplementation 'org.mockito:mockito-core'
testImplementation 'org.robolectric:robolectric'
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.aerogear.auth;
package org.aerogear.android.ags.auth;

import org.aerogear.auth.credentials.ICredential;
import org.aerogear.android.ags.auth.credentials.ICredential;
import org.aerogear.mobile.core.configuration.ServiceConfiguration;

import java.security.Principal;

Expand All @@ -12,10 +13,11 @@ public class AbstractAuthenticator {
/**
* Authentication service configuration.
*/
private final AuthServiceConfig config;
private final ServiceConfiguration serviceConfig;

public AbstractAuthenticator(final AuthServiceConfig config) {
this.config = config;

public AbstractAuthenticator(final ServiceConfiguration serviceConfig) {
this.serviceConfig = serviceConfig;
}

/**
Expand All @@ -40,7 +42,5 @@ public void logout(final Principal principal) {
* Returns the authentication service configuration
* @return the authentication service configuration
*/
protected AuthServiceConfig getConfig() {
return config;
}
public ServiceConfiguration getServiceConfig() { return this.serviceConfig; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aerogear.auth;
package org.aerogear.android.ags.auth;

/**
* Base class for aerogear principals
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aerogear.auth;
package org.aerogear.android.ags.auth;

/**
* Base class for roles pojos.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
package org.aerogear.auth;
package org.aerogear.android.ags.auth;

import org.aerogear.auth.credentials.ICredential;
import org.aerogear.auth.impl.OIDCAuthCodeImpl;
import org.aerogear.auth.impl.OIDCTokenAuthenticatorImpl;
import android.content.Context;

import net.openid.appauth.AuthState;

import org.aerogear.android.ags.auth.credentials.ICredential;
import org.aerogear.android.ags.auth.impl.OIDCAuthCodeImpl;
import org.aerogear.android.ags.auth.impl.OIDCTokenAuthenticatorImpl;
import org.aerogear.mobile.core.MobileCore;
import org.aerogear.mobile.core.ServiceModule;
import org.aerogear.mobile.core.configuration.ServiceConfiguration;

import java.security.Principal;
import java.util.concurrent.Future;

/**
* Entry point for authenticating users.
*/
public class AuthService {

/**
* Authentication service singleton.
*/
private static AuthService INSTANCE;
public class AuthService implements ServiceModule {

private AuthenticationChain authenticatorChain;

/**
* Instantiates a new AuthService object
* @param config Authentication Service configuration
*/
private AuthService(final AuthServiceConfig config) {
this.authenticatorChain = AuthenticationChain
.newChain()
.with(new OIDCTokenAuthenticatorImpl(config))
.with(new OIDCAuthCodeImpl(config))
.build();
}
public AuthService() {}

private void configureDefaultAuthenticationChain(final AuthenticationChain authenticationChain) {

Expand Down Expand Up @@ -67,18 +62,30 @@ public void setAuthenticatorChain(AuthenticationChain newChain) {
this.authenticatorChain = newChain;
}

@Override
public String type() {
return "keycloak";
}

@Override
public void configure(final MobileCore core, final ServiceConfiguration serviceConfiguration) {
this.authenticatorChain = AuthenticationChain
.newChain()
.with(new OIDCTokenAuthenticatorImpl(serviceConfiguration))
.with(new OIDCAuthCodeImpl(serviceConfiguration))
.build();
}

/**
* Returns the authentication service singleton.
*
* @return the authentication service singleton
* Initialize the module. This should be called before any other method when using the module.
* @param context
*/
public static synchronized AuthService getInstance() {
if (INSTANCE == null) {
// FIXME: load the configurations from core and pass it here
INSTANCE = new AuthService(null);
}

return INSTANCE;
public void init(final Context context) {
AuthStateManager.getInstance(context);
}

@Override
public void destroy() {

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aerogear.auth;
package org.aerogear.android.ags.auth;

/**
* Authentication service configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,75 @@
package org.aerogear.auth.utils;
package org.aerogear.android.ags.auth;

import android.content.Context;
import android.content.SharedPreferences;
import org.aerogear.auth.credentials.OIDCCredentials;
import org.json.JSONException;

import org.aerogear.android.ags.auth.credentials.OIDCCredentials;

/**
* Saves, retrieves and delete a token.
*/
public class AuthStateManager {

private static AuthStateManager instance = null;
private static final String STORE_NAME = "org.aerogear.android.auth.AuthState";
private static final String KEY_STATE = "state";

private final SharedPreferences prefs;

public AuthStateManager(final Context context) {
private AuthStateManager(final Context context) {
this.prefs = context.getSharedPreferences(STORE_NAME, Context.MODE_PRIVATE);
}

/**
* Reads credentials from storage.
* @return OIDCCredentials
*/
public OIDCCredentials read() {
String currentState = prefs.getString(KEY_STATE, null);
public OIDCCredentials load() {
final String currentState = prefs.getString(KEY_STATE, null);
if (currentState == null) {
return new OIDCCredentials();
}
try {
return new OIDCCredentials(currentState);
} catch (JSONException ex) {
return new OIDCCredentials();
}
return OIDCCredentials.deserialize(currentState);
}

/**
* Saves a token
* @param authState token to be saved
* @throws IllegalStateException
*/
public synchronized void write(final OIDCCredentials authState) {
public synchronized void save(final OIDCCredentials authState) {
if (authState == null) {
clear();
} else {
if(!prefs.edit().putString(KEY_STATE, authState.serialise()).commit()) {
SharedPreferences.Editor e = prefs.edit();
SharedPreferences.Editor bleh = e.putString(KEY_STATE, authState.serialize());
if(!bleh.commit()) {
throw new IllegalStateException("Failed to update state from shared preferences");
}
}
}

/**
* Deletes a token
* @throws IllegalArgumentException
*/
public synchronized void clear() {
if (!prefs.edit().remove(KEY_STATE).commit()) {
throw new IllegalStateException("Failed to clear state from shared preferences");
}
}

static AuthStateManager getInstance(final Context context) {
if (instance == null) {
instance = new AuthStateManager(context);
}
return instance;
}

public static AuthStateManager getInstance() {
if (instance == null) {
throw new IllegalStateException("Context has not previously been provided. Cannot initialize without Context.");
}
return instance;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.aerogear.auth;
package org.aerogear.android.ags.auth;

import org.aerogear.auth.credentials.ICredential;
import org.aerogear.android.ags.auth.credentials.ICredential;

import java.security.Principal;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aerogear.auth;
package org.aerogear.android.ags.auth;

/**
* Exception thrown when an error occurs authenticating a user.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aerogear.auth;
package org.aerogear.android.ags.auth;

public final class ClientRole extends AbstractRole {
public ClientRole(final String roleName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aerogear.auth;
package org.aerogear.android.ags.auth;

public interface IRole {
String getRoleName();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.aerogear.auth;
package org.aerogear.android.ags.auth;

import org.aerogear.auth.credentials.ICredential;
import org.aerogear.android.ags.auth.credentials.ICredential;

import java.security.Principal;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.aerogear.auth;
package org.aerogear.android.ags.auth;

public final class RealmRole extends AbstractRole {
public RealmRole(final String roleName) {
super(roleName);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aerogear.auth.credentials;
package org.aerogear.android.ags.auth.credentials;

/**
* Base interface for credential objects.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.aerogear.android.ags.auth.credentials;

public interface IIntegrityCheckParameters {

String getAudience();
String getIssuer();
String getPublicKey();

/**
* Check whether the parameters are valid or not. The criteria for validity is that each of
* the parameters is defined (not null) and has valid formatting.
* @return <code>true</code> if the parameters are valid.
*/
boolean isValid();
String serialize();
}

0 comments on commit ae0b6f2

Please sign in to comment.