-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMocClient.java
More file actions
95 lines (70 loc) · 3.2 KB
/
MocClient.java
File metadata and controls
95 lines (70 loc) · 3.2 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package city.makeour.moc;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import org.springframework.web.client.RestClient.ResponseSpec;
import city.makeour.moc.ngsiv2.Ngsiv2Client;
import city.makeour.ngsi.v2.api.EntitiesApi;
import city.makeour.ngsi.v2.invoker.ApiClient;
public class MocClient {
protected Ngsiv2Client client;
protected TokenFetcherInterface tokenFetcher;
protected RefreshTokenStorageInterface refreshTokenStorage;
public MocClient() {
this("https://orion.sandbox.makeour.city");
}
public MocClient(String basePath) {
ApiClient apiClient = new ApiClient();
apiClient.setBasePath(basePath);
this.client = new Ngsiv2Client(apiClient);
this.refreshTokenStorage = new RefreshTokenStorage();
}
public EntitiesApi entities() {
return this.client.getEntitiesApi();
}
public void setMocAuthInfo(String cognitoUserPoolId, String cognitoClientId) {
this.tokenFetcher = new FetchCognitoToken(cognitoUserPoolId, cognitoClientId);
}
public void auth(String username, String password) throws InvalidKeyException, NoSuchAlgorithmException {
if (this.tokenFetcher == null) {
throw new IllegalStateException("MocClient is not initialized with Cognito auth info.");
}
this.tokenFetcher.setAuthParameters(username, password);
if (this.refreshTokenStorage.hasRefreshToken()) {
RefreshTokenInterface refreshToken = this.refreshTokenStorage.getRefreshToken();
if (!refreshToken.isExpired()) {
this.refreshToken();
}
return;
}
this.login(username, password);
}
public void login(String username, String password) throws InvalidKeyException, NoSuchAlgorithmException {
if (this.tokenFetcher == null) {
throw new IllegalStateException("MocClient is not initialized with Cognito auth info.");
}
this.tokenFetcher.setAuthParameters(username, password);
Token token = this.tokenFetcher.fetchTokenWithSrpAuth();
this.setToken(token.getIdToken());
this.refreshTokenStorage.setRefreshToken(token);
}
public void refreshToken() throws InvalidKeyException, NoSuchAlgorithmException {
if (this.tokenFetcher == null) {
throw new IllegalStateException("MocClient is not initialized with Cognito auth info.");
}
if (!this.refreshTokenStorage.hasRefreshToken()) {
throw new IllegalStateException("No refresh token available.");
}
RefreshTokenInterface refreshToken = this.refreshTokenStorage.getRefreshToken();
Token token = this.tokenFetcher.refleshToken(refreshToken.getRefreshToken());
this.setToken(token.getIdToken());
}
public void setToken(String token) {
this.client.getApiClient().addDefaultHeader("Authorization", token);
}
public void setFiwareService(String fiwareService) {
this.client.getApiClient().addDefaultHeader("Fiware-Service", fiwareService);
}
public ResponseSpec createEntity(String contentType, Object body) {
return this.client.createEntity(contentType, body);
}
}