From 59f6b18651f1848d882c92203177981c5f195a9b Mon Sep 17 00:00:00 2001 From: Jithin Roy <51138777+royjit@users.noreply.github.com> Date: Thu, 21 May 2020 08:22:13 -0700 Subject: [PATCH] feat(Auth): Implementation of getCurrentUser api (#455) --- .../AWSCognitoAuthPlugin.swift | 5 ++-- .../AWSCognitoAuthPlugin+ClientBehavior.swift | 4 --- .../AWSCognitoAuthPlugin+UserBehavior.swift | 4 +++ .../AuthenticationProviderAdapter.swift | 13 +++++----- .../AuthenticationProviderBehavior.swift | 2 +- .../Models/AWSAuthUser.swift | 7 +++++ .../AWSMobileClientAdapter.swift | 6 ++++- .../AWSMobileClientBehavior.swift | 4 ++- Podfile.lock | 26 +++++++++---------- Pods/Manifest.lock | 26 +++++++++---------- 10 files changed, 56 insertions(+), 41 deletions(-) diff --git a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin.swift b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin.swift index e504767f56..d52fb2b2f5 100644 --- a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin.swift +++ b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin.swift @@ -10,8 +10,9 @@ import AWSMobileClient /// Auth plugin that uses AWS Cognito UserPool and IdentityPool. /// -/// The implicitly unwrapped optionals in this class are assigned in the `configure` method in `AWSCognitoAuthPlugin+Configure` -/// extension. Make sure to call `Amplify.configure` after adding the plugin to `Amplify`. +/// The implicitly unwrapped optionals in this class are assigned in the `configure` method in +/// `AWSCognitoAuthPlugin+Configure` extension. Make sure to call `Amplify.configure` after adding the +/// plugin to `Amplify`. final public class AWSCognitoAuthPlugin: AuthCategoryPlugin { /// A queue that regulates the execution of operations. diff --git a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+ClientBehavior.swift b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+ClientBehavior.swift index e10f7854f6..1683142696 100644 --- a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+ClientBehavior.swift +++ b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+ClientBehavior.swift @@ -161,8 +161,4 @@ extension AWSCognitoAuthPlugin { queue.addOperation(operation) return operation } - - public func getCurrentUser() -> AuthUser? { - return nil - } } diff --git a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+UserBehavior.swift b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+UserBehavior.swift index 8b34574e19..3a62ead7fd 100644 --- a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+UserBehavior.swift +++ b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+UserBehavior.swift @@ -90,4 +90,8 @@ extension AWSCognitoAuthPlugin { queue.addOperation(operation) return operation } + + public func getCurrentUser() -> AuthUser? { + return authenticationProvider.getCurrentUser() + } } diff --git a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Dependency/AuthenticationProviderAdapter.swift b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Dependency/AuthenticationProviderAdapter.swift index d605f150d4..d1420fa5ee 100644 --- a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Dependency/AuthenticationProviderAdapter.swift +++ b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Dependency/AuthenticationProviderAdapter.swift @@ -16,13 +16,14 @@ class AuthenticationProviderAdapter: AuthenticationProviderBehavior { self.awsMobileClient = awsMobileClient } - func signInUsername() -> Result { + func getCurrentUser() -> AuthUser? { - if let username = awsMobileClient.username() { - return .success(username) + guard let username = awsMobileClient.getUsername() else { + return nil } - // TODO: Fix the error here - return .failure(AuthError.unknown("")) - + guard let sub = awsMobileClient.getUserSub() else { + return nil + } + return AWSAuthUser(username: username, userId: sub) } } diff --git a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Dependency/AuthenticationProviderBehavior.swift b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Dependency/AuthenticationProviderBehavior.swift index d77cc90c71..55c2b09beb 100644 --- a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Dependency/AuthenticationProviderBehavior.swift +++ b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Dependency/AuthenticationProviderBehavior.swift @@ -31,7 +31,7 @@ protocol AuthenticationProviderBehavior { func signOut(request: AuthSignOutRequest, completionHandler: @escaping (Result) -> Void) - func signInUsername() -> Result + func getCurrentUser() -> AuthUser? func resetPassword(request: AuthResetPasswordRequest, completionHandler: @escaping (Result) -> Void) diff --git a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Models/AWSAuthUser.swift b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Models/AWSAuthUser.swift index 1f6324d048..2cfc11438b 100644 --- a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Models/AWSAuthUser.swift +++ b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Models/AWSAuthUser.swift @@ -9,8 +9,15 @@ import Amplify public struct AWSAuthUser: AuthUser { + /// The username for the logged in user + /// + /// Value maps to the username of a user in AWS Cognito User Pool. This value is set by AWS Cognito and not by the + /// user and does not always map with the username used to signIn. public var username: String + /// User Id for the logged in user + /// + /// UserId value maps to the sub value of a user in AWS Cognito User Pool. This value will be unique for a user. public var userId: String } diff --git a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Service/AWSMobileClient/AWSMobileClientAdapter.swift b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Service/AWSMobileClient/AWSMobileClientAdapter.swift index a4e267cfad..c66c9bc8e2 100644 --- a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Service/AWSMobileClient/AWSMobileClientAdapter.swift +++ b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Service/AWSMobileClient/AWSMobileClientAdapter.swift @@ -107,10 +107,14 @@ class AWSMobileClientAdapter: AWSMobileClientBehavior { awsMobileClient.signOut() } - func username() -> String? { + func getUsername() -> String? { return awsMobileClient.username } + func getUserSub() -> String? { + return awsMobileClient.userSub + } + func verifyUserAttribute(attributeName: String, completionHandler: @escaping ((UserCodeDeliveryDetails?, Error?) -> Void)) { awsMobileClient.verifyUserAttribute(attributeName: attributeName, diff --git a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Service/AWSMobileClient/AWSMobileClientBehavior.swift b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Service/AWSMobileClient/AWSMobileClientBehavior.swift index 7e39aeec48..ae566ca175 100644 --- a/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Service/AWSMobileClient/AWSMobileClientBehavior.swift +++ b/AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Service/AWSMobileClient/AWSMobileClientBehavior.swift @@ -52,7 +52,9 @@ protocol AWSMobileClientBehavior { func signOutLocally() - func username() -> String? + func getUsername() -> String? + + func getUserSub() -> String? func verifyUserAttribute(attributeName: String, completionHandler: @escaping ((UserCodeDeliveryDetails?, Error?) -> Void)) diff --git a/Podfile.lock b/Podfile.lock index b43541b0bb..3b4b039671 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -1,14 +1,14 @@ PODS: - - AWSAuthCore (2.13.3): - - AWSCore (= 2.13.3) - - AWSCognitoIdentityProvider (2.13.3): + - AWSAuthCore (2.13.4): + - AWSCore (= 2.13.4) + - AWSCognitoIdentityProvider (2.13.4): - AWSCognitoIdentityProviderASF (= 1.0.1) - - AWSCore (= 2.13.3) + - AWSCore (= 2.13.4) - AWSCognitoIdentityProviderASF (1.0.1) - - AWSCore (2.13.3) - - AWSMobileClient (2.13.3): - - AWSAuthCore (= 2.13.3) - - AWSCognitoIdentityProvider (= 2.13.3) + - AWSCore (2.13.4) + - AWSMobileClient (2.13.4): + - AWSAuthCore (= 2.13.4) + - AWSCognitoIdentityProvider (= 2.13.4) - CwlCatchException (1.0.2) - CwlPreconditionTesting (1.1.1): - CwlCatchException @@ -24,7 +24,7 @@ DEPENDENCIES: - SwiftLint SPEC REPOS: - https://cdn.cocoapods.org/: + trunk: - AWSAuthCore - AWSCognitoIdentityProvider - AWSCognitoIdentityProviderASF @@ -50,11 +50,11 @@ CHECKOUT OPTIONS: :tag: 1.2.0 SPEC CHECKSUMS: - AWSAuthCore: 93b71ac38596e75790015c7299c725bb09ae1d5f - AWSCognitoIdentityProvider: 3480819d983e78eda7a43e141273756784be30cb + AWSAuthCore: 72b4bd064c28985ea5345c62759632d511de342a + AWSCognitoIdentityProvider: a2d1d2857316519c018c03ac8205cf78154db5c0 AWSCognitoIdentityProviderASF: f94f1a502e72ef3d0a1de93e10bf7a79c8698118 - AWSCore: fc361166b595e3c0183c55987743b295646c39d2 - AWSMobileClient: 9149b664d1a89279bd49d36598c7afc38a63d7d1 + AWSCore: 06beea22b6bdbfe9f5ab5015c6901e8475c746a8 + AWSMobileClient: 33ecce524fbc011d543dae8306ca29a43ecb72e5 CwlCatchException: 70a52ae44ea5d46db7bd385f801a94942420cd8c CwlPreconditionTesting: d33a4e4f285c0b885fddcae5dfedfbb34d4f3961 SwiftFormat: b72e592ea0979aeee53f6052abff291181364933 diff --git a/Pods/Manifest.lock b/Pods/Manifest.lock index b43541b0bb..3b4b039671 100644 --- a/Pods/Manifest.lock +++ b/Pods/Manifest.lock @@ -1,14 +1,14 @@ PODS: - - AWSAuthCore (2.13.3): - - AWSCore (= 2.13.3) - - AWSCognitoIdentityProvider (2.13.3): + - AWSAuthCore (2.13.4): + - AWSCore (= 2.13.4) + - AWSCognitoIdentityProvider (2.13.4): - AWSCognitoIdentityProviderASF (= 1.0.1) - - AWSCore (= 2.13.3) + - AWSCore (= 2.13.4) - AWSCognitoIdentityProviderASF (1.0.1) - - AWSCore (2.13.3) - - AWSMobileClient (2.13.3): - - AWSAuthCore (= 2.13.3) - - AWSCognitoIdentityProvider (= 2.13.3) + - AWSCore (2.13.4) + - AWSMobileClient (2.13.4): + - AWSAuthCore (= 2.13.4) + - AWSCognitoIdentityProvider (= 2.13.4) - CwlCatchException (1.0.2) - CwlPreconditionTesting (1.1.1): - CwlCatchException @@ -24,7 +24,7 @@ DEPENDENCIES: - SwiftLint SPEC REPOS: - https://cdn.cocoapods.org/: + trunk: - AWSAuthCore - AWSCognitoIdentityProvider - AWSCognitoIdentityProviderASF @@ -50,11 +50,11 @@ CHECKOUT OPTIONS: :tag: 1.2.0 SPEC CHECKSUMS: - AWSAuthCore: 93b71ac38596e75790015c7299c725bb09ae1d5f - AWSCognitoIdentityProvider: 3480819d983e78eda7a43e141273756784be30cb + AWSAuthCore: 72b4bd064c28985ea5345c62759632d511de342a + AWSCognitoIdentityProvider: a2d1d2857316519c018c03ac8205cf78154db5c0 AWSCognitoIdentityProviderASF: f94f1a502e72ef3d0a1de93e10bf7a79c8698118 - AWSCore: fc361166b595e3c0183c55987743b295646c39d2 - AWSMobileClient: 9149b664d1a89279bd49d36598c7afc38a63d7d1 + AWSCore: 06beea22b6bdbfe9f5ab5015c6901e8475c746a8 + AWSMobileClient: 33ecce524fbc011d543dae8306ca29a43ecb72e5 CwlCatchException: 70a52ae44ea5d46db7bd385f801a94942420cd8c CwlPreconditionTesting: d33a4e4f285c0b885fddcae5dfedfbb34d4f3961 SwiftFormat: b72e592ea0979aeee53f6052abff291181364933