ASWebAuthenticationSession does not work for some reason for an OAuth Authorization Code grant

I’m having some trouble with getting a OAuth Authorization Code redirect with a custom scheme to work with ASWebAuthenticationSession.
I am trying to build an app that integrates with an authentication provider, in which I have configured like this:

  • Callback URL: myapp://auth

In my iOS app, I have define this as a custom scheme in my info.plist file.

<dict>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLName</key>
            <string>com.abc.def</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>myapp</string>
            </array>
        </dict>
        <dict/>
    </array>
</dict>

Excuse the messy-ish code below, but I just want to see this work.

import SwiftUI

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            AContentView()
                .onOpenURL { url in
                    print("Received URL in onOpenURL: (url)")
                    Self.handleURL(url)
                }
        }
    }
    
    static func handleURL(_ url: URL) {
        print("Handled URL: (url)")
    }
}

import AuthenticationServices

struct AContentView: View {
    @Bindable var viewModel = SomeViewModel()
    @State private var authSession: ASWebAuthenticationSession?
    @State private var presentationContextProvider = PresentationContextProvider()
    
    var body: some View {
        VStack {
            Button(action: doIt) {
                Text("Authenticate")
                
            }
        }
    }
    
    func doIt() {
        Task { @MainActor in
            await viewModel.onLaunchAsync() // this asynchronously gets some stuff that is used to build `viewModel.loginUrl`
            authenticate()
        }
    }
    
    func authenticate() {
        let authURL = viewModel.loginUrl! // Replace with your auth URL
        let callbackURLScheme = "myapp"

        authSession = ASWebAuthenticationSession(url: authURL, callback: .customScheme(callbackURLScheme)) { callbackURL, error in
            if let error = error {
                print("Authentication error: (error.localizedDescription)")
                return
            }
            
            guard let callbackURL = callbackURL else {
                print("No callback URL")
                return
            }
            
            print("Callback URL: (callbackURL)")
            MyApp.handleURL(callbackURL)
        }
        
        authSession?.presentationContextProvider = presentationContextProvider
        authSession?.start()
    }
}

class PresentationContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding {
    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
        return UIApplication.shared.windows.first!
    }
}

I’m running Proxyman, and can see the calls the iOS app makes.

When I click the “authenticate” button, I get the expected request to open Safari, and login to a web form provided by an authentication provider. Next, I am redirected to a “choose consents” page, where I can choose scopes. Finally, on this page, I click “Allow” at the bottom of this list of scopes, but instead of being ‘sent’ back to the app, the redirect doesn’t work.

The final API call the web screen makes is to a /consent endpoint which replies with an HTTP 302, and a Location header as below:

Location: myapp://auth#code=<something>.

This doesn’t close the window, either in a simulator or a real device.

I can verify that my scheme is working correctly, as if I manually in Safari browse to myapp://auth#code=1234 it asks me if I want to open in my app, and I can see my print firing off.

Am I missing something? What am I doing wrong here?

While I could implement this myself using WKWebView / WKNavigationDelegate to intercept the new location, see if its my custom scheme, and then close it out, that seems hacky, and AFAIK ASWebAuthenticationSession should support my use-case.

Many thanks!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật