Skip to content

Commit

Permalink
fix(frontend): Fix common OIDC issues (#4351)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjoyce0510 authored Mar 8, 2022
1 parent 48380ad commit ef31b0e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
24 changes: 23 additions & 1 deletion datahub-frontend/app/auth/AuthModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
import com.linkedin.metadata.restli.DefaultRestliClientFactory;
import com.linkedin.util.Configuration;
import com.datahub.authentication.Authentication;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collections;
import org.pac4j.core.client.Client;
import org.pac4j.core.client.Clients;
Expand All @@ -20,6 +24,7 @@
import org.pac4j.play.http.PlayHttpActionAdapter;
import org.pac4j.play.store.PlayCookieSessionStore;
import org.pac4j.play.store.PlaySessionStore;
import org.pac4j.play.store.ShiroAesDataEncrypter;
import play.Environment;

import java.util.ArrayList;
Expand All @@ -41,6 +46,13 @@
*/
public class AuthModule extends AbstractModule {

/**
* Pac4j Stores Session State in a browser-side cookie in encrypted fashion. This configuration
* value provides a stable encryption base from which to derive the encryption key.
*
* We hash this value (SHA1), then take the first 16 bytes as the AES key.
*/
private static final String PAC4J_AES_KEY_BASE_CONF = "play.http.secret.key";
private final com.typesafe.config.Config _configs;

public AuthModule(final Environment environment, final com.typesafe.config.Config configs) {
Expand All @@ -49,7 +61,17 @@ public AuthModule(final Environment environment, final com.typesafe.config.Confi

@Override
protected void configure() {
final PlayCookieSessionStore playCacheCookieStore = new PlayCookieSessionStore();
PlayCookieSessionStore playCacheCookieStore;
try {
final String aesKeyBase = _configs.getString(PAC4J_AES_KEY_BASE_CONF);
MessageDigest sha = MessageDigest.getInstance("SHA-1");
byte[] key = sha.digest(aesKeyBase.getBytes(StandardCharsets.UTF_8));
key = Arrays.copyOf(key, 16);
playCacheCookieStore = new PlayCookieSessionStore(
new ShiroAesDataEncrypter(new String(key)));
} catch (Exception e) {
throw new RuntimeException("Failed to instantiate Pac4j cookie session store!", e);
}
bind(SessionStore.class).toInstance(playCacheCookieStore);
bind(PlaySessionStore.class).toInstance(playCacheCookieStore);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public CustomOidcClient(final OidcConfiguration configuration) {
protected void clientInit() {
CommonHelper.assertNotNull("configuration", getConfiguration());
getConfiguration().init();

defaultRedirectActionBuilder(new OidcRedirectActionBuilder(getConfiguration(), this));
defaultCredentialsExtractor(new OidcExtractor(getConfiguration(), this));
defaultAuthenticator(new CustomOidcAuthenticator(getConfiguration(), this));
Expand Down
11 changes: 10 additions & 1 deletion datahub-frontend/app/controllers/AuthenticationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.time.temporal.ChronoUnit;

import static auth.AuthUtils.*;
import static org.pac4j.core.client.IndirectClient.*;


// TODO add logging.
public class AuthenticationController extends Controller {
Expand Down Expand Up @@ -147,7 +149,14 @@ public Result logIn() {

private Result redirectToIdentityProvider() {
final PlayWebContext playWebContext = new PlayWebContext(ctx(), _playSessionStore);
final Client client = _ssoManager.getSsoProvider().client();
final Client<?, ?> client = _ssoManager.getSsoProvider().client();

// This is to prevent previous login attempts from being cached.
// We replicate the logic here, which is buried in the Pac4j client.
if (_playSessionStore.get(playWebContext, client.getName() + ATTEMPTED_AUTHENTICATION_SUFFIX) != null) {
_logger.debug("Found previous login attempt. Removing it manually to prevent unexpected errors.");
_playSessionStore.set(playWebContext, client.getName() + ATTEMPTED_AUTHENTICATION_SUFFIX, "");
}
final HttpAction action = client.redirect(playWebContext);
return new PlayHttpActionAdapter().adapt(action.getCode(), playWebContext);
}
Expand Down

0 comments on commit ef31b0e

Please sign in to comment.