Skip to content

Commit a096eb8

Browse files
authored
Swift: Add Sign In With Apple example (#6927)
* Swift example using Sign In With Apple for AWS This adds a new example that uses Sign In With Apple to authenticate to AWS, then display a list of the Amazon S3 buckets the user has available. The user interface is presented using SwiftUI, and the application works on macOS, iOS, and iPadOS. * Use SimpleKeychain to save local data This update switches from using the unencrypted SwiftUI `@AppStorage` to using Auth0's `SimpleKeychain` library to save the user account information locally.
1 parent d75cd1e commit a096eb8

File tree

16 files changed

+1236
-0
lines changed

16 files changed

+1236
-0
lines changed

swift/example_code/apple/Buckets/Buckets.xcodeproj/project.pbxproj

Lines changed: 462 additions & 0 deletions
Large diffs are not rendered by default.

swift/example_code/apple/Buckets/Buckets.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict/>
5+
</plist>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"colors" : [
3+
{
4+
"idiom" : "universal"
5+
}
6+
],
7+
"info" : {
8+
"author" : "xcode",
9+
"version" : 1
10+
}
11+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"platform" : "ios",
6+
"size" : "1024x1024"
7+
},
8+
{
9+
"idiom" : "mac",
10+
"scale" : "1x",
11+
"size" : "16x16"
12+
},
13+
{
14+
"idiom" : "mac",
15+
"scale" : "2x",
16+
"size" : "16x16"
17+
},
18+
{
19+
"idiom" : "mac",
20+
"scale" : "1x",
21+
"size" : "32x32"
22+
},
23+
{
24+
"idiom" : "mac",
25+
"scale" : "2x",
26+
"size" : "32x32"
27+
},
28+
{
29+
"idiom" : "mac",
30+
"scale" : "1x",
31+
"size" : "128x128"
32+
},
33+
{
34+
"idiom" : "mac",
35+
"scale" : "2x",
36+
"size" : "128x128"
37+
},
38+
{
39+
"idiom" : "mac",
40+
"scale" : "1x",
41+
"size" : "256x256"
42+
},
43+
{
44+
"idiom" : "mac",
45+
"scale" : "2x",
46+
"size" : "256x256"
47+
},
48+
{
49+
"idiom" : "mac",
50+
"scale" : "1x",
51+
"size" : "512x512"
52+
},
53+
{
54+
"idiom" : "mac",
55+
"scale" : "2x",
56+
"size" : "512x512"
57+
}
58+
],
59+
"info" : {
60+
"author" : "xcode",
61+
"version" : 1
62+
}
63+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"author" : "xcode",
4+
"version" : 1
5+
}
6+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>com.apple.developer.applesignin</key>
6+
<array>
7+
<string>Default</string>
8+
</array>
9+
<key>com.apple.security.app-sandbox</key>
10+
<true/>
11+
<key>com.apple.security.files.user-selected.read-only</key>
12+
<true/>
13+
<key>com.apple.security.network.client</key>
14+
<true/>
15+
</dict>
16+
</plist>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import SwiftUI
5+
6+
/// The main SwiftUI entry point.
7+
@main
8+
struct BucketsApp: App {
9+
var body: some Scene {
10+
WindowGroup {
11+
ContentView().environmentObject(ViewModel())
12+
}
13+
}
14+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import Foundation
5+
6+
/// Represents errors that need to be handled or reported to the user.
7+
enum BucketsAppError: Error, LocalizedError {
8+
/// Sign In With Apple request failed.
9+
case signInWithAppleFailed
10+
/// Sign In With Apple was canceled by the user.
11+
case signInWithAppleCanceled
12+
/// The `AssumeRoleWithWebIdentity` request failed.
13+
case assumeRoleFailed
14+
/// The credentials returned by Sign In With Apple is missing
15+
/// required information.
16+
case credentialsIncomplete
17+
/// The authentication request failed.
18+
case credentialsFailed
19+
/// The `ListBuckets` request did not successfully return a list
20+
/// of Amazon S3 buckets.
21+
case bucketListMissing
22+
/// The token file could not be written to storage.
23+
case tokenFileError(reason: String = "Unable to create the token.")
24+
25+
var errorDescription: String? {
26+
switch self {
27+
case .signInWithAppleFailed:
28+
return "Unable to sign into AWS"
29+
case .signInWithAppleCanceled:
30+
return "Sign in canceled"
31+
case .assumeRoleFailed:
32+
return "Unable to access AWS S3"
33+
case .credentialsIncomplete:
34+
return "Unable to authenticate"
35+
case .credentialsFailed:
36+
return "Invalid web token"
37+
case .bucketListMissing:
38+
return "Unable to access AWS S3"
39+
case .tokenFileError:
40+
return "Token error"
41+
}
42+
}
43+
44+
/// A human-readable error message string corresponding to the
45+
/// error returned.
46+
var recoverySuggestion: String? {
47+
switch self {
48+
case .signInWithAppleFailed:
49+
return "Sign In With Apple returned an error."
50+
case .signInWithAppleCanceled:
51+
return "User did not authenticate."
52+
case .assumeRoleFailed:
53+
return "The role could not be assumed using the web token returned by Sign In With Apple."
54+
case .credentialsIncomplete:
55+
return "The credentials returned by AssumeRoleWithWebIdentity are incomplete."
56+
case .credentialsFailed:
57+
return "An error occurred while attempting to retrieve credentials from AWS."
58+
case .bucketListMissing:
59+
return "Amazon S3 did not return a valid bucket list."
60+
case .tokenFileError(let reason):
61+
return "An error occurred with the local Sign In With Apple token: \(reason)"
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)