Skip to content

Commit

Permalink
Sjekk at respons i hentToken har statuskode 2xx (#1383)
Browse files Browse the repository at this point in the history
  • Loading branch information
dijjal committed Sep 11, 2024
1 parent db90070 commit 8f5def5
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static OidcTokenResponse hentTokenRetryable(HttpRequest request, URI prox
public static OidcTokenResponse hentToken(HttpRequest request, URI proxy) {
try (var client = hentEllerByggHttpClient(proxy)) { // På sikt vurder å bruke en generell klient eller å cache. De er blitt autocloseable
var response = client.send(request, HttpResponse.BodyHandlers.ofString(UTF_8));
if (response == null || response.body() == null) {
if (response == null || response.body() == null || !responskode2xx(response)) {
throw new TekniskException("F-157385", "Kunne ikke hente token");
}
return READER.readValue(response.body());
Expand All @@ -55,6 +55,11 @@ public static OidcTokenResponse hentToken(HttpRequest request, URI proxy) {
}
}

private static boolean responskode2xx(HttpResponse<String> response) {
var status = response.statusCode();
return status >= 200 && status < 300;
}

private static HttpClient hentEllerByggHttpClient(URI proxy) {
return HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NEVER)
Expand Down

0 comments on commit 8f5def5

Please sign in to comment.