Exemple #1
0
def register_oauth():
    """Register the ``jwql`` application with the ``auth.mast``
    authentication service.

    Returns
    -------
    oauth : Object
        An object containing methods to authenticate a user, provided
        by the ``auth.mast`` service.
    """

    # Get configuration parameters
    client_id = get_config()['client_id']
    client_secret = get_config()['client_secret']
    auth_mast = get_config()['auth_mast']

    # Register with auth.mast
    oauth = OAuth()
    client_kwargs = {'scope': 'mast:user:info'}
    oauth.register(
        'mast_auth',
        client_id='{}'.format(client_id),
        client_secret='{}'.format(client_secret),
        access_token_url='https://{}/oauth/access_token?client_secret={}'.
        format(auth_mast, client_secret),
        access_token_params=None,
        refresh_token_url=None,
        authorize_url='https://{}/oauth/authorize'.format(auth_mast),
        api_base_url='https://{}/1.1/'.format(auth_mast),
        client_kwargs=client_kwargs)

    return oauth
    def test_register_remote_app(self):
        oauth = OAuth()
        self.assertRaises(AttributeError, lambda: oauth.dev)

        oauth.register('dev',
                       client_id='dev',
                       client_secret='dev',
                       request_token_url='https://i.b/reqeust-token',
                       base_url='https://i.b/api',
                       access_token_url='https://i.b/token',
                       authorize_url='https://i.b/authorize')
        self.assertEqual(oauth.dev.name, 'dev')
        self.assertEqual(oauth.dev.client_id, 'dev')
 def test_register_with_overwrite(self):
     oauth = OAuth()
     oauth.register('dev_overwrite',
                    overwrite=True,
                    client_id='dev',
                    client_secret='dev',
                    request_token_url='https://i.b/reqeust-token',
                    base_url='https://i.b/api',
                    access_token_url='https://i.b/token',
                    access_token_params={'foo': 'foo'},
                    authorize_url='https://i.b/authorize')
     self.assertEqual(oauth.dev_overwrite.client_id, 'dev-client-id')
     self.assertEqual(oauth.dev_overwrite.access_token_params['foo'],
                      'foo-1')
    def test_with_fetch_token_in_oauth(self):
        def fetch_token(name, request):
            return {'access_token': name, 'token_type': 'bearer'}

        oauth = OAuth(fetch_token)
        client = oauth.register(
            'dev',
            client_id='dev',
            client_secret='dev',
            api_base_url='https://i.b/api',
            access_token_url='https://i.b/token',
            authorize_url='https://i.b/authorize'
        )

        def fake_send(sess, req, **kwargs):
            self.assertEqual(sess.token['access_token'], 'dev')
            return mock_send_value(get_bearer_token())

        with mock.patch('requests.sessions.Session.send', fake_send):
            request = self.factory.get('/login')
            client.get('/user', request=request)
 def test_register_from_settings(self):
     oauth = OAuth()
     oauth.register('dev')
     self.assertEqual(oauth.dev.client_id, 'dev-key')
     self.assertEqual(oauth.dev.client_secret, 'dev-secret')
Exemple #6
0
from django.http import HttpResponse, JsonResponse
from django.urls import include, path
from authlib.django.client import OAuth
from loginpass import create_django_urlpatterns
from loginpass import OAUTH_BACKENDS

oauth = OAuth()


def handle_authorize(request, remote, token, user_info):
    return JsonResponse(user_info)


urlpatterns = []
for backend in OAUTH_BACKENDS:
    oauth_urls = create_django_urlpatterns(backend, oauth, handle_authorize)
    urlpatterns.append(path(backend.OAUTH_NAME + '/', include(oauth_urls)))


def home(request):
    tpl = '<li><a href="/{}/login">{}</a></li>'
    lis = [tpl.format(b.OAUTH_NAME, b.OAUTH_NAME) for b in OAUTH_BACKENDS]
    html = '<ul>{}</ul>'.format(''.join(lis))
    return HttpResponse(html)
Exemple #7
0
import os
from authlib.django.client import OAuth

RACO_CLIENT_ID = os.getenv('RACO_CLIENT_ID')
RACO_CLIENT_SECRET = os.getenv('RACO_CLIENT_SECRET')
RACO_AUTH_URL = os.getenv('RACO_AUTH_URL')

oauth = OAuth()
oauth.register(
    'raco',
    client_id=RACO_CLIENT_ID,
    client_secret=RACO_CLIENT_SECRET,
    request_token_url=None,
    request_token_params=None,
    access_token_url='https://api.fib.upc.edu/v2/o/token',
    access_token_params=None,
    refresh_token_url='https://api.fib.upc.edu/v2/o/token',
    authorize_url='https://api.fib.upc.edu/v2/o/authorize',
    api_base_url='https://api.fib.upc.edu/v2/',
    client_kwargs={
        'response_type': 'code',
        'scope': 'read',
        'approval_prompt': 'force'
    },
)
Exemple #8
0
def get_oauth_client(apps):
    oauth_client = OAuth()
    for name, values in apps.items():
        oauth_client.register(name, **values)
    return oauth_client
Exemple #9
0
from django.contrib.auth import login as authLogin
from django.http import (HttpResponse, HttpResponseRedirect)

from authlib.django.client import OAuth
from .models import OAuth2Token, UserPreferences
from .schema import UserLoginResult

from .. import jsonapi


def fetch_token(name, request):
    item = OAuth2Token.objects.get(name=name, user=request.user)
    return item.to_token()


oauth = OAuth(fetch_token=fetch_token)
oauth.register('lichess')


def index(request):
    return render(request, "sonder/frontend/index.html")


def login(request):
    redirect_uri = request.build_absolute_uri(reverse('login.authorize'))
    response = oauth.lichess.authorize_redirect(request, redirect_uri)
    return HttpResponse(json.dumps({'url': response.url}))


@transaction.atomic
def authorize(request):
Exemple #10
0
                    vacation_days=attrs.get('vacation_days', {}),
                    project_settings=attrs.get('project_settings', {}))
                new_django_project.save()
                new_projects.append(project_name)
            else:
                p.velocities = velocity_json
                p.project_settings = attrs.get('project_settings',
                                               p.project_settings)
                p.mode = attrs.get('mode', p.mode)
                p.save()
                updated_projects.append(project_name)
    logging.info('parse_tms has finished')
    response_message = ''
    if len(new_projects) > 0:
        response_message += "New projects found and parsed: {}.".format(
            ', '.join(new_projects))
    if len(updated_projects) > 0:
        response_message += " Updated existing projects: {}.".format(
            ', '.join(updated_projects))
    return response_message


PROD_HOST_URL = getattr(settings, "PROD_HOST_URL", "http://localhost:8000")
atlassian_redirect_uri = PROD_HOST_URL + '/atlassian_callback'
logging.debug('atlassian_redirect_uri: "{}"'.format(atlassian_redirect_uri))

oauth = OAuth(fetch_token=fetch_oauth_token, update_token=update_oauth_token)
oauth.register(name='atlassian')
logging.debug('oauth registered: {}'.format(oauth.atlassian))
logging.info('models import finished.')