55
66package software .amazon .smithy .java .dynamicclient .plugins ;
77
8+ import java .util .HashMap ;
9+ import java .util .HashSet ;
10+ import java .util .Map ;
11+ import java .util .ServiceLoader ;
812import software .amazon .smithy .java .client .core .AutoClientPlugin ;
913import software .amazon .smithy .java .client .core .ClientConfig ;
14+ import software .amazon .smithy .java .client .core .auth .scheme .AuthSchemeFactory ;
1015import software .amazon .smithy .java .client .core .auth .scheme .AuthSchemeResolver ;
1116import software .amazon .smithy .java .dynamicclient .settings .ModelSetting ;
1217import software .amazon .smithy .java .dynamicclient .settings .ServiceIdSetting ;
18+ import software .amazon .smithy .java .logging .InternalLogger ;
1319import software .amazon .smithy .model .Model ;
1420import software .amazon .smithy .model .knowledge .ServiceIndex ;
1521import software .amazon .smithy .model .shapes .ShapeId ;
1622
1723/**
1824 * A plugin used to detect if built-in auth schemes can be applied to a client automatically.
1925 */
26+ @ SuppressWarnings ("rawtypes" )
2027public final class SimpleAuthDetectionPlugin implements AutoClientPlugin {
2128
29+ private static final InternalLogger LOGGER = InternalLogger .getLogger (SimpleAuthDetectionPlugin .class );
30+
31+ private static final Map <ShapeId , AuthSchemeFactory > AUTH_SCHEME_FACTORIES = new HashMap <>();
32+ static {
33+ for (var factory : ServiceLoader .load (AuthSchemeFactory .class ,
34+ SimpleAuthDetectionPlugin .class .getClassLoader ())) {
35+ AUTH_SCHEME_FACTORIES .put (factory .schemeId (), factory );
36+ }
37+ }
38+
2239 public static final SimpleAuthDetectionPlugin INSTANCE = new SimpleAuthDetectionPlugin ();
2340
2441 @ Override
@@ -40,14 +57,35 @@ public void configureClient(ClientConfig.Builder config) {
4057 }
4158 }
4259
60+ @ SuppressWarnings ("unchecked" )
4361 private void injectAuthSchemeResolver (ClientConfig .Builder config , Model model , ShapeId service ) {
4462 var index = ServiceIndex .of (model );
4563 var potentialAuthSchemes = index .getEffectiveAuthSchemes (service );
4664 if (potentialAuthSchemes .isEmpty ()) {
4765 config .authSchemeResolver (AuthSchemeResolver .NO_AUTH );
48- } else {
49- // TODO: Add similar behavior as done in ClientInterfaceGenerator to register AuthSchemeFactories.
50- config .authSchemeResolver (AuthSchemeResolver .DEFAULT );
66+ return ;
67+ }
68+
69+ // Make a set of the auth schemes explicitly configured since we don't want to overwrite them.
70+ var existingSchemeIds = new HashSet <>(config .supportedAuthSchemes ().size ());
71+ for (var scheme : config .supportedAuthSchemes ()) {
72+ existingSchemeIds .add (scheme .schemeId ());
5173 }
74+
75+ for (var entry : potentialAuthSchemes .entrySet ()) {
76+ var id = entry .getKey ();
77+ if (existingSchemeIds .contains (id )) {
78+ continue ;
79+ }
80+ var factory = AUTH_SCHEME_FACTORIES .get (id );
81+ if (factory != null ) {
82+ config .putSupportedAuthSchemes (factory .createAuthScheme (entry .getValue ()));
83+ } else {
84+ LOGGER .warn ("Could not find software.amazon.smithy.java.client.core.auth.scheme.AuthSchemeFactory "
85+ + "implementation for auth scheme {}" , id );
86+ }
87+ }
88+
89+ config .authSchemeResolver (AuthSchemeResolver .DEFAULT );
5290 }
5391}
0 commit comments