24
24
import java .util .concurrent .locks .Lock ;
25
25
import java .util .concurrent .locks .ReentrantLock ;
26
26
27
- public class AttestationTokenRetriever {
28
- private static final Logger LOGGER = LoggerFactory .getLogger (AttestationTokenRetriever .class );
27
+ public class AttestationResponseHandler {
28
+ private static final Logger LOGGER = LoggerFactory .getLogger (AttestationResponseHandler .class );
29
29
30
30
private final IAttestationProvider attestationProvider ;
31
31
private final String clientApiToken ;
@@ -47,27 +47,28 @@ public class AttestationTokenRetriever {
47
47
private final AttestationTokenDecryptor attestationTokenDecryptor ;
48
48
private final String appVersionHeader ;
49
49
private final int attestCheckMilliseconds ;
50
-
51
- public AttestationTokenRetriever (Vertx vertx ,
52
- String attestationEndpoint ,
53
- String clientApiToken ,
54
- ApplicationVersion appVersion ,
55
- IAttestationProvider attestationProvider ,
56
- Handler <Pair <Integer , String >> responseWatcher ,
57
- Proxy proxy ) {
50
+ private final AtomicReference <String > optOutUrl ;
51
+
52
+ public AttestationResponseHandler (Vertx vertx ,
53
+ String attestationEndpoint ,
54
+ String clientApiToken ,
55
+ ApplicationVersion appVersion ,
56
+ IAttestationProvider attestationProvider ,
57
+ Handler <Pair <Integer , String >> responseWatcher ,
58
+ Proxy proxy ) {
58
59
this (vertx , attestationEndpoint , clientApiToken , appVersion , attestationProvider , responseWatcher , proxy , new InstantClock (), null , null , 60000 );
59
60
}
60
- public AttestationTokenRetriever (Vertx vertx ,
61
- String attestationEndpoint ,
62
- String clientApiToken ,
63
- ApplicationVersion appVersion ,
64
- IAttestationProvider attestationProvider ,
65
- Handler <Pair <Integer , String >> responseWatcher ,
66
- Proxy proxy ,
67
- IClock clock ,
68
- URLConnectionHttpClient httpClient ,
69
- AttestationTokenDecryptor attestationTokenDecryptor ,
70
- int attestCheckMilliseconds ) {
61
+ public AttestationResponseHandler (Vertx vertx ,
62
+ String attestationEndpoint ,
63
+ String clientApiToken ,
64
+ ApplicationVersion appVersion ,
65
+ IAttestationProvider attestationProvider ,
66
+ Handler <Pair <Integer , String >> responseWatcher ,
67
+ Proxy proxy ,
68
+ IClock clock ,
69
+ URLConnectionHttpClient httpClient ,
70
+ AttestationTokenDecryptor attestationTokenDecryptor ,
71
+ int attestCheckMilliseconds ) {
71
72
this .vertx = vertx ;
72
73
this .attestationEndpoint = attestationEndpoint ;
73
74
this .encodedAttestationEndpoint = this .encodeStringUnicodeAttestationEndpoint (attestationEndpoint );
@@ -77,6 +78,7 @@ public AttestationTokenRetriever(Vertx vertx,
77
78
this .attestationToken = new AtomicReference <>(null );
78
79
this .optOutJwt = new AtomicReference <>(null );
79
80
this .coreJwt = new AtomicReference <>(null );
81
+ this .optOutUrl = new AtomicReference <>(null );
80
82
this .responseWatcher = responseWatcher ;
81
83
this .clock = clock ;
82
84
this .lock = new ReentrantLock ();
@@ -125,7 +127,7 @@ private void attestationExpirationCheck(long timerId) {
125
127
}
126
128
127
129
attest ();
128
- } catch (AttestationTokenRetrieverException e ) {
130
+ } catch (AttestationResponseHandlerException e ) {
129
131
notifyResponseWatcher (401 , e .getMessage ());
130
132
LOGGER .info ("Re-attest failed: " , e );
131
133
} catch (IOException e ){
@@ -144,9 +146,9 @@ private void scheduleAttestationExpirationCheck() {
144
146
}
145
147
}
146
148
147
- public void attest () throws IOException , AttestationTokenRetrieverException {
149
+ public void attest () throws IOException , AttestationResponseHandlerException {
148
150
if (!attestationProvider .isReady ()) {
149
- throw new AttestationTokenRetrieverException ("attestation provider is not ready" );
151
+ throw new AttestationResponseHandlerException ("attestation provider is not ready" );
150
152
}
151
153
152
154
try {
@@ -177,26 +179,26 @@ public void attest() throws IOException, AttestationTokenRetrieverException {
177
179
178
180
if (statusCode < 200 || statusCode >= 300 ) {
179
181
LOGGER .warn ("attestation failed with UID2 Core returning statusCode=" + statusCode );
180
- throw new AttestationTokenRetrieverException (statusCode , "unexpected status code from uid core service" );
182
+ throw new AttestationResponseHandlerException (statusCode , "unexpected status code from uid core service" );
181
183
}
182
184
183
185
JsonObject responseJson = (JsonObject ) Json .decodeValue (responseBody );
184
186
if (isFailed (responseJson )) {
185
- throw new AttestationTokenRetrieverException (statusCode , "response did not return a successful status" );
187
+ throw new AttestationResponseHandlerException (statusCode , "response did not return a successful status" );
186
188
}
187
189
188
190
JsonObject innerBody = responseJson .getJsonObject ("body" );
189
191
if (innerBody == null ) {
190
- throw new AttestationTokenRetrieverException (statusCode , "response did not contain a body object" );
192
+ throw new AttestationResponseHandlerException (statusCode , "response did not contain a body object" );
191
193
}
192
194
193
195
String atoken = getAttestationToken (innerBody );
194
196
if (atoken == null ) {
195
- throw new AttestationTokenRetrieverException (statusCode , "response json does not contain body.attestation_token" );
197
+ throw new AttestationResponseHandlerException (statusCode , "response json does not contain body.attestation_token" );
196
198
}
197
199
String expiresAt = getAttestationTokenExpiresAt (innerBody );
198
200
if (expiresAt == null ) {
199
- throw new AttestationTokenRetrieverException (statusCode , "response json does not contain body.expiresAt" );
201
+ throw new AttestationResponseHandlerException (statusCode , "response json does not contain body.expiresAt" );
200
202
}
201
203
202
204
atoken = new String (attestationTokenDecryptor .decrypt (Base64 .getDecoder ().decode (atoken ), keyPair .getPrivate ()), StandardCharsets .UTF_8 );
@@ -205,12 +207,13 @@ public void attest() throws IOException, AttestationTokenRetrieverException {
205
207
setAttestationTokenExpiresAt (expiresAt );
206
208
setOptoutJWTFromResponse (innerBody );
207
209
setCoreJWTFromResponse (innerBody );
210
+ setOptoutURLFromResponse (innerBody );
208
211
209
212
scheduleAttestationExpirationCheck ();
210
213
} catch (IOException ioe ) {
211
214
throw ioe ;
212
215
} catch (Exception e ) {
213
- throw new AttestationTokenRetrieverException (e );
216
+ throw new AttestationResponseHandlerException (e );
214
217
}
215
218
}
216
219
@@ -230,6 +233,10 @@ public String getCoreJWT() {
230
233
return this .coreJwt .get ();
231
234
}
232
235
236
+ public String getOptOutUrl () {
237
+ return this .optOutUrl .get ();
238
+ }
239
+
233
240
public String getAppVersionHeader () {
234
241
return this .appVersionHeader ;
235
242
}
@@ -266,6 +273,17 @@ private void setCoreJWTFromResponse(JsonObject responseBody) {
266
273
}
267
274
}
268
275
276
+ private void setOptoutURLFromResponse (JsonObject responseBody ) {
277
+ String url = responseBody .getString ("optout_url" );
278
+ if (url == null ) {
279
+ LOGGER .info ("OptOut URL not received" );
280
+ } else {
281
+ LOGGER .info ("OptOut URL received" );
282
+ LOGGER .debug ("OptOut URL to use: {}" , url );
283
+ this .optOutUrl .set (url );
284
+ }
285
+ }
286
+
269
287
private static boolean isFailed (JsonObject responseJson ) {
270
288
return responseJson .getString ("status" ) == null || !responseJson .getString ("status" ).equals ("success" );
271
289
}
0 commit comments