All Projects → trivago → Heimdallr.swift

trivago / Heimdallr.swift

Licence: apache-2.0
Easy to use OAuth 2 library for iOS, written in Swift.

Programming Languages

swift
15916 projects

Labels

Projects that are alternatives of or similar to Heimdallr.swift

Auth
:atom: Social (OAuth1\OAuth2\OpenID\OpenIDConnect) sign with PHP
Stars: ✭ 457 (-24.46%)
Mutual labels:  oauth2
Sns auth
通用第三方登录SDK,支持微信,微信扫码,QQ,微博登录,支付宝登录,Facebook,Line,Twitter,Google
Stars: ✭ 520 (-14.05%)
Mutual labels:  oauth2
Serverless Authentication Boilerplate
Generic authentication boilerplate for Serverless framework
Stars: ✭ 563 (-6.94%)
Mutual labels:  oauth2
Rest Api With Lumen
Rest API boilerplate for Lumen micro-framework.
Stars: ✭ 464 (-23.31%)
Mutual labels:  oauth2
Fastapi React
🚀 Cookiecutter Template for FastAPI + React Projects. Using PostgreSQL, SQLAlchemy, and Docker
Stars: ✭ 501 (-17.19%)
Mutual labels:  oauth2
Aspnet.security.openidconnect.server
OpenID Connect/OAuth2 server framework for OWIN/Katana and ASP.NET Core
Stars: ✭ 544 (-10.08%)
Mutual labels:  oauth2
Product Is
Welcome to the WSO2 Identity Server source code! For info on working with the WSO2 Identity Server repository and contributing code, click the link below.
Stars: ✭ 435 (-28.1%)
Mutual labels:  oauth2
Scribejava
Simple OAuth library for Java
Stars: ✭ 5,223 (+763.31%)
Mutual labels:  oauth2
Auth0 Spa Js
Auth0 authentication for Single Page Applications (SPA) with PKCE
Stars: ✭ 507 (-16.2%)
Mutual labels:  oauth2
Identityserver4.samples
Samples for IdentityServer4,use .net core 2.0
Stars: ✭ 561 (-7.27%)
Mutual labels:  oauth2
Cloudfront Auth
An AWS CloudFront [email protected] function to authenticate requests using Google Apps, Microsoft, Auth0, OKTA, and GitHub login
Stars: ✭ 471 (-22.15%)
Mutual labels:  oauth2
Example Oauth2 Server
Example for OAuth 2 Server for Authlib.
Stars: ✭ 499 (-17.52%)
Mutual labels:  oauth2
Taroco
整合Nacos、Spring Cloud Alibaba,提供了一系列starter组件, 同时提供服务治理、服务监控、OAuth2权限认证,支持服务降级/熔断、服务权重,前端采用vue+elementUI+webpack,可以很好的解决转向Spring Cloud的一系列问题。
Stars: ✭ 545 (-9.92%)
Mutual labels:  oauth2
Api Boot
“ ApiBoot”是为接口服务而生的,基于“ SpringBoot”完成扩展和自动配置,内部封装了一系列的开箱即用Starters。
Stars: ✭ 460 (-23.97%)
Mutual labels:  oauth2
Angular Auth Oidc Client
npm package for OpenID Connect, OAuth Code Flow with PKCE, Refresh tokens, Implicit Flow
Stars: ✭ 577 (-4.63%)
Mutual labels:  oauth2
Spring Boot Study
SpringBoot框架源码实战(已更新到springboot2版本实现)~基本用法,Rest,Controller,事件监听,连接数据库MySQL,jpa,redis集成,mybatis集成(声明式与xml两种方式~对应的添删查改功能),日志处理,devtools配置,拦截器用法,资源配置读取,测试集成,Web层实现请求映射,security安全验证,rabbitMq集成,kafka集成,分布式id生成器等。项目实战:https://github.com/hemin1003/yfax-parent 已投入生产线上使用
Stars: ✭ 440 (-27.27%)
Mutual labels:  oauth2
Doorkeeper
Doorkeeper is an OAuth 2 provider for Ruby on Rails / Grape.
Stars: ✭ 4,917 (+712.73%)
Mutual labels:  oauth2
Angular Oauth2
AngularJS OAuth2
Stars: ✭ 601 (-0.66%)
Mutual labels:  oauth2
Lumen Passport
Making Laravel Passport work with Lumen
Stars: ✭ 585 (-3.31%)
Mutual labels:  oauth2
Books Recommendation
程序员进阶书籍(视频),持续更新(Programmer Books)
Stars: ✭ 558 (-7.77%)
Mutual labels:  oauth2

Heimdallr

Heimdallr is an OAuth 2.0 client specifically designed for easy usage. It currently supports the resource owner password credentials grant flow, refreshing an access token, as well as extension grants.

If you are an Android Developer, please take a look at the Android version of Heimdallr.

Build Status

Example

Before requesting an access token, the client must be configured appropriately:

let tokenURL = URL(string: "https://example.com/oauth/v2/token")!
let heimdallr = Heimdallr(tokenURL: tokenURL)

On login, the resource owner's password credentials are used to request an access token:

heimdallr.requestAccessToken(username: "johndoe", password: "A3ddj3w") { result in
    switch result {
    case .success:
        print("success")
    case .failure(let error):
        print("failure: \(error.localizedDescription)")
    }
}

Heimdallr automatically persists the access token. Afterwards, any URLRequest can be easily authenticated using the received access token:

var session: URLSession!
var request: URLRequest!

heimdallr.authenticateRequest(request) { result in
    switch result {
    case .success(let request):
        let task = session.dataTask(with: request) { data, response, error in
            // ...
        }
        
        task.resume()
    case .failure(let error):
        print("failure: \(error.localizedDescription)")
    }
}

Installation

Installation is possible via Carthage or CocoaPods, see below for either method:

Carthage

Carthage is a simple, decentralized dependency manager for Cocoa.

  1. Add Heimdallr to your Cartfile:
github "trivago/Heimdallr.swift" ~> 3.6.1
  1. Run carthage update to fetch and build Heimdallr and its dependencies.

  2. Make sure your application's target links against Heimdallr.framework and copies all relevant frameworks into its application bundle (iOS); or embeds the binaries of all relevant frameworks (Mac).

CocoaPods

  1. Add Heimdallr to your Podfile:

    pod 'Heimdallr', '~> 3.6.1'
    
  2. Run pod install to fetch and build Heimdallr and its dependencies.

Usage

OAuthClientCredentials

The client credentials, consisting of the client's identifier and optionally its secret, are used for authenticating with the token endpoint:

var identifier: String!
var secret: String!

let credentials = OAuthClientCredentials(id: identifier)
               // OAuthClientCredentials(id: identifier, secret: secret)

Please note that native applications are considered to be public clients.

OAuthAccessTokenStore

An access token store is used to (persistently) store an access token received from the token endpoint. It must implement the following storage and retrieval methods:

protocol OAuthAccessTokenStore {
    func storeAccessToken(accessToken: OAuthAccessToken?)
    func retrieveAccessToken() -> OAuthAccessToken?
}

Heimdallr ships with an already built-in persistent keychain-based access token store. The service is configurable:

var service: String!

let accessTokenStore = OAuthAccessTokenKeychainStore(service: service)

HeimdallrHTTPClient

An HTTP client that can be used by Heimdallr for requesting access tokens. It must implement the following sendRequest method:

protocol HeimdallrHTTPClient {
    func sendRequest(request: URLRequest, completion: (data: Data!, response: URLResponse!, error: Error?) -> ())
}

For convenience, a default HTTP client named HeimdallrHTTPClientURLSession and based on URLSession is provided. It may be configured with an URLSession:

var urlSession: URLSession!

let httpClient = HeimdallrHTTPClientURLSession(urlSession: session)

OAuthAccessTokenParser

You can provide your own parser to handle the access token response of the server. It can be useful for parsing additional parameters sent in the response that your application may need. The parser must implement the following parse method:

protocol OAuthAccessTokenParser {
    func parse(data: Data) -> Result<OAuthAccessToken, Error>
}

Heimdallr

Heimdallr must be initialized with the token endpoint URL and can optionally be configured with client credentials, an access token store and an HTTP client:

var tokenURL: URL!

let heimdallr = Heimdallr(tokenURL: tokenURL)
             // Heimdallr(tokenURL: tokenURL, credentials: credentials)
             // Heimdallr(tokenURL: tokenURL, credentials: credentials, accessTokenStore: accessTokenStore)
             // Heimdallr(tokenURL: tokenURL, credentials: credentials, accessTokenStore: accessTokenStore, accessTokenParser: accessTokenParser)
             // Heimdallr(tokenURL: tokenURL, credentials: credentials, accessTokenStore: accessTokenStore, accessTokenParser: accessTokenParser, httpClient: httpClient)
             // Heimdallr(tokenURL: tokenURL, credentials: credentials, accessTokenStore: accessTokenStore, accessTokenParser: accessTokenParser, httpClient: httpClient, resourceRequestAuthenticator: resourceRequestAuthenticator)

Whether the client's access token store currently holds an access token can be checked using the hasAccessToken property. It's not checked whether the stored access token, if any, has already expired.

The authorize method takes the resource owner's password credentials as parameters and uses them to request an access token from the token endpoint:

var username: String!
var password: String!

heimdallr.requestAccessToken(username: username, password: password) { result in
    // ...
}

The completion closure may be invoked on any thread.

Once successfully authorized, any URLRequest can be easily altered to include authentication via the received access token:

var request: URLRequest!

heimdallr.authenticateRequest(request) { result in
    // ...
}

If the access token has already expired and a refresh token is available, Heimdallr will automatically refresh the access token. Refreshing requires network I/O. The completion closure may be invoked on any thread.

HeimdallrResourceRequestAuthenticator

By default, Heimdallr authenticates a request by setting the HTTP header field Authorization. This behavior can be changed by passing another resource request authenticator implementing HeimdallrResourceRequestAuthenticator to the initializer.

About

Heimdallr was built by trivago 🏭

Credits

Contains code for query string escaping taken from Alamofire (MIT License)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].