Giter Site home page Giter Site logo

raulriera / authenticationviewcontroller Goto Github PK

View Code? Open in Web Editor NEW
257.0 13.0 14.0 177 KB

A simple to use, standard interface for authenticating to oauth 2.0 protected endpoints via SFSafariViewController.

License: MIT License

Swift 67.46% Objective-C 4.44% Ruby 28.10%
sfsafariviewcontroller authentication oauth swift

authenticationviewcontroller's Introduction

AuthenticationViewController

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

Step1

Instructions

In order to use this View Controller you need to be running iOS 9 on your simulator or device.

Step 1

Setup the URL Scheme of your app as shown in the image below. (You can find this in the Info tab of your project's settings)

Step1

Step 2

Prepare your AppDelegate to handle this newly created URL Scheme

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {

    // Before doing this, you should check the url is your redirect-uri before doing anything. Be safe :)
    if let components = NSURLComponents(URL: url, resolvingAgainstBaseURL: false),
        queryItems = components.queryItems, 
        code = queryItems.first?.value {

        // Let's find the instance of our authentication controller, 
        // it would be the presentedViewController. This is another 
        // reason to check before that we are actually coming from the SFSafariViewController
        if let rootViewController = window?.rootViewController,
            authenticationViewController = rootViewController.presentedViewController as? AuthenticationViewController {
            authenticationViewController.authenticateWithCode(code)
        }

        return true
    }

    return false
}

Note that you need to pass the authentication code received by your URL scheme to the AuthenticationViewController so it can exchange it for an actual access token.

Step 3

Create an AuthenticationProvider following the AuthenticationProvider protocol.

Step 4

Instantiate an AuthenticationViewController in your code and pass in the provider.

let provider = OAuthDribbble(clientId: "your-client-id", 
    clientSecret: "your-client-secret", 
    scopes: ["public", "upload"])

let authenticationViewController = AuthenticationViewController(provider: provider)

authenticationViewController.failureHandler = { error in
    print(error)
}

authenticationViewController.authenticationHandler = { token in
    print(token)

    authenticationViewController.dismissViewControllerAnimated(true, completion: nil)
}

presentViewController(authenticationViewController, animated: true, completion: nil)

That is it, when you fill in your user account in the AuthenticationViewController and if everything went correctly you should get the access token in the authenticationHandler closure. Otherwise check for any errors in the failureHandler closure.

Installation

Choose one of the following options.

Carthage

Add the following to your Cartfile:

github "raulriera/AuthenticationViewController"

CocoaPods

Add the following to your Podfile:

pod "AuthenticationViewController"

Manual

Just drag and drop the AuthenticationViewController/AuthenticationViewController folder into your project.

Example

Sometimes it's easier to dig in into some code, included in this project is an example for Dribbble and Instagram. You will still need to edit the source code to provide real clientId, clientSecret, and your URL scheme.

Created by

Raul Riera, @raulriera

authenticationviewcontroller's People

Contributors

raulriera avatar readmecritic avatar regnerjr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

authenticationviewcontroller's Issues

Coub doesn't work properly

Description for coub:
https://coub.com/dev/docs/Coub+API%2FAuthentication

If you need test credentials:
ID:
60b88af1e39803f5f68716bc364a498f9d4e733a7f76735dc48729e697de3bd6
Secret:
6f62d7366a58b07377a3046922d49c4f5ab882eebe322f50467856bb7b86e049
Redirect URL:
http://gargo.of.by
Permissions:
like+recoub

I used your example and added OAuthCoub (looks like your OAuthDribble and OAuthInstagram) and called it. It seems everything ok with authorization - successfully logged in, and redirected to "Redirect URL". The only main trouble is application:openURL:options: is not called at all and so I don't have a token in my app.

Could you please resolve this strange issue?

Can't receive access_token

Does it work correctly fro Twitter/Facebook or Google+ ?
Can't manage any of them.
Can anyone provide an example with those networks?
e.g. I have following url for twitter:
"https://api.twitter.com/oauth/authorize/?client_id=\(clientId)&scope=\(scopes.joined(separator: "+"))&redirect_uri=(redirectURI)&response_type=code"
accessTokenURL = https://api.twitter.com/oauth/access_token"

redirectURI = "https://www.google.com"

Then, I put my ClientID and Secret, but nothing works. I can login and do not receive my token after switching to redirectURL.

Google implementation connector for OAuth2

Here I share the connector for Google OAuth2 with an usage example (hope its help):

OAuthGoogle.swift

static let ClientId = "188766122934-0t21j64fd4qmcomewpho6jeqwxe6kpwq.apps.googleusercontent.com"
static let ClientSecret = "AIza34De1h2JIehyugxcgYnXewtokGdi0xXPsJMTY"
    
static let ScopeYoutubeReadonly = "https://www.googleapis.com/auth/youtube.readonly"
static let ScopeUserinfoEmail = "https://www.googleapis.com/auth/userinfo.email"
static let ScopeUserinfoProfile = "https://www.googleapis.com/auth/userinfo.profile"
let provider = OAuthGoogle(clientId: ServiceRequest.ClientId, clientSecret: "", scopes: [ServiceRequest.ScopeUserinfoEmail, ServiceRequest.ScopeUserinfoProfile, ServiceRequest.ScopeYoutubeReadonly])
let authenticationViewController = AuthenticationViewController(provider: provider)
authenticationViewController.failureHandler = { error in
	print(error)
}
authenticationViewController.authenticationHandler = { token in
	print (token)
	authenticationViewController.dismiss(animated: true, completion: nil)
}
present(authenticationViewController, animated: true, completion: nil)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.