Skip to content

Commit 7bd2be8

Browse files
authored
Swift: Cognito scenario example (#7403)
* Finish Cognito identity provider scenario This commit finishes the last touches on the Cognito identity provider scenario example with cleanup, added comments, much better error handling, and a README. It also moves the scenario into the `cognito-identity-provider` directory.
1 parent 6eb1997 commit 7bd2be8

File tree

5 files changed

+816
-0
lines changed

5 files changed

+816
-0
lines changed

.doc_gen/metadata/cognito-identity-provider_metadata.yaml

+83
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ cognito-identity-provider_SignUp:
163163
- cpp.example_code.cognito.client_configuration
164164
- cpp.example_code.cognito.cognito_client
165165
- cpp.example_code.cognito.signup
166+
Swift:
167+
versions:
168+
- sdk_version: 1
169+
github: swift/example_code/cognito-identity-provider
170+
excerpts:
171+
- description:
172+
snippet_tags:
173+
- swift.cognito-identity-provider.import
174+
- swift.cognito-identity-provider.SignUp
166175
services:
167176
cognito-identity-provider: {SignUp}
168177
cognito-identity-provider_AdminGetUser:
@@ -223,6 +232,15 @@ cognito-identity-provider_AdminGetUser:
223232
- cpp.example_code.cognito.client_configuration
224233
- cpp.example_code.cognito.cognito_client
225234
- cpp.example_code.cognito.admin_get_user
235+
Swift:
236+
versions:
237+
- sdk_version: 1
238+
github: swift/example_code/cognito-identity-provider
239+
excerpts:
240+
- description:
241+
snippet_tags:
242+
- swift.cognito-identity-provider.import
243+
- swift.cognito-identity-provider.AdminGetUser
226244
services:
227245
cognito-identity-provider: {AdminGetUser}
228246
cognito-identity-provider_ResendConfirmationCode:
@@ -283,6 +301,15 @@ cognito-identity-provider_ResendConfirmationCode:
283301
- cpp.example_code.cognito.client_configuration
284302
- cpp.example_code.cognito.cognito_client
285303
- cpp.example_code.cognito.resend_confirmation
304+
Swift:
305+
versions:
306+
- sdk_version: 1
307+
github: swift/example_code/cognito-identity-provider
308+
excerpts:
309+
- description:
310+
snippet_tags:
311+
- swift.cognito-identity-provider.import
312+
- swift.cognito-identity-provider.ResendConfirmationCode
286313
services:
287314
cognito-identity-provider: {ResendConfirmationCode}
288315
cognito-identity-provider_ConfirmSignUp:
@@ -343,6 +370,15 @@ cognito-identity-provider_ConfirmSignUp:
343370
- cpp.example_code.cognito.client_configuration
344371
- cpp.example_code.cognito.cognito_client
345372
- cpp.example_code.cognito.confirm_signup
373+
Swift:
374+
versions:
375+
- sdk_version: 1
376+
github: swift/example_code/cognito-identity-provider
377+
excerpts:
378+
- description:
379+
snippet_tags:
380+
- swift.cognito-identity-provider.import
381+
- swift.cognito-identity-provider.ConfirmSignUp
346382
services:
347383
cognito-identity-provider: {ConfirmSignUp}
348384
cognito-identity-provider_ListUsers:
@@ -453,6 +489,15 @@ cognito-identity-provider_AdminInitiateAuth:
453489
- cpp.example_code.cognito.client_configuration
454490
- cpp.example_code.cognito.cognito_client
455491
- cpp.example_code.cognito.admin_initiate_auth
492+
Swift:
493+
versions:
494+
- sdk_version: 1
495+
github: swift/example_code/cognito-identity-provider
496+
excerpts:
497+
- description:
498+
snippet_tags:
499+
- swift.cognito-identity-provider.import
500+
- swift.cognito-identity-provider.AdminInitiateAuth
456501
services:
457502
cognito-identity-provider: {AdminInitiateAuth}
458503
cognito-identity-provider_AssociateSoftwareToken:
@@ -513,6 +558,15 @@ cognito-identity-provider_AssociateSoftwareToken:
513558
- cpp.example_code.cognito.client_configuration
514559
- cpp.example_code.cognito.cognito_client
515560
- cpp.example_code.cognito.associate_software_token
561+
Swift:
562+
versions:
563+
- sdk_version: 1
564+
github: swift/example_code/cognito-identity-provider
565+
excerpts:
566+
- description:
567+
snippet_tags:
568+
- swift.cognito-identity-provider.import
569+
- swift.cognito-identity-provider.AssociateSoftwareToken
516570
services:
517571
cognito-identity-provider: {AssociateSoftwareToken}
518572
cognito-identity-provider_VerifySoftwareToken:
@@ -573,6 +627,15 @@ cognito-identity-provider_VerifySoftwareToken:
573627
- cpp.example_code.cognito.client_configuration
574628
- cpp.example_code.cognito.cognito_client
575629
- cpp.example_code.cognito.verify_software_token
630+
Swift:
631+
versions:
632+
- sdk_version: 1
633+
github: swift/example_code/cognito-identity-provider
634+
excerpts:
635+
- description:
636+
snippet_tags:
637+
- swift.cognito-identity-provider.import
638+
- swift.cognito-identity-provider.VerifySoftwareToken
576639
services:
577640
cognito-identity-provider: {VerifySoftwareToken}
578641
cognito-identity-provider_AdminRespondToAuthChallenge:
@@ -633,6 +696,15 @@ cognito-identity-provider_AdminRespondToAuthChallenge:
633696
- cpp.example_code.cognito.client_configuration
634697
- cpp.example_code.cognito.cognito_client
635698
- cpp.example_code.cognito.admin_respond_to_auth_challenge
699+
Swift:
700+
versions:
701+
- sdk_version: 1
702+
github: swift/example_code/cognito-identity-provider
703+
excerpts:
704+
- description:
705+
snippet_tags:
706+
- swift.cognito-identity-provider.import
707+
- swift.cognito-identity-provider.AdminRespondToAuthChallenge
636708
services:
637709
cognito-identity-provider: {AdminRespondToAuthChallenge}
638710
cognito-identity-provider_ConfirmDevice:
@@ -947,6 +1019,17 @@ cognito-identity-provider_Scenario_SignUpUserWithMfa:
9471019
snippet_tags:
9481020
- cpp.example_code.cognito.client_configuration
9491021
- cpp.example_code.getting_started_with_user_pools
1022+
Swift:
1023+
versions:
1024+
- sdk_version: 1
1025+
github: swift/example_code/cognito-identity-provider
1026+
excerpts:
1027+
- description: The <code>Package.swift</code> file.
1028+
snippet_tags:
1029+
- swift.cognito-identity-provider.scenario.package
1030+
- description: The Swift code file.
1031+
snippet_tags:
1032+
- swift.cognito-identity-provider.scenario
9501033
services:
9511034
cognito-identity-provider: {ConfirmSignUp, ConfirmDevice, AssociateSoftwareToken, AdminGetUser, ListUsers, ResendConfirmationCode,
9521035
RespondToAuthChallenge, AdminRespondToAuthChallenge, SignUp, InitiateAuth, AdminInitiateAuth, VerifySoftwareToken}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Amazon Cognito Identity Provider code examples for the SDK for Swift
2+
3+
## Overview
4+
5+
Shows how to use the AWS SDK for Swift to work with Amazon Cognito Identity Provider.
6+
7+
<!--custom.overview.start-->
8+
<!--custom.overview.end-->
9+
10+
_Amazon Cognito Identity Provider handles user authentication and authorization for your web and mobile apps._
11+
12+
## ⚠ Important
13+
14+
* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/).
15+
* Running the tests might result in charges to your AWS account.
16+
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
17+
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
18+
19+
<!--custom.important.start-->
20+
<!--custom.important.end-->
21+
22+
## Code examples
23+
24+
### Prerequisites
25+
26+
For prerequisites, see the [README](../../README.md#Prerequisites) in the `swift` folder.
27+
28+
29+
<!--custom.prerequisites.start-->
30+
<!--custom.prerequisites.end-->
31+
32+
### Single actions
33+
34+
Code excerpts that show you how to call individual service functions.
35+
36+
- [AdminGetUser](scenario/Sources/entry.swift#L98)
37+
- [AdminInitiateAuth](scenario/Sources/entry.swift#L247)
38+
- [AdminRespondToAuthChallenge](scenario/Sources/entry.swift#L377)
39+
- [AssociateSoftwareToken](scenario/Sources/entry.swift#L298)
40+
- [ConfirmSignUp](scenario/Sources/entry.swift#L215)
41+
- [ResendConfirmationCode](scenario/Sources/entry.swift#L180)
42+
- [SignUp](scenario/Sources/entry.swift#L131)
43+
- [VerifySoftwareToken](scenario/Sources/entry.swift#L334)
44+
45+
### Scenarios
46+
47+
Code examples that show you how to accomplish a specific task by calling multiple
48+
functions within the same service.
49+
50+
- [Sign up a user with a user pool that requires MFA](scenario/Package.swift)
51+
52+
53+
<!--custom.examples.start-->
54+
<!--custom.examples.end-->
55+
56+
## Run the examples
57+
58+
### Instructions
59+
60+
To build any of these examples from a terminal window, navigate into its
61+
directory, then use the following command:
62+
63+
```
64+
$ swift build
65+
```
66+
67+
To build one of these examples in Xcode, navigate to the example's directory
68+
(such as the `ListUsers` directory, to build that example). Then type `xed.`
69+
to open the example directory in Xcode. You can then use standard Xcode build
70+
and run commands.
71+
72+
<!--custom.instructions.start-->
73+
<!--custom.instructions.end-->
74+
75+
76+
77+
#### Sign up a user with a user pool that requires MFA
78+
79+
This example shows you how to do the following:
80+
81+
- Sign up and confirm a user with a username, password, and email address.
82+
- Set up multi-factor authentication by associating an MFA application with the user.
83+
- Sign in by using a password and an MFA code.
84+
85+
<!--custom.scenario_prereqs.cognito-identity-provider_Scenario_SignUpUserWithMfa.start-->
86+
<!--custom.scenario_prereqs.cognito-identity-provider_Scenario_SignUpUserWithMfa.end-->
87+
88+
89+
<!--custom.scenarios.cognito-identity-provider_Scenario_SignUpUserWithMfa.start-->
90+
<!--custom.scenarios.cognito-identity-provider_Scenario_SignUpUserWithMfa.end-->
91+
92+
### Tests
93+
94+
⚠ Running tests might result in charges to your AWS account.
95+
96+
97+
To find instructions for running these tests, see the [README](../../README.md#Tests)
98+
in the `swift` folder.
99+
100+
101+
102+
<!--custom.tests.start-->
103+
<!--custom.tests.end-->
104+
105+
## Additional resources
106+
107+
- [Amazon Cognito Identity Provider Developer Guide](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools.html)
108+
- [Amazon Cognito Identity Provider API Reference](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/Welcome.html)
109+
- [SDK for Swift Amazon Cognito Identity Provider reference](https://sdk.amazonaws.com/swift/api/awscognitoidentityprovider/latest/documentation/awscognitoidentityprovider)
110+
111+
<!--custom.resources.start-->
112+
<!--custom.resources.end-->
113+
114+
---
115+
116+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
117+
118+
SPDX-License-Identifier: Apache-2.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// swift-tools-version: 5.9
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// (swift-tools-version has two lines here because it needs to be the first
6+
// line in the file, but it should also appear in the snippet below)
7+
//
8+
// snippet-start:[swift.cognito-identity-provider.scenario.package]
9+
// swift-tools-version: 5.9
10+
//
11+
// The swift-tools-version declares the minimum version of Swift required to
12+
// build this package.
13+
14+
import PackageDescription
15+
16+
let package = Package(
17+
name: "cognito-scenario",
18+
// Let Xcode know the minimum Apple platforms supported.
19+
platforms: [
20+
.macOS(.v13),
21+
.iOS(.v15)
22+
],
23+
dependencies: [
24+
// Dependencies declare other packages that this package depends on.
25+
.package(
26+
url: "https://github.com/awslabs/aws-sdk-swift",
27+
from: "1.0.0"),
28+
.package(
29+
url: "https://github.com/apple/swift-argument-parser.git",
30+
branch: "main"
31+
)
32+
],
33+
targets: [
34+
// Targets are the basic building blocks of a package, defining a module or a test suite.
35+
// Targets can depend on other targets in this package and products
36+
// from dependencies.
37+
.executableTarget(
38+
name: "cognito-scenario",
39+
dependencies: [
40+
.product(name: "AWSCognitoIdentityProvider", package: "aws-sdk-swift"),
41+
.product(name: "ArgumentParser", package: "swift-argument-parser")
42+
],
43+
path: "Sources")
44+
45+
]
46+
)
47+
// snippet-end:[swift.cognito-identity-provider.scenario.package]

0 commit comments

Comments
 (0)