|
| 1 | +/* |
| 2 | + * Copyright 2023 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.go.codegen.integration.auth; |
| 17 | + |
| 18 | +import static software.amazon.smithy.go.codegen.GoWriter.emptyGoTemplate; |
| 19 | +import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; |
| 20 | + |
| 21 | +import java.util.Map; |
| 22 | +import software.amazon.smithy.aws.traits.auth.SigV4ATrait; |
| 23 | +import software.amazon.smithy.aws.traits.auth.SigV4Trait; |
| 24 | +import software.amazon.smithy.aws.traits.auth.UnsignedPayloadTrait; |
| 25 | +import software.amazon.smithy.go.codegen.GoWriter; |
| 26 | +import software.amazon.smithy.go.codegen.SmithyGoTypes; |
| 27 | +import software.amazon.smithy.go.codegen.integration.AuthSchemeDefinition; |
| 28 | +import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; |
| 29 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 30 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 31 | +import software.amazon.smithy.utils.MapUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * Implements codegen for aws.auth#sigv4a. |
| 35 | + */ |
| 36 | +public class SigV4ADefinition implements AuthSchemeDefinition { |
| 37 | + private static final Map<String, Object> COMMON_ENV = MapUtils.of( |
| 38 | + "properties", SmithyGoTypes.Smithy.Properties, |
| 39 | + "option", SmithyGoTypes.Auth.Option, |
| 40 | + "schemeId", SmithyGoTypes.Auth.SchemeIDSigV4A, |
| 41 | + "setSigningName", SmithyGoTypes.Transport.Http.SetSigV4ASigningName, |
| 42 | + "setSigningRegions", SmithyGoTypes.Transport.Http.SetSigV4ASigningRegions |
| 43 | + ); |
| 44 | + |
| 45 | + @Override |
| 46 | + public GoWriter.Writable generateServiceOption( |
| 47 | + ProtocolGenerator.GenerationContext context, ServiceShape service |
| 48 | + ) { |
| 49 | + var trait = service.expectTrait(SigV4ATrait.class); |
| 50 | + return goTemplate(""" |
| 51 | + &$option:T{ |
| 52 | + SchemeID: $schemeId:T, |
| 53 | + SignerProperties: func() $properties:T { |
| 54 | + var props $properties:T |
| 55 | + $setSigningName:T(&props, $name:S) |
| 56 | + $setSigningRegions:T(&props, []string{params.Region}) |
| 57 | + return props |
| 58 | + }(), |
| 59 | + },""", |
| 60 | + COMMON_ENV, |
| 61 | + MapUtils.of( |
| 62 | + "name", trait.getName() |
| 63 | + )); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public GoWriter.Writable generateOperationOption( |
| 68 | + ProtocolGenerator.GenerationContext context, OperationShape operation |
| 69 | + ) { |
| 70 | + var trait = context.getService().expectTrait(SigV4Trait.class); |
| 71 | + return goTemplate(""" |
| 72 | + &$option:T{ |
| 73 | + SchemeID: $schemeId:T, |
| 74 | + SignerProperties: func() $properties:T { |
| 75 | + var props $properties:T |
| 76 | + $setSigningName:T(&props, $name:S) |
| 77 | + $setSigningRegions:T(&props, []string{params.Region}) |
| 78 | + $unsignedPayload:W |
| 79 | + return props |
| 80 | + }(), |
| 81 | + },""", |
| 82 | + COMMON_ENV, |
| 83 | + MapUtils.of( |
| 84 | + "name", trait.getName(), |
| 85 | + "unsignedPayload", generateIsUnsignedPayload(operation) |
| 86 | + )); |
| 87 | + } |
| 88 | + |
| 89 | + private GoWriter.Writable generateIsUnsignedPayload(OperationShape operation) { |
| 90 | + return operation.hasTrait(UnsignedPayloadTrait.class) |
| 91 | + ? goTemplate("$T(&props, true)", SmithyGoTypes.Transport.Http.SetIsUnsignedPayload) |
| 92 | + : emptyGoTemplate(); |
| 93 | + } |
| 94 | +} |
0 commit comments