Oauth2 with Qt broken on iOS (QOAuth2AuthorizationCodeFlow)

92 Views Asked by At

I need to sign in users via Oauth2 on multiple platforms. I'm using the cross platform Qt Framework, class 'QOAuth2AuthorizationCodeFlow'. I created the following function according to some online examples, and it works on macOS but gets stuck in the web browser on iOS...

void WebServices::authenticate()
{
    if(mOauthFlow){
        PRINT_WARNING("[WS] authentication already in progress\n");
        return;
    }

    QOAuthHttpServerReplyHandler *replyHandler = new QOAuthHttpServerReplyHandler(OAUTH_REDIRECT_PORT, mOauthFlow);

    DEBUG_PRINT("Oauth replyHandler CB[%s] CBP[%s] CBT[%s]\n", replyHandler->callback().toUtf8().constData(), replyHandler->callbackPath().toUtf8().constData(), replyHandler->callbackText().toUtf8().constData());

    mOauthFlow = new QOAuth2AuthorizationCodeFlow(this);
    mOauthFlow->setScope(OAUTH_SCOPE);
    mOauthFlow->setAuthorizationUrl(QUrl(OAUTH_AUTHORIZE_URL));
    mOauthFlow->setAccessTokenUrl(QUrl(OAUTH_TOKEN_URL));
    mOauthFlow->setClientIdentifier(OAUTH_CLIENT_ID);
    mOauthFlow->setClientIdentifierSharedKey(OAUTH_PUBLIC_KEY);
    mOauthFlow->setReplyHandler(replyHandler);

    connect(mOauthFlow, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, [=](QUrl url)
    {
        DEBUG_PRINT("Oauth authorize with browser: \n\t%s\n", url.toString().toUtf8().constData());
        QDesktopServices::openUrl(url);
    });
    connect(mOauthFlow, &QOAuth2AuthorizationCodeFlow::error, [=](const QString &error, const QString &errorDescription, const QUrl &uri)
    {
        PRINT_ERROR("Oauth failed: %s (%s)\n", errorDescription.toUtf8().constData(), error.toUtf8().constData());
    });
    connect(mOauthFlow, &QOAuth2AuthorizationCodeFlow::granted, [=]()
    {
        PRINT("Oauth access granted, token(%d) refresh(%d)\n", mOauthFlow->token().length(), mOauthFlow->refreshToken().length());
        emit returnFromWebBrowser();
    });
    connect(mOauthFlow, &QOAuth2AuthorizationCodeFlow::stateChanged, [=](const QString &state)
    {
        DEBUG_PRINT("Oauth state: %s\n", state.toUtf8().constData());
    });

    // start the auth flow
    mOauthFlow->grant();
}

When URL is opened in an external browser, iOS app sleeps in background and does not receive callback from web browser until user brings app to foreground. According to Apple docs, I think Qt should use ASWebAuthenticationSession internally for macOS and iOS. I've yet to find any information regarding Qt and ASWebAuthenticationSession, so I'm guessing it is not supported. AFAIK there is no Qt way to do oauth2 on iOS.

0

There are 0 best solutions below