-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIBMAuthenticator.java
More file actions
80 lines (68 loc) · 2.81 KB
/
IBMAuthenticator.java
File metadata and controls
80 lines (68 loc) · 2.81 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.UoB.AILearningTool;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONObject;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class IBMAuthenticator extends Thread {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private final Logger log = LoggerFactory.getLogger(SpringController.class);
private String bearerToken;
private Integer statusCode;
// Returns a bearer token
public String getBearerToken() {
if (this.statusCode == null) {
System.out.println("Token is not available yet. Requesting a new token.");
requestNewToken();
}
return this.bearerToken;
}
// Return status code of the latest request
public int getStatusCode() {
if (this.statusCode == null) {
requestNewToken();
}
return this.statusCode;
}
// Uses IBM IAM API key to receive a temporary Bearer token.
public void requestNewToken() {
final String API_KEY = "wXU_-wyEW1tG1S8n4T3-6eiZ70Pfc2WxqXwqExsorjDH";
// Prepare a request with an API key to receive a Bearer token.
HttpRequest request = HttpRequest.newBuilder(URI.create("https://iam.cloud.ibm.com/identity/token"))
.headers("Content-Type", "application/x-www-form-urlencoded")
.POST(
HttpRequest.BodyPublishers.ofString(
"grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=" + API_KEY
)
).build();
try {
HttpResponse<String> response = HttpClient.newBuilder()
.build()
.send(request, HttpResponse.BodyHandlers.ofString());
this.statusCode = response.statusCode();
if (this.statusCode == 200) {
this.bearerToken = new JSONObject(response.body()).getString("access_token");
log.info("New Bearer token obtained.");
}
} catch (Exception e) {
this.bearerToken = null;
this.statusCode = 500;
log.error("Exception {}\nHTTP status code: {}", e, this.statusCode.toString());
}
}
// Finishes the current request and stops the scheduler.
public void stopTimer() {
this.scheduler.shutdown();
log.info("Scheduler stopped.");
}
// Runs a scheduler that executes requestNewToken() in specified frequency.
public void run() {
this.scheduler.scheduleAtFixedRate(this::requestNewToken, 0, 58, TimeUnit.MINUTES);
}
}