Skip to content

Commit 66cbd54

Browse files
author
Chris Byron
committed
static akey
1 parent e6324c1 commit 66cbd54

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

pom.xml

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,33 @@
66

77
<groupId>com.mulesoft</groupId>
88
<artifactId>keycloak-duo-spi</artifactId>
9-
<version>1.0</version>
9+
<version>1.1</version>
1010

1111
<name>Duo Mfa Keycloak</name>
1212
<description/>
1313
<packaging>jar</packaging>
1414

15+
<properties>
16+
<keycloak.version>3.4.3.Final</keycloak.version>
17+
</properties>
18+
1519
<dependencies>
1620
<dependency>
1721
<groupId>org.keycloak</groupId>
1822
<artifactId>keycloak-server-spi</artifactId>
19-
<version>3.3.0.CR2</version>
23+
<version>${keycloak.version}</version>
2024
<scope>provided</scope>
2125
</dependency>
2226
<dependency>
2327
<groupId>org.keycloak</groupId>
2428
<artifactId>keycloak-server-spi-private</artifactId>
25-
<version>3.3.0.CR2</version>
29+
<version>${keycloak.version}</version>
2630
<scope>provided</scope>
2731
</dependency>
2832
<dependency>
2933
<groupId>org.keycloak</groupId>
3034
<artifactId>keycloak-core</artifactId>
31-
<version>3.3.0.CR2</version>
35+
<version>${keycloak.version}</version>
3236
<scope>provided</scope>
3337
</dependency>
3438
<dependency>

src/main/java/com/mulesoft/keycloak/auth/spi/duo/DuoMfaAuthenticator.java

+8-16
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,7 @@
4141

4242
public class DuoMfaAuthenticator implements Authenticator{
4343

44-
private String akey;
45-
46-
public DuoMfaAuthenticator() {
47-
try {
48-
// yay java `hashlib.sha256(os.urandom(32))`
49-
int r = new Random().nextInt();
50-
byte[] b = ByteBuffer.allocate(4).putInt(r).array();
51-
byte[] d = MessageDigest.getInstance("SHA-256").digest(b);
52-
byte[] e = Base64.getEncoder().encode(d);
53-
akey = new String(e);
54-
} catch (NoSuchAlgorithmException ex) {
55-
throw new AuthenticationFlowException("Error initializing sha256: " + ex.getMessage(), AuthenticationFlowError.INTERNAL_ERROR);
56-
}
57-
}
44+
public DuoMfaAuthenticator() {}
5845

5946
@Override
6047
public boolean requiresUser() {
@@ -74,7 +61,7 @@ public void setRequiredActions(KeycloakSession session, RealmModel realm, UserMo
7461
}
7562

7663
private Response createDuoForm(AuthenticationFlowContext context, String error) {
77-
String sig_request = DuoWeb.signRequest(duoIkey(context), duoSkey(context), akey, context.getUser().getUsername());
64+
String sig_request = DuoWeb.signRequest(duoIkey(context), duoSkey(context), duoAkey(context), context.getUser().getUsername());
7865
LoginFormsProvider form = context.form()
7966
.setAttribute("sig_request", sig_request)
8067
.setAttribute("apihost", duoApihost(context));
@@ -106,7 +93,7 @@ public void action(AuthenticationFlowContext context) {
10693
String sig_response = formData.getFirst("sig_response");
10794
String authenticated_username = null;
10895
try {
109-
authenticated_username = DuoWeb.verifyResponse(duoIkey(context), duoSkey(context), akey, sig_response);
96+
authenticated_username = DuoWeb.verifyResponse(duoIkey(context), duoSkey(context), duoAkey(context), sig_response);
11097
} catch (Exception ex) {
11198
context.failureChallenge(AuthenticationFlowError.INVALID_CREDENTIALS, createDuoForm(context, ex.getMessage()));
11299
return;
@@ -132,6 +119,11 @@ private String duoSkey(AuthenticationFlowContext context) {
132119
if (config == null) return "";
133120
return String.valueOf(config.getConfig().get(PROP_SKEY));
134121
}
122+
private String duoAkey(AuthenticationFlowContext context) {
123+
AuthenticatorConfigModel config = context.getAuthenticatorConfig();
124+
if (config == null) return "";
125+
return String.valueOf(config.getConfig().get(PROP_AKEY));
126+
}
135127
private String duoApihost(AuthenticationFlowContext context) {
136128
AuthenticatorConfigModel config = context.getAuthenticatorConfig();
137129
if (config == null) return "";

src/main/java/com/mulesoft/keycloak/auth/spi/duo/DuoMfaAuthenticatorFactory.java

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class DuoMfaAuthenticatorFactory implements AuthenticatorFactory{
3232
private static final DuoMfaAuthenticator SINGLETON = new DuoMfaAuthenticator();
3333
public static final String PROP_IKEY = "duomfa.ikey";
3434
public static final String PROP_SKEY = "duomfa.skey";
35+
public static final String PROP_AKEY = "duomfa.akey";
3536
public static final String PROP_APIHOST = "duomfa.apihost";
3637

3738
@Override
@@ -80,6 +81,13 @@ public boolean isConfigurable() {
8081
skey.setHelpText("Secret key from Duo admin portal");
8182
configProperties.add(skey);
8283

84+
ProviderConfigProperty akey = new ProviderConfigProperty();
85+
akey.setName(PROP_AKEY);
86+
akey.setLabel("Challenge nonce");
87+
akey.setType(ProviderConfigProperty.STRING_TYPE);
88+
akey.setHelpText("Any random alpha-numeric string, 40 characters or more. You can generate this yourself");
89+
configProperties.add(akey);
90+
8391
ProviderConfigProperty api_host = new ProviderConfigProperty();
8492
api_host.setName(PROP_APIHOST);
8593
api_host.setLabel("Duo WebSDK API host");

src/test/java/com/mulesoft/keycloak/auth/spi/duo/DuoMfaAuthenticatorTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public void testAuthenticate() {
3030
Map<String, String> m = new HashMap<>(3);
3131
m.put(PROP_IKEY, "XXXXXXXXXXXXXXXXXXXX");
3232
m.put(PROP_SKEY, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
33+
m.put(PROP_AKEY, "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
3334
m.put(PROP_APIHOST, "api-99999999.duosecurity.com");
3435

3536
AuthenticatorConfigModel t = mock(AuthenticatorConfigModel.class);
@@ -80,4 +81,4 @@ public void testAction() {
8081
verify(lfp, never()).setError(anyString());
8182
}
8283
*/
83-
}
84+
}

0 commit comments

Comments
 (0)