Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowContext;
import io.serverlessworkflow.impl.WorkflowModel;
import java.net.URI;

public interface AuthProvider {

String authScheme();
String scheme();

String authParameter(WorkflowContext workflow, TaskContext task, WorkflowModel model);
String content(WorkflowContext workflow, TaskContext task, WorkflowModel model, URI uri);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,40 @@ public static Optional<AuthProvider> getAuth(
WorkflowDefinition definition, EndpointConfiguration configuration) {
return configuration == null
? Optional.empty()
: getAuth(definition, configuration.getAuthentication());
: getAuth(definition, configuration.getAuthentication(), "GET");
}

public static Optional<AuthProvider> getAuth(
WorkflowDefinition definition, ReferenceableAuthenticationPolicy auth) {
WorkflowDefinition definition, ReferenceableAuthenticationPolicy auth, String method) {
if (auth == null) {
return Optional.empty();
}
if (auth.getAuthenticationPolicyReference() != null) {
return buildFromReference(
definition.application(),
definition.workflow(),
auth.getAuthenticationPolicyReference().getUse());
auth.getAuthenticationPolicyReference().getUse(),
method);
} else if (auth.getAuthenticationPolicy() != null) {
return buildFromPolicy(
definition.application(), definition.workflow(), auth.getAuthenticationPolicy());
definition.application(), definition.workflow(), auth.getAuthenticationPolicy(), method);
}
return Optional.empty();
}

private static Optional<AuthProvider> buildFromReference(
WorkflowApplication app, Workflow workflow, String use) {
WorkflowApplication app, Workflow workflow, String use, String method) {
return workflow.getUse().getAuthentications().getAdditionalProperties().entrySet().stream()
.filter(s -> s.getKey().equals(use))
.findAny()
.flatMap(e -> buildFromPolicy(app, workflow, e.getValue()));
.flatMap(e -> buildFromPolicy(app, workflow, e.getValue(), method));
}

private static Optional<AuthProvider> buildFromPolicy(
WorkflowApplication app, Workflow workflow, AuthenticationPolicyUnion authenticationPolicy) {
WorkflowApplication app,
Workflow workflow,
AuthenticationPolicyUnion authenticationPolicy,
String method) {
if (authenticationPolicy.getBasicAuthenticationPolicy() != null) {
return Optional.of(
new BasicAuthProvider(
Expand All @@ -70,8 +74,9 @@ private static Optional<AuthProvider> buildFromPolicy(
new BearerAuthProvider(
app, workflow, authenticationPolicy.getBearerAuthenticationPolicy()));
} else if (authenticationPolicy.getDigestAuthenticationPolicy() != null) {
// TODO implement digest authentication
return Optional.empty();
return Optional.of(
new DigestAuthProvider(
app, workflow, authenticationPolicy.getDigestAuthenticationPolicy(), method));
} else if (authenticationPolicy.getOAuth2AuthenticationPolicy() != null) {
return Optional.of(
new OAuth2AuthProvider(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.serverlessworkflow.impl.auth;

import java.util.Random;

public class AuthUtils {

private AuthUtils() {}
Expand All @@ -38,7 +40,15 @@ private AuthUtils() {}

private static final String AUTH_HEADER_FORMAT = "%s %s";

private static class RandomHolder {
private static final Random random = new Random();
}

public static String authHeaderValue(String scheme, String parameter) {
return String.format(AUTH_HEADER_FORMAT, scheme, parameter);
}

public static String getRandomHexString() {
return String.format("%08x", RandomHolder.random.nextInt());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.WorkflowUtils;
import io.serverlessworkflow.impl.WorkflowValueResolver;
import java.net.URI;
import java.util.Base64;

class BasicAuthProvider implements AuthProvider {
Expand Down Expand Up @@ -57,7 +58,7 @@ public BasicAuthProvider(
}

@Override
public String authParameter(WorkflowContext workflow, TaskContext task, WorkflowModel model) {
public String content(WorkflowContext workflow, TaskContext task, WorkflowModel model, URI uri) {
return new String(
Base64.getEncoder()
.encode(
Expand All @@ -69,7 +70,7 @@ public String authParameter(WorkflowContext workflow, TaskContext task, Workflow
}

@Override
public String authScheme() {
public String scheme() {
return "Basic";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.WorkflowUtils;
import io.serverlessworkflow.impl.WorkflowValueResolver;
import java.net.URI;

class BearerAuthProvider implements AuthProvider {

Expand All @@ -48,12 +49,12 @@ public BearerAuthProvider(
}

@Override
public String authParameter(WorkflowContext workflow, TaskContext task, WorkflowModel model) {
public String content(WorkflowContext workflow, TaskContext task, WorkflowModel model, URI uri) {
return tokenFilter.apply(workflow, task, model);
}

@Override
public String authScheme() {
public String scheme() {
return "Bearer";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.serverlessworkflow.impl.WorkflowContext;
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.WorkflowValueResolver;
import java.net.URI;
import java.util.Arrays;
import java.util.Map;
import java.util.ServiceLoader;
Expand All @@ -48,12 +49,12 @@ protected CommonOAuthProvider(WorkflowValueResolver<AccessTokenProvider> tokenPr
}

@Override
public String authParameter(WorkflowContext workflow, TaskContext task, WorkflowModel model) {
public String content(WorkflowContext workflow, TaskContext task, WorkflowModel model, URI uri) {
return tokenProvider.apply(workflow, task, model).validateAndGet(workflow, task, model).token();
}

@Override
public String authScheme() {
public String scheme() {
return "Bearer";
}

Expand Down
Loading