Simple OAuth2 library with a support of multiple services.

Overview

OhMyAuth

⚠️ DEPRECATED, NO LONGER MAINTAINED

CI Status Version Carthage Compatible License Platform

Description

Simple OAuth2 library with a support of multiple services.

Usage

  • Setup:
let config = AuthConfig(
  clientId: "client-id",
  accessTokenUrl: NSURL(string: "access-token-url")!,
  accessGrantType: "authorization_code",
  authorizeURL: NSURL(string: "authorise-url")!,
  changeUserURL: NSURL(string: "change-user-url")!,
  redirectURI: "yourapp://auth")

config.extraAccessTokenParameters = ["resource": "resource"]
config.extraRefreshTokenParameters = ["resource": "resource"]

let service = AuthService(name: "service", config: config)
AuthContainer.addService(service)
  • Safari app will be opened by default for authorization, if it's iOS9 and you'd like to use SFSafariViewController, there is a ready-to-use class for you:
// SFSafariViewController will be presented on top of provided controller
service.config.webView = SafariWebView(viewController: viewController)
  • Show a login web page:
AuthContainer.serviceNamed("service")?.authorize()
  • Handle response. If you use SafariWebView it will be dismissed automagically:
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
   AuthContainer.serviceNamed("service")?.accessToken(URL: url) { accessToken, error in
      if let accessToken = accessToken where error == nil {
         // User is logged in!!!
      }
   }
}
  • Get an access token to include it in the each request. If token is about to expire it will be refreshed automagically, so you always get an active token is the completion closure:
AuthContainer.serviceNamed("service")?.accessToken(completion)
  • If you need to change user and have a separate URL for that:
AuthContainer.serviceNamed("service")?.changeUser()
  • If you don't have authorisation by code, but by username and password, there is a flow:
let config = AuthConfig(
  clientId: "client-id",
  accessTokenUrl: NSURL(string: "access-token-url")!,
  accessGrantType: "password")

let service = AuthService(name: "service", config: config)
AuthContainer.addService(service)

let parameters = ["username": "weirdo", "password": "123456"]
service.accessToken(parameters: parameters) { accessToken, error in
 // Ready!
}
  • If you need to get your tokens, expiry date, username, user UPN:
let accessToken = AuthContainer.serviceNamed("service")?.locker.accessToken
let userUPN = AuthContainer.serviceNamed("service")?.locker.userUPN

And yeah, you could add as many auth services as you want if you have some crazy setup in the app. Just register a new one with a different name:

let service = AuthService(name: "another-service", config: config)

Author

Hyper Interaktiv AS, [email protected]

Installation

OhMyAuth is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'OhMyAuth'

OhMyAuth is also available through Carthage. To install just write into your Cartfile:

github "hyperoslo/OhMyAuth"

Author

Hyper Interaktiv AS, [email protected]

Contributing

We would love you to contribute to OhMyAuth, check the CONTRIBUTING file for more info.

License

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

