# Delegated authentication for Discourse Mobile app

**URL:** <https://meta.discourse.org/t/delegated-authentication-for-discourse-mobile-app/110414>\
**Category:** Development\
**Created:** [March 1, 2019, 5:12am UTC](https://meta.discourse.org/t/delegated-authentication-for-discourse-mobile-app/110414 "2019-03-01T05:12:44Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![pmusaraj](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pmusaraj/32/119489_2.png) [@pmusaraj](https://meta.discourse.org/u/pmusaraj)\
**Post date:** [March 25, 2019, 1:58am UTC](https://meta.discourse.org/t/delegated-authentication-for-discourse-mobile-app/110414/6 "2019-03-25T01:58:16Z")

</div>

I have started working on this and have a few things to verify regarding the advanced flow:

- I don’t think we need to send the public key on every request in SafariViewController. The app currently sends [discourse\_app=1](https://github.com/discourse/DiscourseMobile/blob/d50f89591065d5757f26f2ea35e83dc32d2682e4/js/screens/HomeScreen.js#L127) which flags the session as coming from a mobile app. If user is logged in, `discourse_app=1` can be stripped by Ember and the user can continue using the site normally.
- If user is not logged in, Discourse in the SVC will redirect back to the app via the custom `discourse://` URL scheme. Here too, I don’t think we need URL parameters, the request to the app can simply be `discourse://request-one-time-password`. The app, upon receiving this request, will know which site was used last and it will thus know which site url and public key to use in the subsequent OTP Safari request.
- The next step (i.e. step 5 in advanced flow above) would be a simplified version of the initial Safari authentication request in the basic flow, but this time only passing the `one-time-password` scope, without `notifications,session_info`, those are already working for the site in the ReactNative context (and they’re not needed in SVC).
- If user is already authenticated in Safari, then the simple confirmation page “Would you like to allow DiscourseHub access to the site” is shown. If not, the user has to login first. I’m not sure whether the original spec assumed this last bit or not @sam, but it sounds like the safest and most transparent flow for users (and if this advanced flow is only hit when SVC cookies go missing, users should almost always be already authenticated in Safari).
- In both basic and advanced flows, I’m assuming we will use Apple’s [ASWebAuthenticationSession](https://developer.apple.com/documentation/authenticationservices/aswebauthenticationsession) in the Safari auth steps. This does introduce an iOS system alert (“Discourse app wants to use x site to Sign In”) in the flow, but it is the standard way to do this in iOS 12.
- And finally, to address @david’s point, I agree with Sam, we should not use email tokens here. As Sam notes, these tokens expire very quickly, I would assume 5-10 mins max. And they will be a non-optional part of the user api key system, unlike email tokens which are disableable via the “enable local logins via email” site setting.

---

_[View the full topic](https://meta.discourse.org/t/delegated-authentication-for-discourse-mobile-app/110414)._
