Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Move ClientInfo out of class
Browse files Browse the repository at this point in the history
  • Loading branch information
Demogorgon314 committed Jul 4, 2023
1 parent 17beaed commit 879f342
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,15 @@
*/
package io.streamnative.pulsar.handlers.kop.security.oauth;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
* The client configs associated with OauthLoginCallbackHandler.
Expand Down Expand Up @@ -77,42 +69,15 @@ public ClientConfig(Map<String, String> configs) {
"invalid %s \"%s\": %s", OAUTH_CREDENTIALS_URL, credentialsUrlString, e.getMessage()));
}
try {
this.clientInfo = loadPrivateKey();
final URLConnection connection = getCredentialsUrl().openConnection();
try (InputStream inputStream = connection.getInputStream()) {
this.clientInfo = CLIENT_INFO_READER.readValue(inputStream);
}
} catch (IOException e) {
throw new IllegalArgumentException(String.format(
"failed to load client credentials from %s: %s", credentialsUrlString, e.getMessage()));
}
this.audience = configs.getOrDefault(OAUTH_AUDIENCE, null);
this.scope = configs.getOrDefault(OAUTH_SCOPE, null);
}

@VisibleForTesting
ClientInfo loadPrivateKey() throws IOException {
final URLConnection connection = getCredentialsUrl().openConnection();
try (InputStream inputStream = connection.getInputStream()) {
return CLIENT_INFO_READER.readValue(inputStream);
}
}

@Getter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class ClientInfo {

@JsonProperty("client_id")
private String id;

@JsonProperty("client_secret")
private String secret;

@JsonProperty("tenant")
private String tenant;

@JsonProperty("group_id")
private String groupId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected ClientCredentialsFlow(ClientConfig clientConfig, AsyncHttpClient httpC

public OAuthBearerTokenImpl authenticate() throws IOException {
final String tokenEndPoint = findAuthorizationServer().getTokenEndPoint();
final ClientConfig.ClientInfo clientInfo = clientConfig.getClientInfo();
final ClientInfo clientInfo = clientConfig.getClientInfo();
try {
final String body = buildClientCredentialsBody(clientInfo);
final Response response = httpClient.preparePost(tokenEndPoint)
Expand Down Expand Up @@ -128,7 +128,7 @@ private static String encode(String s) throws UnsupportedEncodingException {
return URLEncoder.encode(s, StandardCharsets.UTF_8.name());
}

private String buildClientCredentialsBody(ClientConfig.ClientInfo clientInfo) throws UnsupportedEncodingException {
private String buildClientCredentialsBody(ClientInfo clientInfo) throws UnsupportedEncodingException {
final Map<String, String> bodyMap = new HashMap<>();
bodyMap.put("grant_type", "client_credentials");
bodyMap.put("client_id", encode(clientInfo.getId()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.streamnative.pulsar.handlers.kop.security.oauth;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ClientInfo {

@JsonProperty("client_id")
private String id;

@JsonProperty("client_secret")
private String secret;

@JsonProperty("tenant")
private String tenant;

@JsonProperty("group_id")
private String groupId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void handleCallback(OAuthBearerTokenCallback callback) throws IOExceptio
private void handleExtensionsCallback(SaslExtensionsCallback callback) {

Map<String, String> extensions = new HashMap<>();
ClientConfig.ClientInfo clientInfo = clientConfig.getClientInfo();
ClientInfo clientInfo = clientConfig.getClientInfo();

if (clientInfo.getTenant() != null) {
extensions.put("tenant", clientInfo.getTenant());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void testValidConfig() {
Assert.assertEquals(clientConfig.getCredentialsUrl().toString(), credentialsUrl);
Assert.assertEquals(clientConfig.getAudience(), "audience");
Assert.assertEquals(clientConfig.getClientInfo(),
new ClientConfig.ClientInfo("my-id", "my-secret", "my-tenant", null));
new ClientInfo("my-id", "my-secret", "my-tenant", null));
}

@Test
Expand All @@ -55,7 +55,7 @@ public void testValidConfigWithGroupId() {
Assert.assertEquals(clientConfig.getCredentialsUrl().toString(), credentialsUrl);
Assert.assertEquals(clientConfig.getAudience(), "audience");
Assert.assertEquals(clientConfig.getClientInfo(),
new ClientConfig.ClientInfo("my-id", "my-secret", "my-tenant", "my-group-id"));
new ClientInfo("my-id", "my-secret", "my-tenant", "my-group-id"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void testLoadPrivateKey() {
Objects.requireNonNull(
getClass().getClassLoader().getResource("private_key.json")).toString()
);
ClientConfig.ClientInfo clientInfo = clientConfig.getClientInfo();
ClientInfo clientInfo = clientConfig.getClientInfo();
Assert.assertEquals(clientInfo.getId(), "my-id");
Assert.assertEquals(clientInfo.getSecret(), "my-secret");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import io.fusionauth.jwks.domain.JSONWebKey;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import io.streamnative.pulsar.handlers.kop.security.oauth.ClientConfig;
import io.streamnative.pulsar.handlers.kop.security.oauth.ClientInfo;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
Expand All @@ -46,7 +46,7 @@ public class HydraOAuthUtils {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

private static final ObjectWriter CLIENT_INFO_WRITER =
OBJECT_MAPPER.writerFor(ClientConfig.ClientInfo.class);
OBJECT_MAPPER.writerFor(ClientInfo.class);

private static String publicKey;

Expand Down Expand Up @@ -121,8 +121,7 @@ public static String writeCredentialsFile(String clientId,
String tenant,
String groupId,
String basename) throws IOException {
ClientConfig.ClientInfo clientInfo =
new ClientConfig.ClientInfo(clientId, clientSecret, tenant, groupId);
ClientInfo clientInfo = new ClientInfo(clientId, clientSecret, tenant, groupId);
final String content = CLIENT_INFO_WRITER.writeValueAsString(clientInfo);

File file = File.createTempFile("oauth-credentials-", basename);
Expand Down

0 comments on commit 879f342

Please sign in to comment.