Comments
  • Fix

    Fix "can't extract expiration date" bug

    In the project I’m working on the original code was unable to extract the (otherwise correct) expiration date from the authentication server’s response (Azure AD). The modified code works just fine.

    opened by attila-at-hyper 7
  • Handle info in 401

    Handle info in 401

    For case of 401, the network response may include some more info that we want to notify in the completion handler, rather than just the statusCode

    So this allows consumer to specify the parse function to do that

    opened by onmyway133 6
  • Feature: basic implementation

    Feature: basic implementation

    @zenangst @RamonGilabert All right, so it was a long way of many commits, but the library is apparently working (tested it with one real project), public API is good I believe, and code has been refactored so many times that it couldn't be better. Should surely work with Azure SSO, and every other OAuth2 service, at least I truly believe in that :smile:

    Usage:

    • Setup:
    let config = AuthConfig(
      clientId: "client-id",
      accessTokenUrl: NSURL(string: "access-token-url")!,
      accessGrantType: "authorization_code",
      authorizeURL: NSURL(string: "authorise-url")!,
      changeUserURL: NSURL(string: "change-user-url")!,
      redirectURI: "yourapp://auth")
    
    config.extraAccessTokenParameters = ["resource": "resource"]
    config.extraRefreshTokenParameters = ["resource": "resource"]
    
    let service = AuthService(name: "service", config: config)
    AuthContainer.addService(service)
    
    • Safari app will be opened by default for authorisation, if it's iOS9 and you'd like to use SFSafariViewController, there is a ready-to-use class for you:
    // SFSafariViewController will be presented on top of provided controller
    service.config.webView = SafariWebView(viewController: viewController) 
    
    • Show a login web page:
    AuthContainer.serviceNamed("service")?.authorize()
    
    • Handle response. If you use SafariWebView it will be dismissed _automagically_:
    func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
       AuthContainer.serviceNamed("service")?.accessToken(URL: url) { accessToken, error in
          if let accessToken = accessToken where error == nil {
             // User is logged in!!!
          }
       }
    }
    
    • Get an access token to include it in the each request. If token is about to expire it will be refreshed _automagically_, so you always get an active token is the completion closure:
    AuthContainer.serviceNamed("service")?.accessToken(completion)
    
    • If you need to change user and have a separate URL for that:
    AuthContainer.serviceNamed("service")?.changeUser()
    
    • If you don't have authorisation by code, but by username and password, there is a flow:
    let config = AuthConfig(
      clientId: "client-id",
      accessTokenUrl: NSURL(string: "access-token-url")!,
      accessGrantType: "password")
    
    let service = AuthService(name: "service", config: config)
    AuthContainer.addService(service)
    
    let parameters = ["username": "weirdo", "password": "123456"]
    service.accessToken(parameters: parameters) { accessToken, error in
     // Ready!
    }
    
    • If you need to get your tokens, expiry date, username, user UPN:
    let accessToken = AuthContainer.serviceNamed("service")?.locker.accessToken
    let userUPN = AuthContainer.serviceNamed("service")?.locker.userUPN
    

    And yeah, you could add as many auth services as you want if you have some crazy setup in the app. Just register a new one with a different name:

    let service = AuthService(name: "another-service", config: config)
    
    opened by vadymmarkov 4
  • A few little tweaks to bring the project up to date

    A few little tweaks to bring the project up to date

    This PR includes a few small tweaks to make OhMyAuth compile successfully using the Swift 4.2 compiler. It also includes a bug fix I've made on our fork, which you might want to include in the main repo. Let me know if you want me to take any of these out 🙂

    • Remove concurrency from token queue, since it relies on being serial in order to work.
    • Use the latest recommended Xcode project settings.
    • Replace open with public for properties and methods that can't be overridden anyway.
    • Remove an iOS <8.2 code path that's no longer used and won't compile anymore.

    Note that the project still uses Swift 4.0, I haven't changed that.

    opened by JohnSundell 3
  • Add authorize() call if token refresh failed

    Add authorize() call if token refresh failed

    There is an edge case not handled by the current code. I’ve run into this issue on an earlier project (using a home-grown OAuth solution).

    If the user successfully authorizes, but then doesn’t use the app for at least two weeks, the refresh token also expires (at least with Azure AD). In this case next time app is opened and an access token refresh is attempted, it will fail. In the other project I’ve handled this edge case with an attempt to authorize again (and perform any code block waiting for the access token, if succeeded). The aim was to provide as smooth UX as possible if it went wrong.

    I’m not sure if the solution in this PR is the right one (I’m happy to share the Objective-C code from that earlier project with you), but we should get started somewhere.

    opened by attila-at-hyper 3
  • Release 1.0.0

    Release 1.0.0

    • Update Cartfile as Keychain swift-3 branch has been merged to master
    • We should release Keychain first, as this depends on Keychain. However, Keychain name has been registered on cocoapods, we need another podname. However, this PR just bump podspec version to mark this milestone
    opened by onmyway133 2
  • Fix logic properly this time.

    Fix logic properly this time.

    It seems like we had a brain-fart earlier when trying to fix this logic. If you analysis the statement it should be pretty straight forward to see that the expiryDate should be in the past and not the future. Otherwise it would cause it to always refresh the token, unless of course it was expired and then it would end up doing nothing.

    opened by zenangst 2
  • Add deauthorize()

    Add deauthorize()

    I think it’d be useful to add a way to logout/deauthorize. The completion is to let the client to do its own post-logout clean-up. Unfortunately the browser controller can’t be closed automagically in this case because we don’t get notified about the event.

    opened by attila-at-hyper 1
  • Fix network errors treated as “Internal error”

    Fix network errors treated as “Internal error”

    This patch fixes a bug that would cause all network related errors (such as when the user is offline) as opaque OhMyAuthError.internalError, which prevents the appropriate error handling to be done in clients.

    The problem was that the logic dealing with responses was first checking if a valid HTTPURLResponse was received before checking the error. This patch simply flips the order of those two operations.

    Tests have also been implemented for NetworkRequestable.

    opened by JohnSundell 0
  • NetworkTasks: Don’t treat missing refresh token as fatal error

    NetworkTasks: Don’t treat missing refresh token as fatal error

    This change makes it so that a refresh token is no longer required in the data returned from a network request. Some authentication services do not reply with a refresh token, making all requests fail. The change is to only assign the current locker’s refresh token if it exists in the data.

    opened by JohnSundell 0
  • Fix expiration check

    Fix expiration check

    • I think the window minimumValidity is used as a safe range to refresh the access token. This should be + instead of -
    • This adds some more tests just to make sure
    opened by onmyway133 0
