Giter VIP home page Giter VIP logo

aio-alf's Introduction

aio-alf build-status ===========

aiohttp OAuth 2 Client ---------------------

aio-alf is a OAuth 2 Client base on the aiohttp's AsyncHTTPClient

Features

  • Automatic token retrieving and renewing
  • Token expiration control
  • Automatic retry on status 401 (UNAUTHORIZED)

Usage

Initialize the client and use it as a AsyncHTTPClient object.

from aioalf.client import Client
from aioalf.httpclient import HTTPRequest

client = Client(
    token_endpoint='http://example.com/token',
    client_id='client-id',
    client_secret='secret')

resource_uri = 'http://example.com/resource'

response = await client.request(
    'POST',
    resource_uri,
    data='{"name": "alf"}',
    headers={'Content-Type': 'application/json'}
)

Alternatively one can pass directly a string to the fetch client

# ...
response = await client.request(
    'POST',
    'http://example.com/resource',
    data='{"name": "alf"}',
    headers={'Content-Type': 'application/json'}
)

Implicit Flow

Support for OAuth2 implict flow to enable it, call use_implicit_flow with a TokenStorage object and a port range, it defaults to the range (32000, 32009).

Example:

await use_implicit_flow(TokenStorage(), (30000, 30009))

async with Client(token_endpoint='https://token.endpoint',
                  client_id='glBQ3nYU/8/kaVi/bIgXGA==',
                  client_secret='') as client:
    response = await client.request('GET', 'http://example.com/resource')
    text = await response.text()
    print(response.status)

The library has a really simple in memory token storage, you should subclass and overwrite its methods if you need to persist the token for a longer period.

How it works?

Before any request the client tries to retrieve a token on the endpoint, expecting a JSON response with the access_token and expires_in keys.

The client keeps the token until it is expired, according to the expires_in value.

After getting the token, the request is issued with a Bearer authorization header:

GET /resource/1 HTTP/1.1
Host: example.com
Authorization: Bearer token

If the request fails with a 401 (UNAUTHORIZED) status, a new token is retrieved from the endpoint and the request is retried. This happens only once, if it fails again the error response is returned.

Troubleshooting

In case of an error retrieving a token, the error response will be returned, the real request won't happen.

Related projects

This project tries to be an adaptation to aiohttp of alf

aio-alf's People

Contributors

pedrokiefer avatar silascoker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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

aio-alf's Issues

Please support OAuth 2.0 scopes

Hello,

thanks for creating aio-alf.

I have noticed that there is no way for TokenManager to send the scope[1] parameter in its token request:

data={'grant_type': 'client_credentials'}

Specifying the scopes is quite useful and I was wondering whether this functionality could be added to the library.

As a (quite ugly) workaround, I have overridden the _request_token method in a custom token manager class and using it in a custom subclass of Client.

token_manager_class = TokenManager

What do you think about optionally passing the scopes to the Client so it can forward them to its token manager?

Many thanks

[1] https://tools.ietf.org/html/rfc6749#section-3.3

Debugging output exposes authorization token in Authorization header

Hello,

when turning on debugging, aioalf logs the Authorization header of the request, thus disclosing private credentials.

I would like to suggest that such header is filtered before logging so credentials are not exposed (this is a security best practice).

For example, one could log the first 5 and last 2 characters of the token, something like:

DEBUG:aioalf.client:Header Authorization: Bearer abcde<...>fg

This happens in both the client and the manager:

logger.debug('Header %s: %s', header, kwargs.get('headers').get(header))

logger.debug('Header %s: %s', header, request_data.get('headers', {}).get(header))

I may be able to submit a PR if you are willing to take it.

Thanks

TokenManager: wrong initialization of TokenError results in unhelpful error message

Hello,

line 79 in manager.py initializes a TokenError object with the wrong number of arguments, resulting in an unhelpful TypeError: __init__() takes 2 positional arguments but 3 were given exception.

'Missing credentials (client_id:client_secret)', str(e)

I think a viable solution would be to use something like

raise TokenError('Missing credentials (client_id:client_secret), {}'.format(e))

Many thanks

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.