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

openapi.templates.kora.javaServerSecuritySchema.mustache Maven / Gradle / Ivy

There is a newer version: 1.1.16
Show newest version
/**
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech) ({{{generatorVersion}}}).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */
package {{apiPackage}};

import java.util.function.Function;

import ru.tinkoff.kora.common.Context;
import ru.tinkoff.kora.common.DefaultComponent;
import ru.tinkoff.kora.common.Principal;

import ru.tinkoff.kora.config.common.extractor.ConfigValueExtractor;
import ru.tinkoff.kora.config.common.Config;
import ru.tinkoff.kora.http.server.common.HttpServerInterceptor;
import ru.tinkoff.kora.http.server.common.HttpServerInterceptor.InterceptChain;
import ru.tinkoff.kora.http.server.common.HttpServerRequest;
import ru.tinkoff.kora.http.server.common.HttpServerResponse;
import ru.tinkoff.kora.http.server.common.HttpServerResponseException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

import ru.tinkoff.kora.http.common.auth.*;
import ru.tinkoff.kora.http.server.common.auth.*;



@ru.tinkoff.kora.common.annotation.Generated("openapi generator kora server")
@ru.tinkoff.kora.common.Module
public interface ApiSecurity {
{{#vendorExtensions.tags}}

    static final class {{.}} {}
{{/vendorExtensions.tags}}

{{#vendorExtensions.authMethods}}
    @ru.tinkoff.kora.common.annotation.Generated("openapi generator kora server")
    static final class {{name}}HttpServerInterceptor implements HttpServerInterceptor {

    {{#methods}}
        private final HttpServerPrincipalExtractor<{{#isOAuth}}PrincipalWithScopes{{/isOAuth}}{{^isOAuth}}Principal{{/isOAuth}}> {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}};
    {{/methods}}

        public {{name}}HttpServerInterceptor({{#methods}}
            @ru.tinkoff.kora.common.Tag(ApiSecurity.{{#lambda.classname}}{{name}}{{/lambda.classname}}.class) HttpServerPrincipalExtractor<{{#isOAuth}}PrincipalWithScopes{{/isOAuth}}{{^isOAuth}}Principal{{/isOAuth}}> {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{^vendorExtensions.isLast}},{{/vendorExtensions.isLast}}{{/methods}}) {
            {{#methods}}
            this.{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}} = {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}};
            {{/methods}}
        }

        @Override
        public CompletionStage intercept(Context ctx, HttpServerRequest request, InterceptChain chain) {
            var future = new CompletableFuture();
            {{#methods}}{{#vendorExtensions.isFirst}}{{#isApiKey}}{{#isKeyInHeader}}
            var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_token = request.headers().getFirst("{{keyParamName}}");{{/isKeyInHeader}}{{#isKeyInQuery}}
            var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_query = request.queryParams().get("{{keyParamName}}");
            var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_token = {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_query == null || {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_query.isEmpty()
              ? null
              : {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_query.iterator().next();{{/isKeyInQuery}}{{#isKeyInCookie}}
            var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_token = request.cookies().stream().filter(c -> "{{keyParamName}}".equals(c.name())).map(c -> c.value()).findFirst().orElse(null);
            {{/isKeyInCookie}}
            {{/isApiKey}}{{#isBasicBasic}}
            var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_token = request.headers().getFirst("Authorization");{{/isBasicBasic}}{{#isBasicBearer}}
            var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_token = request.headers().getFirst("Authorization");{{/isBasicBearer}}{{#isOAuth}}
            var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_token = request.headers().getFirst("Authorization");{{/isOAuth}}
            var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}} = this.{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}.extract(request, {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_token){{^isOAuth}};{{/isOAuth}}{{#isOAuth}}{{#vendorExtensions.hasScopes}}
              .thenCompose((principal) -> { {{#scopes}}
                 if (!principal.scopes().contains("{{scope}}")) {
                    return CompletableFuture.failedFuture(HttpServerResponseException.of(403, "Forbidden"));
                 }{{/scopes}}
                 return CompletableFuture.completedFuture(principal);
              }){{/vendorExtensions.hasScopes}};{{/isOAuth}}

            {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}.whenComplete(({{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_principal, {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_error) -> {
                if ({{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_error != null) {
                    future.completeExceptionally({{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_error);
                } else if ({{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_principal != null) {
                    future.complete({{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_principal);
                } else {
                  {{#vendorExtensions.isLast}}
                  future.completeExceptionally(HttpServerResponseException.of(403, "Forbidden"));
                  {{/vendorExtensions.isLast}}
            {{/vendorExtensions.isFirst}}{{/methods}}{{#methods}}{{^vendorExtensions.isFirst}}{{#lambda.indented}}{{#isApiKey}}{{#isKeyInHeader}}
                var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_token = request.headers().getFirst("{{keyParamName}}");{{/isKeyInHeader}}{{#isKeyInQuery}}
                var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_query = request.queryParams().get("{{keyParamName}}");
                var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_token = {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_query == null || {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_query.isEmpty()
                  ? null
                  : {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_query.iterator().next();{{/isKeyInQuery}}{{#isKeyInCookie}}
                var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_token = request.cookies().stream().filter(c -> "{{keyParamName}}".equals(c.name())).map(c -> c.value()).findFirst().orElse(null);{{/isKeyInCookie}}{{/isApiKey}}{{#isBasicBasic}}
                var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_token = request.headers().getFirst("Authorization");{{/isBasicBasic}}{{#isBasicBearer}}
                var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_token = request.headers().getFirst("Authorization");{{/isBasicBearer}}{{#isOAuth}}
                var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_token = request.headers().getFirst("Authorization");{{/isOAuth}}
                var {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}} = this.{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}.extract(request, {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_token){{^isOAuth}};{{/isOAuth}}{{#isOAuth}}{{#vendorExtensions.hasScopes}}
                  .thenCompose((principal) -> { {{#scopes}}
                     if (!principal.scopes().contains("{{scope}}")) {
                        return CompletableFuture.failedFuture(HttpServerResponseException.of(403, "Forbidden"));
                     }{{/scopes}}
                     return CompletableFuture.completedFuture(principal);
                  }){{/vendorExtensions.hasScopes}};{{/isOAuth}}

                {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}.whenComplete(({{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_principal, {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_error) -> {
                    if ({{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_error != null) {
                        future.completeExceptionally({{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_error);
                    } else if ({{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_principal != null) {
                        future.complete({{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}_principal);
                    } else {
                      {{#vendorExtensions.isLast}}
                      future.completeExceptionally(HttpServerResponseException.of(403, "Forbidden"));
                      {{/vendorExtensions.isLast}}
            {{/lambda.indented}}{{/vendorExtensions.isFirst}}{{/methods}}
            {{#methods}}
                }
            });
            {{/methods}}

            return future
              .thenCompose(principal -> {
                if (principal == null) {
                  return CompletableFuture.failedFuture(HttpServerResponseException.of(403, "Forbidden"));
                }
                Principal.set(ctx, principal);
                try {
                  return chain.process(ctx, request);
                } catch (Exception e) {
                  return CompletableFuture.failedFuture(e);
                }
              });
        }
    }

    @ru.tinkoff.kora.common.Tag({{name}}.class)
    @DefaultComponent
    default {{name}}HttpServerInterceptor {{name}}HttpServerAuthInterceptor({{#methods}}
      @ru.tinkoff.kora.common.Tag(ApiSecurity.{{#lambda.classname}}{{name}}{{/lambda.classname}}.class) HttpServerPrincipalExtractor<{{#isOAuth}}PrincipalWithScopes{{/isOAuth}}{{^isOAuth}}Principal{{/isOAuth}}> {{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{^vendorExtensions.isLast}},{{/vendorExtensions.isLast}}{{/methods}}) {
        return new {{name}}HttpServerInterceptor({{#methods}}{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{^vendorExtensions.isLast}}, {{/vendorExtensions.isLast}}{{/methods}});
    }

{{/vendorExtensions.authMethods}}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy