Skip to content

Commit

Permalink
Merge pull request #154 from Apicurio/add-scope
Browse files Browse the repository at this point in the history
Add scope to client credentials grant
  • Loading branch information
carlesarnal authored Jul 17, 2023
2 parents d5ad147 + e492479 commit 7ea5987
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.URLEncoder;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

Expand All @@ -41,6 +42,7 @@ public class OidcAuth implements Auth, AutoCloseable {

private final String clientId;
private final String clientSecret;
private final String scope;
private final Duration tokenExpirationReduction;

private String cachedAccessToken;
Expand All @@ -53,9 +55,14 @@ public OidcAuth(ApicurioHttpClient httpClient, String clientId, String clientSec
}

public OidcAuth(ApicurioHttpClient httpClient, String clientId, String clientSecret, Duration tokenExpirationReduction) {
this(httpClient, clientId, clientSecret, DEFAULT_TOKEN_EXPIRATION_REDUCTION, null);
}

public OidcAuth(ApicurioHttpClient httpClient, String clientId, String clientSecret, Duration tokenExpirationReduction, String scope) {
this.clientId = clientId;
this.clientSecret = clientSecret;
this.apicurioHttpClient = httpClient;
this.scope = scope;
if (null == tokenExpirationReduction) {
this.tokenExpirationReduction = DEFAULT_TOKEN_EXPIRATION_REDUCTION;
} else {
Expand All @@ -76,7 +83,15 @@ public void apply(Map<String, String> requestHeaders) {

private void requestAccessToken() {
try {
final Map<String, String> params = Map.of("grant_type", CLIENT_CREDENTIALS_GRANT, "client_id", clientId, "client_secret", clientSecret);
final Map<String, String> params = new HashMap<>();
params.put("grant_type", CLIENT_CREDENTIALS_GRANT);
params.put("client_id", clientId);
params.put("client_secret", clientSecret);

if (scope != null) {
params.put("scope", scope);
}

final String paramsEncoded = params.entrySet().stream().map(entry -> String.join("=",
URLEncoder.encode(entry.getKey(), UTF_8),
URLEncoder.encode(entry.getValue(), UTF_8))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class AuthTest {
private static ApicurioHttpClient httpClient;

private OidcAuth createOidcAuth(String adminClientId) {
return new OidcAuth(httpClient, adminClientId, "test1", Duration.ofSeconds(18));
return new OidcAuth(httpClient, adminClientId, "test1", Duration.ofSeconds(18), "openid profile email");
}

@BeforeAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ public static void close() {
}

private OidcAuth createOidcAuth(String adminClientId) {
return new OidcAuth(httpClient, adminClientId, "test1", Duration.ofSeconds(18));
return new OidcAuth(httpClient, adminClientId, "test1", Duration.ofSeconds(18), "openid profile email");
}
}

0 comments on commit 7ea5987

Please sign in to comment.