All Downloads are FREE. Search and download functionalities are using the official Maven repository.

templates.java-micronaut.client.auth.configuration.ApiKeyAuthConfiguration.mustache Maven / Gradle / Ivy

{{>common/licenseInfo}}
package {{invokerPackage}}.auth.configuration;

import io.micronaut.context.annotation.ConfigurationInject;
import io.micronaut.context.annotation.EachProperty;
import io.micronaut.context.annotation.Parameter;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.http.MutableHttpRequest;
import io.micronaut.http.cookie.Cookie;
{{#generatedAnnotation}}
import {{javaxPackage}}.annotation.Generated;
{{/generatedAnnotation}}
{{#lombok}}
import lombok.Getter;
import lombok.Setter;
{{/lombok}}

{{#generatedAnnotation}}
{{>common/generatedAnnotation}}
{{/generatedAnnotation}}
{{#lombok}}
@Getter
@Setter
{{/lombok}}
@EachProperty("security.api-key-auth")
public class ApiKeyAuthConfiguration implements ConfigurableAuthorization {

    private final String name;
    private AuthKeyLocation location;
    private String paramName;
    private String apiKey;

    @ConfigurationInject
    public ApiKeyAuthConfiguration(
            @Parameter String name,
            @NonNull AuthKeyLocation location,
            @NonNull String paramName,
            @NonNull String apiKey
    ) {
        this.name = name;
        this.location = location;
        this.paramName = paramName;
        this.apiKey = apiKey;
    }

    @Override
    public void applyAuthorization(@NonNull MutableHttpRequest request) {
        if (location == AuthKeyLocation.HEADER) {
            request.header(paramName, apiKey);
        } else if (location == AuthKeyLocation.QUERY) {
            request.getParameters().add(paramName, apiKey);
        } else if (location == AuthKeyLocation.COOKIE) {
            request.cookie(Cookie.of(paramName, apiKey));
        }
    }
{{^lombok}}

    @Override
    public String getName() {
        return name;
    }

    public AuthKeyLocation getLocation() {
        return location;
    }

    public String getParamName() {
        return paramName;
    }

    public String getApiKey() {
        return apiKey;
    }

    public void setLocation(AuthKeyLocation location) {
        this.location = location;
    }

    public void setParamName(String paramName) {
        this.paramName = paramName;
    }

    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }
{{/lombok}}

    public enum AuthKeyLocation {
        HEADER,
        QUERY,
        COOKIE
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy