|
| 1 | +/* |
| 2 | + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.smithy.typescript.codegen.integration; |
| 17 | + |
| 18 | +import java.nio.file.Paths; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.function.BiConsumer; |
| 23 | +import java.util.function.Consumer; |
| 24 | +import software.amazon.smithy.codegen.core.Symbol; |
| 25 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 26 | +import software.amazon.smithy.model.Model; |
| 27 | +import software.amazon.smithy.model.knowledge.ServiceIndex; |
| 28 | +import software.amazon.smithy.model.knowledge.TopDownIndex; |
| 29 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 30 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 31 | +import software.amazon.smithy.model.traits.HttpApiKeyAuthTrait; |
| 32 | +import software.amazon.smithy.model.traits.OptionalAuthTrait; |
| 33 | +import software.amazon.smithy.typescript.codegen.CodegenUtils; |
| 34 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 35 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 36 | +import software.amazon.smithy.utils.IoUtils; |
| 37 | +import software.amazon.smithy.utils.ListUtils; |
| 38 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 39 | + |
| 40 | +/** |
| 41 | + * Add config and middleware to support a service with the @httpApiKeyAuth trait. |
| 42 | + */ |
| 43 | +@SmithyInternalApi |
| 44 | +public final class AddHttpApiKeyAuthPlugin implements TypeScriptIntegration { |
| 45 | + |
| 46 | + /** |
| 47 | + * Plug into code generation for the client. |
| 48 | + * |
| 49 | + * This adds the configuration items to the client config and plugs in the |
| 50 | + * middleware to operations that need it. |
| 51 | + * |
| 52 | + * The middleware will inject the client's configured API key into the |
| 53 | + * request as defined by the @httpApiKeyAuth trait. If the trait says to |
| 54 | + * put the API key into a named header, that header will be used, optionally |
| 55 | + * prefixed with a scheme. If the trait says to put the API key into a named |
| 56 | + * query parameter, that query parameter will be used. |
| 57 | + */ |
| 58 | + @Override |
| 59 | + public List<RuntimeClientPlugin> getClientPlugins() { |
| 60 | + return ListUtils.of( |
| 61 | + // Add the config if the service uses HTTP API key authorization. |
| 62 | + RuntimeClientPlugin.builder() |
| 63 | + .inputConfig(Symbol.builder() |
| 64 | + .namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/") |
| 65 | + .name("HttpApiKeyAuthInputConfig") |
| 66 | + .build()) |
| 67 | + .resolvedConfig(Symbol.builder() |
| 68 | + .namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/") |
| 69 | + .name("HttpApiKeyAuthResolvedConfig") |
| 70 | + .build()) |
| 71 | + .resolveFunction(Symbol.builder() |
| 72 | + .namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/") |
| 73 | + .name("resolveHttpApiKeyAuthConfig") |
| 74 | + .build()) |
| 75 | + .servicePredicate((m, s) -> hasEffectiveHttpApiKeyAuthTrait(m, s)) |
| 76 | + .build(), |
| 77 | + |
| 78 | + // Add the middleware to operations that use HTTP API key authorization. |
| 79 | + RuntimeClientPlugin.builder() |
| 80 | + .pluginFunction(Symbol.builder() |
| 81 | + .namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/") |
| 82 | + .name("getHttpApiKeyAuthPlugin") |
| 83 | + .build()) |
| 84 | + .additionalPluginFunctionParamsSupplier((m, s, o) -> new HashMap<String, Object>() {{ |
| 85 | + // It's safe to do expectTrait() because the operation predicate ensures that the trait |
| 86 | + // exists `in` and `name` are required attributes of the trait, `scheme` is optional. |
| 87 | + put("in", s.expectTrait(HttpApiKeyAuthTrait.class).getIn().toString()); |
| 88 | + put("name", s.expectTrait(HttpApiKeyAuthTrait.class).getName()); |
| 89 | + s.expectTrait(HttpApiKeyAuthTrait.class).getScheme().ifPresent(scheme -> |
| 90 | + put("scheme", scheme)); |
| 91 | + }}) |
| 92 | + .operationPredicate((m, s, o) -> ServiceIndex.of(m).getEffectiveAuthSchemes(s, o) |
| 93 | + .keySet() |
| 94 | + .contains(HttpApiKeyAuthTrait.ID) |
| 95 | + && !o.hasTrait(OptionalAuthTrait.class)) |
| 96 | + .build() |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void writeAdditionalFiles( |
| 102 | + TypeScriptSettings settings, |
| 103 | + Model model, |
| 104 | + SymbolProvider symbolProvider, |
| 105 | + BiConsumer<String, Consumer<TypeScriptWriter>> writerFactory |
| 106 | + ) { |
| 107 | + ServiceShape service = settings.getService(model); |
| 108 | + |
| 109 | + // If the service doesn't use HTTP API keys, we don't need to do anything and the generated |
| 110 | + // code doesn't need any additional files. |
| 111 | + if (!hasEffectiveHttpApiKeyAuthTrait(model, service)) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + String noTouchNoticePrefix = "// Please do not touch this file. It's generated from a template in:\n" |
| 116 | + + "// https://github.com/awslabs/smithy-typescript/blob/main/smithy-typescript-codegen/" |
| 117 | + + "src/main/resources/software/amazon/smithy/aws/typescript/codegen/integration/"; |
| 118 | + |
| 119 | + // Write the middleware source. |
| 120 | + writerFactory.accept( |
| 121 | + Paths.get(CodegenUtils.SOURCE_FOLDER, "middleware", "HttpApiKeyAuth", "index.ts").toString(), |
| 122 | + writer -> { |
| 123 | + String source = IoUtils.readUtf8Resource(getClass(), "http-api-key-auth.ts"); |
| 124 | + writer.write("$L$L", noTouchNoticePrefix, "http-api-key-auth.ts"); |
| 125 | + writer.write("$L", source); |
| 126 | + }); |
| 127 | + |
| 128 | + // Write the middleware tests. |
| 129 | + writerFactory.accept( |
| 130 | + Paths.get(CodegenUtils.SOURCE_FOLDER, "middleware", "HttpApiKeyAuth", "index.spec.ts").toString(), |
| 131 | + writer -> { |
| 132 | + String source = IoUtils.readUtf8Resource(getClass(), "http-api-key-auth.spec.ts"); |
| 133 | + writer.write("$L$L", noTouchNoticePrefix, "http-api-key-auth.spec.ts"); |
| 134 | + writer.write("$L", source); |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + // The service has the effective trait if it's in the "effective auth schemes" response |
| 139 | + // AND if not all of the operations have the optional auth trait. |
| 140 | + private static boolean hasEffectiveHttpApiKeyAuthTrait(Model model, ServiceShape service) { |
| 141 | + return ServiceIndex.of(model).getEffectiveAuthSchemes(service) |
| 142 | + .keySet() |
| 143 | + .contains(HttpApiKeyAuthTrait.ID) |
| 144 | + && !areAllOptionalAuthOperations(model, service); |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + // This is derived from https://github.com/aws/aws-sdk-js-v3/blob/main/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddAwsAuthPlugin.java. |
| 149 | + private static boolean areAllOptionalAuthOperations(Model model, ServiceShape service) { |
| 150 | + TopDownIndex topDownIndex = TopDownIndex.of(model); |
| 151 | + Set<OperationShape> operations = topDownIndex.getContainedOperations(service); |
| 152 | + ServiceIndex index = ServiceIndex.of(model); |
| 153 | + |
| 154 | + for (OperationShape operation : operations) { |
| 155 | + if (index.getEffectiveAuthSchemes(service, operation).isEmpty() |
| 156 | + || !operation.hasTrait(OptionalAuthTrait.class)) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + } |
| 160 | + return true; |
| 161 | + } |
| 162 | +} |
0 commit comments