Skip to content

Commit

Permalink
getAcceptableAuthMethods returns a Set
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed May 22, 2024
1 parent 9ce9554 commit 418a9e6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 59 deletions.
48 changes: 10 additions & 38 deletions src/main/java/com/vonage/client/AbstractMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
* Abstract class to assist in implementing a call against a REST endpoint.
Expand All @@ -50,9 +49,7 @@ public abstract class AbstractMethod<RequestT, ResultT> implements RestEndpoint<
}

protected static final BasicResponseHandler basicResponseHandler = new BasicResponseHandler();

protected final HttpWrapper httpWrapper;
private Set<Class<? extends AuthMethod>> acceptable;

public AbstractMethod(HttpWrapper httpWrapper) {
this.httpWrapper = httpWrapper;
Expand Down Expand Up @@ -115,15 +112,12 @@ protected RequestBuilder applyAuth(RequestBuilder request) throws VonageClientEx
AuthMethod am = getAuthMethod();

if (am instanceof SignatureAuthMethod) {
Map<String, String> params = request.getParameters().stream().collect(Collectors.toMap(
NameValuePair::getName,
NameValuePair::getValue,
(v1, v2) -> v1,
TreeMap::new
));
((SignatureAuthMethod) am).apply(params);
request.getParameters().clear();
params.forEach(request::addParameter);
Map<String, String> paramsMap = new TreeMap<>();
List<NameValuePair> reqParams = request.getParameters();
reqParams.forEach(nvp -> paramsMap.put(nvp.getName(), nvp.getValue()));
((SignatureAuthMethod) am).apply(paramsMap);
reqParams.clear();
paramsMap.forEach(request::addParameter);
}
else if (am instanceof HeaderAuthMethod) {
return request.setHeader("Authorization", ((HeaderAuthMethod) am).getHeaderValue());
Expand All @@ -135,35 +129,13 @@ else if (am instanceof QueryParamsAuthMethod) {
}

/**
* Utility method for obtaining an appropriate {@link AuthMethod} for this call.
*
* @param acceptableAuthMethods an array of classes, representing authentication methods that are acceptable for
* this endpoint
*
* @return An AuthMethod created from one of the provided acceptableAuthMethods.
*
* @throws VonageClientException If no AuthMethod is available from the provided array of acceptableAuthMethods.
*/
@SuppressWarnings("unchecked")
private AuthMethod getAuthMethod(Class<?>[] acceptableAuthMethods) throws VonageClientException {
if (acceptable == null) {
acceptable = Arrays.stream(acceptableAuthMethods)
.filter(AuthMethod.class::isAssignableFrom)
.map(c -> (Class<? extends AuthMethod>) c)
.collect(Collectors.toSet());
}

return httpWrapper.getAuthCollection().getAcceptableAuthMethod(acceptable);
}

/**
* Gets the highest priority available authentication method according to {@link AuthMethod#getSortKey()}.
* Gets the highest priority available authentication method according to its sort key.
*
* @return An AuthMethod created from the accepted auth methods.
* @throws VonageUnexpectedException If no AuthMethod is available.
*/
protected AuthMethod getAuthMethod() throws VonageUnexpectedException {
return getAuthMethod(getAcceptableAuthMethods());
return httpWrapper.getAuthCollection().getAcceptableAuthMethod(getAcceptableAuthMethods());
}

/**
Expand All @@ -187,7 +159,7 @@ public String getApplicationIdOrApiKey() throws VonageUnexpectedException {
throw new IllegalStateException(am.getClass().getSimpleName() + " does not have API key.");
}

protected abstract Class<?>[] getAcceptableAuthMethods();
protected abstract Set<Class<? extends AuthMethod>> getAcceptableAuthMethods();

/**
* Construct and return a RequestBuilder instance from the provided request.
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/com/vonage/client/DynamicEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Consumer;

Expand All @@ -41,7 +38,7 @@
*/
@SuppressWarnings("unchecked")
public class DynamicEndpoint<T, R> extends AbstractMethod<T, R> {
protected Collection<Class<? extends AuthMethod>> authMethods;
protected Set<Class<? extends AuthMethod>> authMethods;
protected String contentType, accept;
protected HttpMethod requestMethod;
protected BiFunction<DynamicEndpoint<T, R>, ? super T, String> pathGetter;
Expand Down Expand Up @@ -86,7 +83,7 @@ public static <T, R> Builder<T, R> builder(Class<R> responseType) {

public static final class Builder<T, R> {
private final Class<R> responseType;
private Collection<Class<? extends AuthMethod>> authMethods;
private Set<Class<? extends AuthMethod>> authMethods;
private HttpWrapper wrapper;
private String contentType, accept;
private HttpMethod requestMethod;
Expand All @@ -113,7 +110,7 @@ public Builder<T, R> pathGetter(BiFunction<DynamicEndpoint<T, R>, T, String> pat
}

public Builder<T, R> authMethod(Class<? extends AuthMethod> primary, Class<? extends AuthMethod>... others) {
authMethods = new ArrayList<>(2);
authMethods = new LinkedHashSet<>(2);
authMethods.add(Objects.requireNonNull(primary, "Primary auth method cannot be null"));
if (others != null) {
for (Class<? extends AuthMethod> amc : others) {
Expand Down Expand Up @@ -161,9 +158,8 @@ static RequestBuilder createRequestBuilderFromRequestMethod(HttpMethod requestMe
}

@Override
protected final Class<?>[] getAcceptableAuthMethods() {
Class<?>[] emptyArray = new Class<?>[0];
return authMethods != null ? authMethods.toArray(emptyArray) : emptyArray;
protected final Set<Class<? extends AuthMethod>> getAcceptableAuthMethods() {
return authMethods;
}

private boolean isJsonableArrayResponse() {
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/com/vonage/client/AbstractMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

Expand All @@ -52,8 +53,8 @@ public ConcreteMethod(HttpWrapper httpWrapper) {
}

@Override
protected Class<?>[] getAcceptableAuthMethods() {
return new Class<?>[]{JWTAuthMethod.class};
protected Set<Class<? extends AuthMethod>> getAcceptableAuthMethods() {
return Set.of(JWTAuthMethod.class);
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/com/vonage/client/DynamicEndpointTestSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ private RequestBuilder makeTestRequest(T request) throws Exception {
}

protected void assertAcceptableAuthMethods() {
assertArrayEquals(expectedAuthMethods().toArray(), endpointAsAbstractMethod().getAcceptableAuthMethods());
assertArrayEquals(
expectedAuthMethods().toArray(),
endpointAsAbstractMethod().getAcceptableAuthMethods().toArray()
);
}

protected void assertErrorResponse(int statusCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
*/
package com.vonage.client.auth;

import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
import java.util.Base64;

public class ApiKeySecretAuthMethodTest {
Expand All @@ -28,10 +25,6 @@ public class ApiKeySecretAuthMethodTest {
public void testApplyApiKeyAndSecret() {
String key = "e4e5b6c3", secret = "0123456789abcdef";
var auth = new ApiKeyHeaderAuthMethod(key, secret);
String payload = "{\"name\":\"app name\"}";
RequestBuilder requestBuilder = RequestBuilder.get().setEntity(
new StringEntity(payload, ContentType.APPLICATION_JSON)
);

var header = auth.getHeaderValue();
var prefix = "Basic ";
Expand Down

0 comments on commit 418a9e6

Please sign in to comment.