Skip to content

InQBarna/WebAuthentication

Repository files navigation

WebAuthentication

Version License Platform

A simple library for web-based authentication (OAuth, SSO) on iOS. It presents a browser session, waits for the callback and extracts the token automatically.

Requirements

  • iOS 15+
  • Swift 5.5+

Installation

Swift Package Manager

.package(url: "https://github.com/InQBarna/WebAuthentication.git", from: "0.2.0")

CocoaPods

pod 'WebAuthentication'

Usage

1. Create a configuration

let config = AuthConfiguration(
    authCallbackURLScheme: "myapp",
    authCallbackTokenQueryParamName: "token",
    authStatusChangedNotificationName: Notifications.authStatusChanged,
    authStatusChangedNotificationInfo: "tokenKey"
)

2. Create an instance

let auth = WebAuthentication(configuration: config)

3. Display the authentication flow

UIKit

auth.display(loginURL, from: viewController) { result in
    switch result {
    case .success(.token(let token)):
        // use token
    case .success(.otherCallback(let url)):
        // callback received without token
    case .failure(let error):
        // handle error
    }
}

SwiftUI — callback

auth.display(loginURL) { result in
    switch result {
    case .success(.token(let token)): // use token
    case .failure(let error): // handle error
    }
}

SwiftUI — async/await

let result = await auth.display(loginURL)
if case .success(.token(let token)) = result {
    // use token
}

SwiftUI — View Modifier

struct ContentView: View {
    @State private var authURL: URL? = nil

    var body: some View {
        Button("Login") {
            authURL = URL(string: "https://your-service.com/auth")!
        }
        .webAuthentication(auth, url: $authURL) { result in
            switch result {
            case .success(.token(let token)): // use token
            case .failure(let error): // handle error
            }
        }
    }
}

Ephemeral session

Pass ephemeralWebSession: true to run the login in private mode — no cookies or credentials will be shared with the browser.

let config = AuthConfiguration(
    authCallbackURLScheme: "myapp",
    authCallbackTokenQueryParamName: "token",
    authStatusChangedNotificationName: Notifications.authStatusChanged,
    authStatusChangedNotificationInfo: "tokenKey",
    ephemeralWebSession: true
)

Author

InQBarna, @catchakos, @alexmaxu

License

WebAuthentication is available under the MIT license. See the LICENSE file for more info.

About

Facilitates authentication by providing a configured web environment depending on the iOS version

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors