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

Fix Dismiss AS/SF authentication sessions upon deep-link callback #281

Merged

Conversation

cysp
Copy link
Contributor

@cysp cysp commented Jun 7, 2019

Changes

Cancel any active ASWebAuthenticationSession of SFAuthenticationSession when completing via a deep-link callback.
Currently this scenario results in a crash due to messaging a deallocated authentication session object, as reported in #213

Technical implementation:

  • create a wrapper that can hold either a ASWebAuthenticationSession or a SFAuthenticationSession, exposing their common API
  • in SafariAuthenticationSession, override AuthSession's implementation of resume(_:options:), forwarding to AuthSession and, if successfully resumed, dismissing the relevant authentication session
  • whilst here, also override cancel() to dismiss the authentication session

References

Testing

  • This change adds unit test coverage
  • This change has been tested on the latest version of the platform/language or why not

I couldn't see any existing test suite for this functionality so I tried to write some to cover the completion / cancellation logic but I have not had any success. 😕

To manually test this, configure an Auth0 tenant with passwordless authentication via email and attempt to sign in using the link in the email.

I haven't executed the !swift(>3.2) branches here, but I didn't think that I should just abandon them, nor remove them in this PR. 🙃

Checklist

@cysp cysp requested a review from a team June 7, 2019 01:30
@cysp cysp changed the title Cysp/feature/authentication session dismiss Dismiss AS/SF authentication sessions upon completion Jun 7, 2019
@cocojoe cocojoe added the medium label Jun 10, 2019
@cocojoe
Copy link
Member

cocojoe commented Jun 10, 2019

Can you add detailed, test steps please. Will have someone validate this. Thanks

@cocojoe cocojoe requested review from cocojoe and removed request for a team June 14, 2019 14:31
@cocojoe
Copy link
Member

cocojoe commented Jun 17, 2019

@cysp please can you add a before / after video so I can see your testing in action. Thanks

@cocojoe cocojoe self-assigned this Jun 17, 2019
Copy link
Member

@cocojoe cocojoe left a comment

Choose a reason for hiding this comment

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

Looks okay, follow up comments in main section.

@cysp
Copy link
Contributor Author

cysp commented Jun 18, 2019

Hi @cocojoe, sorry for the radio silence. 😬
I can certainly do up some recordings showing the behavioural difference! 🙂
This is an unreleased app, so would it be okay if I emailed them to you rather than posting them here in public? Is your git committer address okay for that?

@cocojoe
Copy link
Member

cocojoe commented Jun 18, 2019

Sure that's fine. 👍

@cysp
Copy link
Contributor Author

cysp commented Jun 24, 2019

Hi @cocojoe, I've finally gotten around to taking those screen recordings and have sent them through to the email address used in your commits in this repo.

@cocojoe
Copy link
Member

cocojoe commented Jun 25, 2019

Thanks, videos look fine. Can you rebase your branch please.

cocojoe
cocojoe previously approved these changes Jun 25, 2019
@cocojoe cocojoe added this to the vNext milestone Jun 25, 2019
@cysp cysp force-pushed the cysp/feature/authentication-session-dismiss branch from fb0ad1a to 8dd65bd Compare June 25, 2019 13:30
@cysp
Copy link
Contributor Author

cysp commented Jun 25, 2019

Cool, rebased. 🙂

@cocojoe cocojoe changed the title Dismiss AS/SF authentication sessions upon completion Fix Dismiss AS/SF authentication sessions upon deep-link callback Jul 1, 2019
@@ -29,14 +29,46 @@ import AuthenticationServices
#if swift(>=3.2)
@available(iOS 11.0, *)
class SafariAuthenticationSession: AuthSession {
private enum AuthenticationSession {
Copy link
Member

@hzalaz hzalaz Jul 1, 2019

Choose a reason for hiding this comment

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

Can we use a regular class/struct instead of an enum?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What kind of design do you have in mind using a class or struct?
Also is there a rationale for avoiding enumerations? They're a first-class type in Swift and this use-case seems like a perfect fit for an enumeration.

Copy link
Member

Choose a reason for hiding this comment

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

I dont see it as a natural use of an enum or us having more options than these two, maybe something like this:

struct NativeAuthSession {

  @available(iOS 12.0, *)
  private var authServiceSession: ASWebAuthenticationSession?
  private var safariSession: SFAuthenticationSession?
  
  @available(iOS 12.0, *)
  init(_ session: ASWebAuthenticationSession) {
    this.authServiceSession = session;
  }
 
  init(_ session: SFAuthenticationSession) {
    this.safariSession = session;
  }

  func start() -> Bool {
    if #available(iOS 12.0, *) {
      this.authServiceSession.start();
    } else {
      this.safariServices.start();
    }
  }

  func cancel() {
    if #available(iOS 12.0, *) {
      this.authServiceSession?.cancel();
    } else {
      this.safariServices?.cancel();
    }
  }
}

(might have typos since I coded it on the GH UI)

But probably is going into too mich details

Copy link
Contributor Author

@cysp cysp Jul 8, 2019

Choose a reason for hiding this comment

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

The way I see it, we have the situation where we know that an object is one of two types, and that we will only have one, not both of those types of object. To me this seems like a perfect fit for an enum, since we can encode the fact that there's exactly one of those objects, and the type of that object into the type system.
The design where a struct has multiple optional properties feels like a less good fit because the type system admits the possibility that both of those properties may be nil (or not nil) at the same time, and requires unwrapping the optional in order to operate on the contained object.

Is there a style guide or a reason that you would prefer not to use an enum for this use case?

Auth0/SafariAuthenticationSession.swift Show resolved Hide resolved
Auth0/SafariAuthenticationSession.swift Show resolved Hide resolved
@cocojoe
Copy link
Member

cocojoe commented Jul 3, 2019

@cysp ☝️

@cysp cysp force-pushed the cysp/feature/authentication-session-dismiss branch from 8dd65bd to 1f3c192 Compare July 5, 2019 01:29
cocojoe
cocojoe previously approved these changes Jul 11, 2019
@cocojoe
Copy link
Member

cocojoe commented Jul 11, 2019

@cysp can you re-base please and we can get this merged, I'm guessing there are a couple of conflicts now from most recent PR #286 as there is no option to update for me.

@cysp cysp force-pushed the cysp/feature/authentication-session-dismiss branch from 1f3c192 to d1cbd9c Compare July 14, 2019 23:23
@cysp
Copy link
Contributor Author

cysp commented Jul 14, 2019

Sure, I've rebased onto master and it looks good to me.

@cocojoe cocojoe merged commit 0d36455 into auth0:master Jul 15, 2019
@cocojoe cocojoe mentioned this pull request Jul 17, 2019
sam-w pushed a commit to LoungeBuddy/Auth0.swift that referenced this pull request Mar 11, 2020
…th0#281)

* Abstract over SF and AS authentication sessions

* Pass cancellation to authentication sessions

* Cancel authentication session upon completion
sam-w added a commit to LoungeBuddy/Auth0.swift that referenced this pull request Mar 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants