-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthenticationProtocol.java
More file actions
61 lines (50 loc) · 1.77 KB
/
Copy pathAuthenticationProtocol.java
File metadata and controls
61 lines (50 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* (c)2021 CDP Technologies AS
*/
package com.cdptech.cdpclient;
import com.cdptech.cdpclient.proto.StudioAPI;
import com.cdptech.cdpclient.proto.StudioAPI.AuthResponse;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.Map;
class AuthenticationProtocol implements Protocol {
private Authenticator authenticator = new Authenticator();
private Transport transport;
private Runnable finishedCallback;
private String challenge;
AuthenticationProtocol(Transport transport, Runnable finishedCallback) {
this.transport = transport;
this.finishedCallback = finishedCallback;
}
@Override
public void parse(byte[] buf) {
try {
authenticator.updateUserAuthResult(AuthResponse.parseFrom(buf));
// The server asks for an EncryptedPassword response after the PasswordHash round. Answer it from
// the cached credentials.
StudioAPI.AuthRequest reissue = authenticator.encryptedPasswordReissueRequest(challenge);
if (reissue != null) {
transport.send(reissue.toByteArray());
} else {
finishedCallback.run();
}
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
}
}
void authenticate(String challenge, Map<String, String> data) {
this.challenge = challenge;
StudioAPI.AuthRequest authMessage = authenticator.createAuthMessage(challenge, data);
if (authMessage == null) {
finishedCallback.run();
} else {
transport.send(authMessage.toByteArray());
}
}
AuthRequest.UserAuthResult getUserAuthResult() {
return authenticator.getUserAuthResult();
}
/** Forget the credentials cached for this attempt (call once the attempt is granted). */
void clearCachedCredentials() {
authenticator.clearCachedCredentials();
}
}