Package: django-upwork-auth
Simple Upwork login for your app. Tested with Django 1.8 (see example project).Before oDesk rebranded as Upwork, this library was called django-odesk-auth.
The latest version of django-odesk-auth works with Upwork already.
This library is not backwards compatible with django-odesk-auth.Creating Upwork OAuth API keyGo tohttps://www.upwork.com/services/api/apply.Authentication type should be set to “OAuth 1.0”.Callback URL should be left blank.Default authentication backend requires
“View the structure of your companies/teams” permission to be checked.Adding Upwork auth to your Django projectMake sure you havedjango-upwork-authandpython-upwork==1.0installed.
Make sure you have Django’s sites and session frameworks enabled.Add'django_upwork_auth'toINSTALLED_APPS.Add'django_upwork_auth.backends.UpworkOAuthBackend'to AUTHENTICATION_BACKENDS.SpecifyUPWORK_OAUTH_KEYandUPWORK_OAUTH_SECRETsettings
with your key information.Add'your_upwork_username'toUPWORK_AUTH_ALLOWED_USERS,
and setUPWORK_AUTH_AUTO_CREATE_USERSto True.Includedjango_upwork_auth.urlsin your URL patterns.In your login page template, put a link (say, “Log in via Upwork”)
and point it to{% url "upwork_oauth_login" %}.Open login page and click “Log in via Upwork” to verify everything works.IMPORTANT: keepUPWORK_OAUTH_KEYandUPWORK_OAUTH_SECRETsettings
in a file that is not under version control. One way to do that is to keep
all public settings in versioned file named saysettings_base.py,
from which you import everything insettings.pythat is not versioned.
Django in this setup should be pointed tosettings.py, as usual.Example projectRequirements: Vagrant, Ansible, and free 8000 port.First, fill in the necessary settings inexample_project/settings.py(see comments in the file).Fromexample_projectdirectory, bring up a VM using Vagrantfile provided
and manually run Django development server in it:$ vagrant up
$ vagrant ssh
vm$ cd /vagrant/example_project/
vm$ ./manage.py runserver 0.0.0.0:8000On your host machine, navigate to 127.0.0.1:8000 where you should be able
to test Upwork login functionality in action.Authentication backendStock authentication backend assumes that you only use Upwork login in your app.
When someone logs in, if their Upwork ID matches existing username, it logs
them in as that user. If there’s no username matching given Upwork ID, it optionally
creates a user with such username.ATTENTION: Watch out you are using other authentication methods
or for some reason have any chance of one end user’s Upwork ID matching
existing username of another end user!Stock authentication backend provides optional basic access control facilities.
You can specify who is allowed to log in to your site and who upon login gets
staff and/or superuser statuses. This is configured through Django settings.If whitelisting is on and given user is not on the white list, theirUser.is_activeflag gets set to False upon login.See below for settings provided by stock authentication backend.You can subclass stock authentication backend to override user manipulation
(seehandle_unknown_user()andupdate_user()methods).Making authenticated Upwork API callsAfter user is successfully authenticated you can call Upwork API on their behalf.
For that you’d need the access token obtained during OAuth flow.By default this app uses Django’s built-in session framework to store
access token. The key it uses can be retrieved fromdjango_upwork_auth.settings.ACCESS_TOKEN_SESSION_KEYand customized viaUPWORK_OAUTH_ACCESS_TOKEN_SESSION_KEYsetting.Example code you can have in your view:from django_upwork_auth import utils, settings

upwork_client = utils.get_client(
    request.session[settings.ACCESS_TOKEN_SESSION_KEY])

print upwork_client.hr.get_teams()
# Should output list of teamrooms current user belongs toYou can use another storage technique by overridingUPWORK_OAUTH_ACCESS_TOKEN_STORE_FUNC. It’s useful if you need to make Upwork API call
but can’t easily read user’s session because there’s no request context.
For example, you can store access token with associated username in Redis
and query it in your asynchronous tasks.Note:How you make API calls is up to you. Internally django-upwork-auth
uses python-upwork library, and so does the above example.utils.get_client()function returns an instance ofupwork.Client.
Handy if you’re using python-upwork library to make API calls.Verifying OAuth access tokenSometimes there’s a need to make sure that current user’s authentication
is still valid—that they, for example, didn’t revoke access to their account.This app provides a helper for that: seeutils.check_login().
It can be used in a view like this:from django_upwork_auth import utils

def oauth_check_login(request):
    u"""Verifies OAuth access token and user status on Upwork.
    Returns HTTP 200 (OK) or HTTP 401 (Unauthorized)
    with additional information in response body text.
    """
    access_token = utils.access_token.get(request)

    if access_token is None or len(access_token) != 2:
        return http.HttpResponse(
            u"Bad or missing Upwork OAuth access token", status=401)

    result, details = utils.check_login(access_token)

    if result is True:
        return http.HttpResponse(details, status=200)
    else:
        return http.HttpResponse(details, status=401)Available Django settingsUPWORK_OAUTH_KEY, UPWORK_OAUTH_SECRETAPI key information.UPWORK_AUTH_LOGIN_REDIRECT_URL = settings.LOGIN_REDIRECT_URLWhere to redirect the user at the end of OAuth flow.
Path or URL pattern name.UPWORK_AUTH_ACCESS_TOKEN_STORE_FUNCFunction to be called to store OAuth access token for future access.
It’s passed two arguments: a request where user is already
authenticated and the access token associated with that user.
Default implementation stores token in session underACCESS_TOKEN_SESSION_KEY.Specific to stock authentication backendThese are relevant unless you subclass stockbackends.UpworkOAuthBackendand override
some of its logic.By default anyone can log in. Whitelist mode can be turned on by assigning non-empty tuple
toUPWORK_AUTH_WHITELISTorUPWORK_AUTH_TEAM_WHITELISTsetting.UPWORK_AUTH_AUTO_CREATE_USERS = FalseWhether to create a new account in Django if given user uses Upwork login
for the first time (i.e., ID returned by Upwork is free in your Django DB).UPWORK_AUTH_WHITELIST = ()Upwork IDs of users who are allowed to log in via Upwork.UPWORK_AUTH_STAFF_WHITELIST = ()Upwork IDs of users who are marked asis_staffupon login.UPWORK_AUTH_SUPERUSER_WHITELIST = ()Upwork IDs of users who are marked asis_superuserupon login.UPWORK_AUTH_TEAM_WHITELIST = ()IDs of Upwork teamrooms, members of which are allowed to log in via Upwork.UPWORK_AUTH_STAFF_TEAM_WHITELIST = ()IDs of Upwork teamrooms, members of which are marked asis_staffupon login.UPWORK_AUTH_SUPERUSER_TEAM_WHITELIST = ()IDs of Upwork teamrooms, members of which are marked asis_superuserupon login.