41
41
42
42
public class DuoMfaAuthenticator implements Authenticator {
43
43
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 () {}
58
45
59
46
@ Override
60
47
public boolean requiresUser () {
@@ -74,7 +61,7 @@ public void setRequiredActions(KeycloakSession session, RealmModel realm, UserMo
74
61
}
75
62
76
63
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 ());
78
65
LoginFormsProvider form = context .form ()
79
66
.setAttribute ("sig_request" , sig_request )
80
67
.setAttribute ("apihost" , duoApihost (context ));
@@ -106,7 +93,7 @@ public void action(AuthenticationFlowContext context) {
106
93
String sig_response = formData .getFirst ("sig_response" );
107
94
String authenticated_username = null ;
108
95
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 );
110
97
} catch (Exception ex ) {
111
98
context .failureChallenge (AuthenticationFlowError .INVALID_CREDENTIALS , createDuoForm (context , ex .getMessage ()));
112
99
return ;
@@ -132,6 +119,11 @@ private String duoSkey(AuthenticationFlowContext context) {
132
119
if (config == null ) return "" ;
133
120
return String .valueOf (config .getConfig ().get (PROP_SKEY ));
134
121
}
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
+ }
135
127
private String duoApihost (AuthenticationFlowContext context ) {
136
128
AuthenticatorConfigModel config = context .getAuthenticatorConfig ();
137
129
if (config == null ) return "" ;
0 commit comments