@@ -256,9 +256,11 @@ private void generateOperationExecutor(PythonWriter writer) {
256
256
writer .consumer (w -> context .protocolGenerator ().wrapInputStream (context , w )),
257
257
writer .consumer (w -> context .protocolGenerator ().wrapOutputStream (context , w )));
258
258
}
259
+
259
260
writer .addStdlibImport ("typing" , "Any" );
260
261
writer .addStdlibImport ("asyncio" , "iscoroutine" );
261
262
writer .addImports ("smithy_core.exceptions" , Set .of ("SmithyError" , "CallError" , "RetryError" ));
263
+ writer .addImport ("smithy_core.auth" , "AuthParams" );
262
264
writer .pushState ();
263
265
writer .putContext ("request" , transportRequest );
264
266
writer .putContext ("response" , transportResponse );
@@ -438,53 +440,60 @@ await sleep(retry_token.retry_delay)
438
440
439
441
boolean supportsAuth = !ServiceIndex .of (model ).getAuthSchemes (service ).isEmpty ();
440
442
writer .pushState (new ResolveIdentitySection ());
441
- if (context .applicationProtocol ().isHttpProtocol () && supportsAuth ) {
442
- writer .pushState (new InitializeHttpAuthParametersSection ());
443
- writer .write ("""
444
- # Step 7b: Invoke service_auth_scheme_resolver.resolve_auth_scheme
445
- auth_parameters: $1T = $1T(
446
- operation=operation.schema.id.name,
447
- ${2C|}
448
- )
449
-
450
- """ ,
451
- CodegenUtils .getHttpAuthParamsSymbol (context .settings ()),
452
- writer .consumer (this ::initializeHttpAuthParameters ));
453
- writer .popState ();
443
+ if (supportsAuth ) {
444
+ // TODO: delete InitializeHttpAuthParametersSection
454
445
455
446
writer .addDependency (SmithyPythonDependency .SMITHY_CORE );
456
- writer .addDependency (SmithyPythonDependency .SMITHY_HTTP );
457
447
writer .addImport ("smithy_core.interfaces.identity" , "Identity" );
458
- writer .addImports ("smithy_http.aio.interfaces.auth" , Set .of ("HTTPSigner" , "HTTPAuthOption" ));
448
+ writer .addImport ("smithy_core.interfaces.auth" , "AuthOption" );
449
+ writer .addImport ("smithy_core.aio.interfaces.auth" , "Signer" );
450
+ writer .addImport ("smithy_core.shapes" , "ShapeID" );
459
451
writer .addStdlibImport ("typing" , "Any" );
460
452
writer .write ("""
461
- auth_options = config.http_auth_scheme_resolver.resolve_auth_scheme(
453
+ auth_parameters = AuthParams(
454
+ protocol_id=ShapeID($1S),
455
+ operation=operation,
456
+ context=context.properties,
457
+ )
458
+ auth_options = config.auth_scheme_resolver.resolve_auth_scheme(
462
459
auth_parameters=auth_parameters
463
460
)
464
- auth_option: HTTPAuthOption | None = None
461
+
462
+ auth_option: AuthOption | None = None
465
463
for option in auth_options:
466
- if option.scheme_id in config.http_auth_schemes :
464
+ if option.scheme_id in config.auth_schemes :
467
465
auth_option = option
468
466
break
469
467
470
- signer: HTTPSigner[ Any, Any] | None = None
468
+ signer: Signer[$2T, Any, Any] | None = None
471
469
identity: Identity | None = None
470
+ auth_scheme: Any = None
472
471
473
472
if auth_option:
474
- auth_scheme = config.http_auth_schemes[auth_option.scheme_id]
473
+ auth_scheme = config.auth_schemes[auth_option.scheme_id]
474
+ context.properties["auth_scheme"] = auth_scheme
475
475
476
476
# Step 7c: Invoke auth_scheme.identity_resolver
477
- identity_resolver = auth_scheme.identity_resolver(config=config)
477
+ identity_resolver = auth_scheme.identity_resolver(context=context.properties)
478
+ context.properties["identity_resolver"] = identity_resolver
478
479
479
480
# Step 7d: Invoke auth_scheme.signer
480
- signer = auth_scheme.signer
481
+ signer = auth_scheme.signer()
482
+
483
+ # TODO: merge from auth_option
484
+ identity_properties = auth_scheme.identity_properties(
485
+ context=context.properties
486
+ )
487
+ context.properties["identity_properties"] = identity_properties
481
488
482
489
# Step 7e: Invoke identity_resolver.get_identity
483
490
identity = await identity_resolver.get_identity(
484
- identity_properties=auth_option. identity_properties
491
+ identity_properties=identity_properties
485
492
)
486
493
487
- """ );
494
+ """ ,
495
+ context .protocolGenerator ().getProtocol (),
496
+ transportRequest );
488
497
}
489
498
writer .popState ();
490
499
@@ -543,48 +552,29 @@ await sleep(retry_token.retry_delay)
543
552
544
553
writer .pushState (new SignRequestSection ());
545
554
writer .addStdlibImport ("typing" , "cast" );
546
- if (context . applicationProtocol (). isHttpProtocol () && supportsAuth ) {
555
+ if (supportsAuth ) {
547
556
writer .addStdlibImport ("re" );
548
557
writer .addStdlibImport ("typing" , "Any" );
549
558
writer .addImport ("smithy_core.interfaces.identity" , "Identity" );
550
559
writer .addImport ("smithy_core.types" , "PropertyKey" );
551
560
writer .write ("""
552
561
# Step 7i: sign the request
553
562
if auth_option and signer:
554
- logger.debug("HTTP request to sign: %s", context.transport_request)
555
- logger.debug(
556
- "Signer properties: %s",
557
- auth_option.signer_properties
558
- )
563
+ signer_properties = auth_scheme.signer_properties(context=context.properties)
564
+ context.properties["signer_properties"] = signer_properties
565
+
566
+ logger.debug("Request to sign: %s", context.transport_request)
567
+ logger.debug("Signer properties: %s", signer_properties)
568
+
559
569
context = replace(
560
570
context,
561
- transport_request= await signer.sign(
562
- http_request =context.transport_request,
571
+ transport_request = await signer.sign(
572
+ request =context.transport_request,
563
573
identity=identity,
564
- signing_properties=auth_option. signer_properties,
574
+ properties= signer_properties,
565
575
)
566
576
)
567
577
logger.debug("Signed HTTP request: %s", context.transport_request)
568
-
569
- # TODO - Move this to separate resolution/population function
570
- fields = context.transport_request.fields
571
- auth_value = fields["Authorization"].as_string() # type: ignore
572
- signature = re.split("Signature=", auth_value)[-1] # type: ignore
573
- context.properties["signature"] = signature.encode('utf-8')
574
-
575
- identity_key = cast(
576
- PropertyKey[Identity | None],
577
- PropertyKey(
578
- key="identity",
579
- value_type=Identity | None # type: ignore
580
- )
581
- )
582
- sp_key: PropertyKey[dict[str, Any]] = PropertyKey(
583
- key="signer_properties",
584
- value_type=dict[str, Any] # type: ignore
585
- )
586
- context.properties[identity_key] = identity
587
- context.properties[sp_key] = auth_option.signer_properties
588
578
""" );
589
579
}
590
580
writer .popState ();
@@ -757,28 +747,6 @@ private boolean hasEventStream() {
757
747
return false ;
758
748
}
759
749
760
- private void initializeHttpAuthParameters (PythonWriter writer ) {
761
- var derived = new LinkedHashSet <DerivedProperty >();
762
- for (PythonIntegration integration : context .integrations ()) {
763
- for (RuntimeClientPlugin plugin : integration .getClientPlugins (context )) {
764
- if (plugin .matchesService (model , service )
765
- && plugin .getAuthScheme ().isPresent ()
766
- && plugin .getAuthScheme ().get ().getApplicationProtocol ().isHttpProtocol ()) {
767
- derived .addAll (plugin .getAuthScheme ().get ().getAuthProperties ());
768
- }
769
- }
770
- }
771
-
772
- for (DerivedProperty property : derived ) {
773
- var source = property .source ().scopeLocation ();
774
- if (property .initializationFunction ().isPresent ()) {
775
- writer .write ("$L=$T($L)," , property .name (), property .initializationFunction ().get (), source );
776
- } else if (property .sourcePropertyName ().isPresent ()) {
777
- writer .write ("$L=$L.$L," , property .name (), source , property .sourcePropertyName ().get ());
778
- }
779
- }
780
- }
781
-
782
750
private void writeDefaultPlugins (PythonWriter writer , Collection <SymbolReference > plugins ) {
783
751
for (SymbolReference plugin : plugins ) {
784
752
writer .write ("$T," , plugin );
0 commit comments