-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServiceAuthTokenGenerator.java
40 lines (31 loc) · 1.33 KB
/
ServiceAuthTokenGenerator.java
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
package uk.gov.hmcts.reform.authorisation.generators;
import com.warrenstrange.googleauth.GoogleAuthenticator;
import uk.gov.hmcts.reform.authorisation.ServiceAuthorisationApi;
import java.util.HashMap;
import java.util.Map;
import static java.lang.String.format;
@SuppressWarnings("SummaryJavadoc")
public class ServiceAuthTokenGenerator implements AuthTokenGenerator {
private final String secret;
private final String microService;
private final ServiceAuthorisationApi serviceAuthorisationApi;
private final GoogleAuthenticator googleAuthenticator;
public ServiceAuthTokenGenerator(
final String secret,
final String microService,
final ServiceAuthorisationApi serviceAuthorisationApi
) {
this.secret = secret;
this.microService = microService;
this.serviceAuthorisationApi = serviceAuthorisationApi;
this.googleAuthenticator = new GoogleAuthenticator();
}
@Override
public String generate() {
final String oneTimePassword = format("%06d", googleAuthenticator.getTotpPassword(secret));
Map<String, String> signInDetails = new HashMap<>();
signInDetails.put("microservice", this.microService);
signInDetails.put("oneTimePassword", oneTimePassword);
return serviceAuthorisationApi.serviceToken(signInDetails);
}
}