15
15
16
16
package software .amazon .smithy .aws .typescript .codegen ;
17
17
18
+ import static software .amazon .smithy .aws .typescript .codegen .AwsTraitsUtils .isAwsService ;
18
19
import static software .amazon .smithy .aws .typescript .codegen .AwsTraitsUtils .isSigV4Service ;
19
20
import static software .amazon .smithy .typescript .codegen .integration .RuntimeClientPlugin .Convention .HAS_CONFIG ;
20
21
import static software .amazon .smithy .typescript .codegen .integration .RuntimeClientPlugin .Convention .HAS_MIDDLEWARE ;
26
27
import java .util .function .BiConsumer ;
27
28
import java .util .function .Consumer ;
28
29
import software .amazon .smithy .aws .traits .ServiceTrait ;
30
+ import software .amazon .smithy .aws .traits .auth .SigV4Trait ;
29
31
import software .amazon .smithy .codegen .core .SymbolProvider ;
30
32
import software .amazon .smithy .model .Model ;
31
33
import software .amazon .smithy .model .knowledge .TopDownIndex ;
48
50
/**
49
51
* Configure clients with AWS auth configurations and plugin.
50
52
*/
51
- // TODO: Think about AWS Auth supported for only some operations and not all, when not AWS service, with say @auth([])
52
53
@ SmithyInternalApi
53
54
public final class AddAwsAuthPlugin implements TypeScriptIntegration {
54
55
static final String STS_CLIENT_PREFIX = "sts-client-" ;
@@ -67,21 +68,37 @@ public void addConfigInterfaceFields(
67
68
if (!isSigV4Service (service )) {
68
69
return ;
69
70
}
71
+
72
+ if (!isAwsService (service )) {
73
+ writer .writeDocs ("The service name to use as the signing service for AWS Auth\n @internal" )
74
+ .write ("signingName?: string;\n " );
75
+ }
76
+
70
77
if (!areAllOptionalAuthOperations (model , service )) {
71
78
writer .addImport ("Credentials" , "__Credentials" , TypeScriptDependency .AWS_SDK_TYPES .packageName );
72
79
writer .writeDocs ("Default credentials provider; Not available in browser runtime." )
73
80
.write ("credentialDefaultProvider?: (input: any) => __Provider<__Credentials>;\n " );
74
81
}
75
82
}
76
83
84
+ // Only one of AwsAuth or SigV4Auth should be used
85
+ // AwsAuth - for AWS services
86
+ // SigV4Auth - for non AWS services
77
87
@ Override
78
88
public List <RuntimeClientPlugin > getClientPlugins () {
79
89
return ListUtils .of (
80
90
RuntimeClientPlugin .builder ()
81
91
.withConventions (AwsDependency .MIDDLEWARE_SIGNING .dependency , "AwsAuth" , HAS_CONFIG )
82
92
.servicePredicate ((m , s ) -> isSigV4Service (s )
83
- && !areAllOptionalAuthOperations (m , s )
84
- && !testServiceId (s , "STS" ))
93
+ && isAwsService (s )
94
+ && !testServiceId (s , "STS" )
95
+ && !areAllOptionalAuthOperations (m , s ))
96
+ .build (),
97
+ RuntimeClientPlugin .builder ()
98
+ .withConventions (AwsDependency .MIDDLEWARE_SIGNING .dependency , "SigV4Auth" , HAS_CONFIG )
99
+ .servicePredicate ((m , s ) -> isSigV4Service (s )
100
+ && !isAwsService (s )
101
+ && !areAllOptionalAuthOperations (m , s ))
85
102
.build (),
86
103
RuntimeClientPlugin .builder ()
87
104
.withConventions (AwsDependency .STS_MIDDLEWARE .dependency ,
@@ -92,13 +109,31 @@ public List<RuntimeClientPlugin> getClientPlugins() {
92
109
RuntimeClientPlugin .builder ()
93
110
.withConventions (AwsDependency .MIDDLEWARE_SIGNING .dependency , "AwsAuth" , HAS_MIDDLEWARE )
94
111
// See operationUsesAwsAuth() below for AwsAuth Middleware customizations.
95
- .servicePredicate (
96
- (m , s ) -> !testServiceId (s , "STS" ) && isSigV4Service (s ) && !hasOptionalAuthOperation (m , s )
112
+ .servicePredicate ((m , s ) -> isSigV4Service (s )
113
+ && isAwsService (s )
114
+ && !testServiceId (s , "STS" )
115
+ && !hasOptionalAuthOperation (m , s )
116
+ ).build (),
117
+ RuntimeClientPlugin .builder ()
118
+ .withConventions (AwsDependency .MIDDLEWARE_SIGNING .dependency , "SigV4Auth" , HAS_MIDDLEWARE )
119
+ // See operationUsesAwsAuth() below for AwsAuth Middleware customizations.
120
+ .servicePredicate ((m , s ) -> isSigV4Service (s )
121
+ && !isAwsService (s )
122
+ && !hasOptionalAuthOperation (m , s )
97
123
).build (),
98
124
RuntimeClientPlugin .builder ()
99
125
.withConventions (AwsDependency .MIDDLEWARE_SIGNING .dependency , "AwsAuth" , HAS_MIDDLEWARE )
100
- .operationPredicate (AddAwsAuthPlugin ::operationUsesAwsAuth )
126
+ .operationPredicate ((m , s , o ) -> isSigV4Service (s )
127
+ && isAwsService (s )
128
+ && operationUsesAwsAuth (m , s , o ))
129
+ .build (),
130
+ RuntimeClientPlugin .builder ()
131
+ .withConventions (AwsDependency .MIDDLEWARE_SIGNING .dependency , "SigV4Auth" , HAS_MIDDLEWARE )
132
+ .operationPredicate ((m , s , o ) -> isSigV4Service (s )
133
+ && !isAwsService (s )
134
+ && operationUsesAwsAuth (m , s , o ))
101
135
.build ()
136
+
102
137
);
103
138
}
104
139
@@ -114,6 +149,16 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
114
149
return Collections .emptyMap ();
115
150
}
116
151
switch (target ) {
152
+ case SHARED :
153
+ if (isAwsService (service )) {
154
+ return Collections .emptyMap ();
155
+ }
156
+ String signingService = service .getTrait (SigV4Trait .class ).get ().getName ();
157
+ return MapUtils .of (
158
+ "signingName" , writer -> {
159
+ writer .write ("signingName: $S," , signingService );
160
+ }
161
+ );
117
162
case BROWSER :
118
163
return MapUtils .of (
119
164
"credentialDefaultProvider" , writer -> {
@@ -208,7 +253,7 @@ private static boolean operationUsesAwsAuth(Model model, ServiceShape service, O
208
253
}
209
254
210
255
// optionalAuth trait doesn't require authentication.
211
- if (isSigV4Service ( service ) && hasOptionalAuthOperation (model , service )) {
256
+ if (hasOptionalAuthOperation (model , service )) {
212
257
return !operation .hasTrait (OptionalAuthTrait .class );
213
258
}
214
259
return false ;
0 commit comments