@@ -346,146 +346,11 @@ let authorizer = AuthTokenAuthorizer(fetchLatestAuthToken: getUserPoolAccessToke
346
346
347
347
#### AWS IAM
348
348
349
- If you are using Amplify Auth, you can use the following class to implement SigV4 signing logic:
350
-
351
- ``` swift
352
- import Foundation
353
- import Amplify
354
- import AWSPluginsCore
355
- import AwsCommonRuntimeKit
356
- import AWSSDKHTTPAuth
357
- import Smithy
358
- import SmithyHTTPAPI
359
- import SmithyHTTPAuth
360
- import SmithyHTTPAuthAPI
361
- import SmithyIdentity
362
-
363
- class AppSyncEventsSigner {
364
-
365
- public static func createAppSyncSigner (region : String ) -> ((URLRequest) async throws -> URLRequest) {
366
- return { request in
367
- try await signAppSyncRequest (request,
368
- region : region)
369
- }
370
- }
371
-
372
- private static var signer = {
373
- return AWSSigV4Signer ()
374
- }()
375
-
376
- static func signAppSyncRequest (_ urlRequest : URLRequest,
377
- region : Swift.String ,
378
- signingName : Swift.String = " appsync" ,
379
- date : Date = Date ()) async throws -> URLRequest {
380
- CommonRuntimeKit.initialize ()
381
-
382
- // Convert URLRequest to SDK's HTTPRequest
383
- guard let requestBuilder = try createAppSyncSdkHttpRequestBuilder (
384
- urlRequest : urlRequest) else {
385
- return urlRequest
386
- }
387
-
388
- // Retrieve the credentials from credentials provider
389
- let credentials: AWSCredentialIdentity
390
- let authSession = try await Amplify.Auth .fetchAuthSession ()
391
- if let awsCredentialsProvider = authSession as? AuthAWSCredentialsProvider {
392
- let awsCredentials = try awsCredentialsProvider.getAWSCredentials ().get ()
393
- credentials = try awsCredentials.toAWSSDKCredentials ()
394
- } else {
395
- let error = AuthError.unknown (" Auth session does not include AWS credentials information" )
396
- throw error
397
- }
398
-
399
- // Prepare signing
400
- let flags = SigningFlags (useDoubleURIEncode : true ,
401
- shouldNormalizeURIPath : true ,
402
- omitSessionToken : false )
403
- let signedBodyHeader: AWSSignedBodyHeader = .none
404
- let signedBodyValue: AWSSignedBodyValue = .empty
405
- let signingConfig = AWSSigningConfig (credentials : credentials,
406
- signedBodyHeader : signedBodyHeader,
407
- signedBodyValue : signedBodyValue,
408
- flags : flags,
409
- date : date,
410
- service : signingName,
411
- region : region,
412
- signatureType : .requestHeaders ,
413
- signingAlgorithm : .sigv4 )
414
-
415
- // Sign request
416
- guard let httpRequest = await signer.sigV4SignedRequest (
417
- requestBuilder : requestBuilder,
418
- signingConfig : signingConfig
419
- ) else {
420
- return urlRequest
421
- }
422
-
423
- // Update original request with new headers
424
- return setHeaders (from : httpRequest, to : urlRequest)
425
- }
426
-
427
- static func setHeaders (from sdkRequest : SmithyHTTPAPI.HTTPRequest, to urlRequest : URLRequest) -> URLRequest {
428
- var urlRequest = urlRequest
429
- for header in sdkRequest.headers.headers {
430
- urlRequest.setValue (header.value .joined (separator : " ," ), forHTTPHeaderField : header.name )
431
- }
432
- return urlRequest
433
- }
434
-
435
- static func createAppSyncSdkHttpRequestBuilder (urlRequest : URLRequest) throws -> HTTPRequestBuilder? {
436
-
437
- guard let url = urlRequest.url ,
438
- let host = url.host else {
439
- return nil
440
- }
441
-
442
- let headers = urlRequest.allHTTPHeaderFields ?? [: ]
443
- let httpMethod = (urlRequest.httpMethod ? .uppercased ())
444
- .flatMap (HTTPMethodType.init (rawValue: )) ?? .get
445
-
446
- let queryItems = URLComponents (url : url, resolvingAgainstBaseURL : false )? .queryItems ?
447
- .map { URIQueryItem (name : $0 .name , value : $0 .value )} ?? []
448
-
449
- let requestBuilder = HTTPRequestBuilder ()
450
- .withHost (host)
451
- .withPath (url.path )
452
- .withQueryItems (queryItems)
453
- .withMethod (httpMethod)
454
- .withPort (443 )
455
- .withProtocol (.https )
456
- .withHeaders (.init (headers))
457
- .withBody (.data (urlRequest.httpBody ))
458
-
459
- return requestBuilder
460
- }
461
- }
462
-
463
- extension AWSPluginsCore.AWSCredentials {
464
-
465
- func toAWSSDKCredentials () throws -> AWSCredentialIdentity {
466
- if let tempCredentials = self as? AWSTemporaryCredentials {
467
- return AWSCredentialIdentity (
468
- accessKey : tempCredentials.accessKeyId ,
469
- secret : tempCredentials.secretAccessKey ,
470
- expiration : tempCredentials.expiration ,
471
- sessionToken : tempCredentials.sessionToken
472
- )
473
- } else {
474
- return AWSCredentialIdentity (
475
- accessKey : accessKeyId,
476
- secret : secretAccessKey,
477
- expiration : nil
478
- )
479
- }
480
- }
481
- }
482
- ```
483
-
484
- Then, create an ` IAMAuthorizer ` with this helper class.
349
+ If you are using Amplify Auth, you can initialize ` IAMAuthorizer ` with a helper method from ` AWSCognitoAuthPlugin ` like below:
485
350
486
351
``` swift
487
352
let authorizer = IAMAuthorizer (
488
- signRequest : AppSyncEventsSigner .createAppSyncSigner (region : " region" )
353
+ signRequest : AWSCognitoAuthPlugin .createAppSyncSigner (region : " region" )
489
354
)
490
355
```
491
356
0 commit comments