GithubHelp home page GithubHelp logo

raekkeri / sanction Goto Github PK

View Code? Open in Web Editor NEW

This project forked from demianbrecht/sanction

0.0 2.0 0.0 382 KB

A simple, easy-to-use OAuth2 client implementation.

License: MIT License

Python 100.00%

sanction's Introduction

https://secure.travis-ci.org/demianbrecht/sanction.png?branch=master

sanction [sangk-shuhn]: authoritative permission or approval, as for an action.

sanction is a lightweight, dead simple client implementation of the OAuth2 protocol. The major goals of the library are:

  • Grok me
    • Variations on OAuth2 client implementation range from a few hundred LOC to thousands. In a Pythonic world, there's absolutely no need for this when simply dealing with the client side of the spec. Currently, sanction sits at a whopping 65 LOC, one class. This makes the library tremendously easy to grok.
  • Support multiple providers
    • Most providers have varying levels of diversion from the official spec. The goal with this library is to either handle these diversions natively, or expose a method to allow client code to deal with it efficiently and effectively.
  • Support all server-side OAuth2 flows
    • Three of the four OAuth2 flows should be supported by this library. Currently, only authorization code and client credential flows have been tested due to lack of other (known) implementations.

sanction has been tested with the following OAuth2 providers:

note:The intention of the sanction library is to not only provide accessibility to the OAuth2 authorization code flow, but all server-side flows. However, due to lack of implementations, the only tested currently teseted flows are authorization code and client credentials.

For the quickstart, authorization code grant flow is assumed, as is the Bearer token type. If you're unfamiliar with these terms, chances are that they're what you're looking for as it's the default in most public OAuth2 provider implementations (Google, Facebook, Foursquare, etc.).

Introducing this library should be rather trivial (in the usual basic case). There are three steps required in the most common use case (Google is assumed to be the provider throughout sample code):

You can also take a look at the example code in /example.

To access protected resources via the OAuth2 protocol, you must instantiate a Client and pass it relevant endpoints for your current operation:

from sanction.client import Client

# instantiating a client to get the auth URI
c = Client(auth_endpoint="https://accounts.google.com/o/oauth2/auth",
    client_id=config["google.client_id"],
    redirect_uri="http://localhost:8080/login/google")

# instantiating a client to process OAuth2 response
c = Client(token_endpoint="https://accounts.google.com/o/oauth2/token",
    resource_endpoint="https://www.googleapis.com/oauth2/v1",
    redirect_uri="http://localhost:8080/login/google",
    client_id=config["google.client_id"],
    client_secret=config["google.client_secret"])

Of course, you may create the config dict in your preferred method, the above is simply for demonstration using the required config settings (the example project uses ConfigParser against an .ini file for settings.

The next step is to redirect the user agent to the provider's authentication/ authorization uri (continuation from previous code block):

scope_req = ("scope1","scope2",)
my_redirect(c.auth_uri(scope_req))

You can also elect to use the optional state parameter to pass a CSRF token that will be included if the provider's response:

my_redirect(c.auth_uri(state=my_state))
note:It is strongly encouraged that you use the state parameter to offer CSRF protection. It is also up to you to process the state parameter and handle redirection accordingly before calling request_token.

When the user has granted or denied resource access to your application, they will be redirected to the redirect_uri as specified by the value of the GET param. In order to request an access token from the provider, you must request an access token from the provider:

c.request_token(response_dict)

Some (not all) providers provide the ability to refresh a given access token, giving an application to users' data even if they're offline (Google is one of these providers). If your client previously received a refresh token with the initial code/token exchange, then you can use the request_token API to request a refreshed token:

c.request_token(grant_type='refresh_token',
    refresh_token=my_refresh_token)

An example of this is shown in the Google login handler in the sample app.

If the user has granted access and your config settings are correct, you should then be able to access protected resources through the adapter's API:

c.request("/userinfo")

If the provider has deviated from the OAuth2 spec and the response isn't JSON (i.e. Stack Exchange), you can pass a custom parser to request:

c.request("/userinfo", parser=lambda c: dosomething(c))

There are no implementations for individual OAuth2-exposed resources. This is not the intention of the library and will not be added.

sanction's People

Contributors

alectaylor avatar demianbrecht avatar

Watchers

 avatar  avatar

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.