Code Monkey home page Code Monkey logo

drfso2_test's Introduction

Idea :

  • Rest Api with open/protected resources :
    • Available entry points :
      • open : /api/, /api/register/
      • protected : /api/protected/
  • Users :
    • local
    • social
  • Client applications: a) local website, b) mobile app (curl + web browser)
    • register new local user
    • access protected resources

Server backend

  • OAuth2 application : client type is 'Confidental' and authorization grant type is 'Resource owner password-based'

Mobile app

1) Obtain the access token for a github user

Open web browser with URL :

/auth/login/github/?next=/profile/

Authorize the usage of the user information by the server. The final redirection will contain the parameters: access_token, backend, expires

/profile/?access_token=<token>&backend=<backend>&expires=<expires>

Note: a new user will be created automatically

2.1) Create new local user

Send POST request to '/api/register/' with parameters :

  • username=
  • password=
  • email=
curl -X POST -d "username=${username}&password=${password}&email=${email}" /api/register/

Note: user is identified by its unique username

2.2) Obtain the access token for a local user

Send POST request to '/auth/token/' with parameters :

  • username=
  • password=
  • grant_type=password
  • client_id=<client_id>
  • client_secret=<client_secret>
curl -X POST -d "client_id=${client_id}&client_secret=${client_secret}&grant_type=password&username=${username}&password=${password}" ${URL}

3) Access api protected resources

For a local user :

Insert "Authorization: Bearer " to the header and send a request to the url

curl -v -H "Authorization: Bearer <token>" /api/protected/

For a social network user :

Insert "Authorization: Bearer github " to the header and send a request to the url

curl -v -H "Authorization: Bearer github <token>" /api/protected/

Local web site

1) Obtain the access token for a github user

For that user need to login : Html part :

<a href="/auth/login/github/?next=/">Login with Github</a>

Django part : It is possible to insert access token and backend as templates to views.

user = request.user
if user.is_authenticated:
    try:
        # get the last login provider
        provider = request.session['social_auth_last_login_backend']
        social = user.social_auth.get(provider=provider)
        access_token = social.extra_data['access_token']
        expires = social.extra_data['expires']
    except KeyError:
        # This is a local user without social network backend


context = {
    "access_token": access_token,
    "provider": provider,
    "expires": expires,
}

2.1) Create new local user

It can be done with a simple html form

2.2) Obtain the access token for a local user

For that user need to login : configure url to django.contrib.auth.views.login with a custom template html

url(r'^login/$', views.login, {'template_name': 'login.html'}, name='login'),

and redirect the link to the main page.

In the main page django view code to get access token should be separated for social and local users

try:
    provider = request.session['social_auth_last_login_backend']
    social = user.social_auth.get(provider=provider)
    access_token = social.extra_data['access_token']
    expires = social.extra_data['expires']
except KeyError:
    print "This is an ordinary user without social network backend"
    # Issue an access_token
    token = get_or_create_token(user)
    if token is not None:
        access_token = token.token
        provider = None
        expires = token.expires

In the method 'get_or_create_token' find not expired access tokens using 'oauth2_provider.models.AccessToken' :

application = Application.objects.get(name="Local OAuth2 Server with Password")
tokens = AccessToken.objects.filter(user=user, expires__gt=datetime.now(), application=application)

if no tokens found then create one with a refresh token

3) Access api protected resources

This can be done using ajax or whatever other tools that can send requests to the urls. For example, using jquery $.ajax :

For a local user :

var headers = {"Authorization": "Bearer " + token};
var request = $.ajax({
    url: '/api/protected/',
    method: "GET",
    headers: headers
}).done(function( msg ) {
    // Do something with the resulting message object
});

For a social network user :

var headers = {"Authorization": "Bearer github " + token};
var request = $.ajax({
    url: '/api/protected/',
    method: "GET",
    headers: headers
}).done(function( msg ) {
    // Do something with the resulting message object
});

Open questions :

drfso2_test's People

Contributors

vfdev-5 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

murabo

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.