Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth view notification with login reason #3732

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public enum UserAccountManagerError: Error {

extension UserAccountManager {

public func login(reason: SFLoginReason, _ completionBlock: @escaping (Result<(UserAccount, AuthInfo), UserAccountManagerError>) -> Void) -> Bool {
return __login(with: reason, completion: { (authInfo, userAccount) in
completionBlock(Result.success((userAccount,authInfo)))
}, failure: { (authInfo, error) in
completionBlock(Result.failure(.loginFailed(underlyingError: error, authInfo: authInfo)))
})
}

/// Kick off the login process for credentials that's previously configured.
/// - Parameter completionBlock: completion block to invoke with a success tuple (UserAccount, AuthInfo) or UserAccountManagerError for failure wrapped in a Result type.
public func login(_ completionBlock: @escaping (Result<(UserAccount, AuthInfo), UserAccountManagerError>) -> Void) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import <Foundation/Foundation.h>
#import <SalesforceSDKCore/SFSDKOAuth2.h>

NS_ASSUME_NONNULL_BEGIN
@class SFSDKLoginViewControllerConfig;
Expand All @@ -44,6 +45,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nullable, nonatomic, strong) UIScene *scene;
@property (nonatomic, copy) NSString *jwtToken;
@property (nonatomic, copy, nullable) NSString *userAgentForAuth;
@property (nonatomic) SFLoginReason loginReason; // TODO

//IDP flow related properties (SPApp related properties)
@property (nonatomic, readonly, assign) BOOL idpEnabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,10 @@ NS_SWIFT_NAME(UserAccountManager)
- (BOOL)loginWithCompletion:(nullable SFUserAccountManagerSuccessCallbackBlock)completionBlock
failure:(nullable SFUserAccountManagerFailureCallbackBlock)failureBlock NS_REFINED_FOR_SWIFT;

- (BOOL)loginWithReason:(SFLoginReason)reason
completion:(nullable SFUserAccountManagerSuccessCallbackBlock)completionBlock
failure:(nullable SFUserAccountManagerFailureCallbackBlock)failureBlock NS_REFINED_FOR_SWIFT;

/**
Kick off the refresh process for the specified credentials.
@param credentials SFOAuthCredentials to be refreshed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@

//keys used in notifications
NSString * const kSFNotificationUserInfoAccountKey = @"account";
NSString * const kSFNotificationUserInfoLoginReasonKey = @"loginReason";
NSString * const kSFNotificationUserInfoLogoutReasonKey = @"logoutReason";
NSString * const kSFNotificationUserInfoCredentialsKey = @"credentials";
NSString * const kSFNotificationUserInfoAuthTypeKey = @"authType";
Expand Down Expand Up @@ -396,15 +397,25 @@ - (void)kickOffIDPInitiatedLoginFlowForSP:(SFSDKSPConfig *)config statusUpdate:(
}

- (BOOL)loginWithCompletion:(SFUserAccountManagerSuccessCallbackBlock)completionBlock failure:(SFUserAccountManagerFailureCallbackBlock)failureBlock {
return [self loginWithReason:SFLoginReasonUnknown completion:completionBlock failure:failureBlock];
}

- (BOOL)loginWithReason:(SFLoginReason)reason completion:(nullable SFUserAccountManagerSuccessCallbackBlock)completionBlock failure:(nullable SFUserAccountManagerFailureCallbackBlock)failureBlock {
[SFSDKCoreLogger i:[self class] format:@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
BOOL result = NO;
for (UIScene *scene in [SFApplicationHelper sharedApplication].connectedScenes) {
result |= [self loginWithCompletion:completionBlock failure:failureBlock scene:scene];
result |= [self loginWithReason:reason scene:scene completion:completionBlock failure:failureBlock];
}
return result;
}

- (BOOL)loginWithCompletion:(SFUserAccountManagerSuccessCallbackBlock)completionBlock failure:(SFUserAccountManagerFailureCallbackBlock)failureBlock scene:(UIScene *)scene {
return [self authenticateWithCompletion:completionBlock failure:failureBlock scene:scene];
return [self loginWithReason:SFLoginReasonUnknown scene:scene completion:completionBlock failure:failureBlock];
}

- (BOOL)loginWithReason:(SFLoginReason)reason scene:(UIScene *)scene completion:(SFUserAccountManagerSuccessCallbackBlock)completionBlock failure:(SFUserAccountManagerFailureCallbackBlock)failureBlock {
[SFSDKCoreLogger i:[self class] format:@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
return [self authenticateWithReason:reason scene:scene completion:completionBlock failure:failureBlock];
}

- (BOOL)refreshCredentials:(SFOAuthCredentials *)credentials completion:(SFUserAccountManagerSuccessCallbackBlock)completionBlock failure:(SFUserAccountManagerFailureCallbackBlock)failureBlock {
Expand Down Expand Up @@ -476,7 +487,7 @@ - (void)stopCurrentAuthentication:(void (^)(BOOL))completionBlock {
}
}

- (BOOL)authenticateWithCompletion:(SFUserAccountManagerSuccessCallbackBlock)completionBlock failure:(SFUserAccountManagerFailureCallbackBlock)failureBlock scene:(UIScene *)scene {
- (BOOL)authenticateWithReason:(SFLoginReason)reason scene:(UIScene *)scene completion:(SFUserAccountManagerSuccessCallbackBlock)completionBlock failure:(SFUserAccountManagerFailureCallbackBlock)failureBlock {
SFSDKAuthSession *authSession = self.authSessions[scene.session.persistentIdentifier];
if (authSession && authSession.isAuthenticating) {
[SFSDKCoreLogger e:[self class] format:@"Login has already been called. Stop current authentication using SFUserAccountManager::stopCurrentAuthentication and then retry."];
Expand All @@ -490,6 +501,7 @@ - (BOOL)authenticateWithCompletion:(SFUserAccountManagerSuccessCallbackBlock)com
request = [self defaultAuthRequest];
}

request.loginReason = reason;
if (scene) {
request.scene = scene;
}
Expand Down Expand Up @@ -827,12 +839,19 @@ - (void)oauthCoordinatorDidFetchAuthCode:(SFOAuthCoordinator *)coordinator authI
}

- (void)oauthCoordinator:(SFOAuthCoordinator *)coordinator didBeginAuthenticationWithView:(WKWebView *)view {

SFLoginViewController *loginViewController = [self createLoginViewControllerInstance:coordinator];
loginViewController.oauthView = view;
SFSDKAuthViewHolder *viewHolder = [SFSDKAuthViewHolder new];
viewHolder.loginController = loginViewController;
viewHolder.scene = coordinator.authSession.oauthRequest.scene;

SFLoginReason reason = coordinator.authSession.oauthRequest.loginReason;
[SFSDKCoreLogger i:[self class] format:@"Showing auth view, login reason: %@", reason]; // TODO string value
NSDictionary *userInfo = @{ kSFNotificationUserInfoCredentialsKey: coordinator.credentials,
kSFNotificationUserInfoAuthTypeKey: coordinator.authInfo,
kSFNotificationUserInfoLoginReasonKey: @(coordinator.authSession.oauthRequest.loginReason)};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reason should also be applied to the notification posted in the function below - - (void)oauthCoordinator:(SFOAuthCoordinator *)coordinator didBeginAuthenticationWithSession:(ASWebAuthenticationSession *)session

[[NSNotificationCenter defaultCenter] postNotificationName:kSFNotificationUserWillShowAuthView object:self userInfo:userInfo];

// Ensure this runs on the main thread. Has to be sync, because the coordinator expects the auth view
// to be added to a superview by the end of this method.
if (![NSThread isMainThread]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ typedef NS_ENUM(NSInteger, SFLogoutReason) {
SFLogoutReasonRefreshTokenRotated // "Refresh token rotated"
};

typedef NS_ENUM(NSInteger, SFLoginReason) {
SFLoginReasonUnknown,
Copy link
Member Author

@bbirman bbirman Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are tbd, I was thinking we should also have something for the AuthHelper entry point since that's only called by apps

SFLoginReasonRestAPI,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would it be SFLoginReasonRestAPI?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed that meant we try to refresh the access token but the auth token is expired.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, I missed a file, for this one

[[SFUserAccountManager sharedInstance] loginWithCompletion:^(SFOAuthInfo *authInfo, SFUserAccount *userAccount) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better naming ideas would be great :P

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can match the logout reasons when applicable?

SFLoginReasonSwitchToNewUser,
SFLoginReasonBiometricLock,
};

NS_ASSUME_NONNULL_BEGIN
@class SFOAuthCredentials;
@interface SFSDKOAuthTokenEndpointErrorResponse : NSObject
Expand Down