Releases(2.0.0)
Owner
HyperRedink
Connected creativity
HyperRedink
OAuth2 framework for macOS and iOS, written in Swift.

OAuth2 OAuth2 frameworks for macOS, iOS and tvOS written in Swift 5.0. ⤵️ Installation ?? Usage ?? Sample macOS app (with data loader examples) ?? Tec

Pascal Pfiffner 1.1k Jan 2, 2023
A swift implementation of OAuth2

#AlamofireOauth2 A Swift implementation of OAuth2 for iOS using Alamofire. #Intro This library is heavilly inspired by the SwiftOAuth2 repository from

Edwin Vermeer 83 Dec 5, 2022
A simple OAuth library for iOS with a built-in set of providers

SwiftyOAuth is a small OAuth library with a built-in set of providers and a nice API to add your owns. let instagram: Provider = .instagram(clientID:

Damien 477 Oct 15, 2022
A simple library to make authenticating tvOS apps easy via their iOS counterparts.

Voucher The new Apple TV is amazing but the keyboard input leaves a lot to be desired. Instead of making your users type credentials into their TV, yo

Riz 516 Nov 24, 2022
FCLAuthSwift is a Swift library for the Flow Client Library (FCL) that enables Flow wallet authentication on iOS devices.

FCLAuthSwift is a Swift library for the Flow Client Library (FCL) that enables Flow wallet authentication on iOS devices. Demo The demo a

Zed 3 May 2, 2022
LinkedInSignIn - Simple view controller to log in and retrieve an access token from LinkedIn.

LinkedInSignIn Example To run the example project, clone the repo, and run pod install from the Example directory first. Also you need to setup app on

Serhii Londar 34 Sep 1, 2022
A simple way to implement Facebook and Google login in your iOS apps.

Simplicity Simplicity is a simple way to implement Facebook and Google login in your iOS apps. Simplicity can be easily extended to support other exte

Simplicity Mobile 681 Dec 18, 2022
A simple to use, standard interface for authenticating to oauth 2.0 protected endpoints via SFSafariViewController.

AuthenticationViewController A simple to use, standard interface for authenticating to OAuth 2.0 protected endpoints via SFSafariViewController. Instr

Raul Riera 256 Dec 14, 2022
A quick and simple way to authenticate an Instagram user in your iPhone or iPad app.

InstagramSimpleOAuth A quick and simple way to authenticate an Instagram user in your iPhone or iPad app. Adding InstagramSimpleOAuth to your project

Ryan Baumbach 90 Aug 20, 2022
A quick and simple way to authenticate a Dropbox user in your iPhone or iPad app.

DropboxSimpleOAuth A quick and simple way to authenticate a Dropbox user in your iPhone or iPad app. Adding DropboxSimpleOAuth to your project CocoaPo

Ryan Baumbach 42 Dec 29, 2021
A quick and simple way to authenticate a Box user in your iPhone or iPad app.

BoxSimpleOAuth A quick and simple way to authenticate a Box user in your iPhone or iPad app. Adding BoxSimpleOAuth to your project CocoaPods CocoaPods

Ryan Baumbach 15 Mar 10, 2021
A simple project for Face ID Authentication for iOS device using LocalAuthentication Framework

BiometricAuthentication This respository is a simple project for Face ID Authentication for iOS device using LocalAuthentication Framework and infoPli

Lee McCormick 0 Oct 23, 2021
Easy to use OAuth 2 library for iOS, written in Swift.

Heimdallr Heimdallr is an OAuth 2.0 client specifically designed for easy usage. It currently supports the resource owner password credentials grant f

trivago N.V. 628 Oct 17, 2022
Swift based OAuth library for iOS

OAuthSwift Swift based OAuth library for iOS and macOS. Support OAuth1.0, OAuth2.0 Twitter, Flickr, Github, Instagram, Foursquare, Fitbit, Withings, L

OAuthSwift 3.1k Jan 3, 2023
Swift async/await OAuth 2.0 HTTP request library.

SwAuth SwAuth is an OAuth 2.0 HTTP request library written in Swift iOS 15.0+, macOS 12.0+, watchOS 8.0+, and tvOS 15.0+. Features Requirements Instal

Colaski 17 Nov 5, 2022
Swift based OAuth library for iOS and macOS

OAuthSwift Swift based OAuth library for iOS and macOS. Support OAuth1.0, OAuth2.0 Twitter, Flickr, Github, Instagram, Foursquare, Fitbit, Withings, L

OAuthSwift 3.1k Jan 3, 2023
🔥 🔥 🔥Support for ORM operation,Customize the PQL syntax for quick queries,Support dynamic query,Secure thread protection mechanism,Support native operation,Support for XML configuration operations,Support compression, backup, porting MySQL, SQL Server operation,Support transaction operations.

?? ?? ??Support for ORM operation,Customize the PQL syntax for quick queries,Support dynamic query,Secure thread protection mechanism,Support native operation,Support for XML configuration operations,Support compression, backup, porting MySQL, SQL Server operation,Support transaction operations.

null 60 Dec 12, 2022
Example of simple OAuth2 authentication using Alamofire 5 and RxSwift library

REST Client based on Alamofire 5 and RxSwift library. Supports OAuth2 and Basic authentication interceptor.

Tomáš Smolík 3 Dec 8, 2022
OAuth2 framework for macOS and iOS, written in Swift.

OAuth2 OAuth2 frameworks for macOS, iOS and tvOS written in Swift 5.0. ⤵️ Installation ?? Usage ?? Sample macOS app (with data loader examples) ?? Tec

Pascal Pfiffner 1.1k Jan 8, 2023
OAuth2 framework for macOS and iOS, written in Swift.

OAuth2 OAuth2 frameworks for macOS, iOS and tvOS written in Swift 5.0. ⤵️ Installation ?? Usage ?? Sample macOS app (with data loader examples) ?? Tec

Pascal Pfiffner 1.1k Jan 2, 2023