Beispiel #1
0
def authorized():
    if ('auth_token' in session and isinstance(session['auth_token'], tuple)
            and session['auth_token']):
        return JobToken.get_info(session.get('auth_token', (None, ))[0])

    logger.info(
        "Authorized redirection triggered, getting authorized response from Github"
    )
    resp = github.authorized_response()
    logger.info("Got Github authorized response")

    if resp is None or resp.get('access_token') is None:
        msg = 'Access denied: reason=%s error=%s resp=%s' % (
            request.args['error'], request.args['error_description'], resp)
        logger.warning(msg)
        return {'error': msg}, 400

    logger.debug("Assigning authorization token '%s' to session",
                 resp['access_token'])
    session['auth_token'] = (resp['access_token'], '')
    oauth_info = github.get('user')
    if not is_organization_member(oauth_info.data):
        logger.debug("User '%s' is not member of organization '%s'",
                     oauth_info.data['login'], AUTH_ORGANIZATION)
        logout()
        return {'error': 'unauthorized'}, 401

    token_info = JobToken.store_token(oauth_info.data['login'],
                                      resp['access_token'])
    return token_info
Beispiel #2
0
def authorized():
    """Perform authorization via GitHub service."""
    auth_token = request.headers.get('auth_token')
    if 'auth_token' in session:
        # Authorization token in session has higher priority
        auth_token = session.get('auth_token', (None, ))[0]

    if auth_token:
        return JobToken.get_info(auth_token)

    logger.info(
        "Authorized redirection triggered, getting authorized response from Github"
    )
    resp = github.authorized_response()
    logger.info("Got Github authorized response")

    if resp is None or resp.get('access_token') is None:
        msg = 'Access denied: reason=%s error=%s resp=%s' % (
            request.args['error'], request.args['error_description'], resp)
        logger.warning(msg)
        return {'error': msg}, 400

    logger.debug("Assigning authorization token '%s' to session",
                 resp['access_token'])
    session['auth_token'] = (resp['access_token'], '')
    oauth_info = github.get('user')
    if not is_organization_member(oauth_info.data):
        logger.debug("User '%s' is not member of organization '%s'",
                     oauth_info.data['login'], AUTH_ORGANIZATION)
        logout()
        return {'error': 'unauthorized'}, 401

    token_info = JobToken.store_token(oauth_info.data['login'],
                                      resp['access_token'])
    return token_info