Пример #1
0
def linkedin_process(request):
    """Process the LinkedIn redirect"""
    if 'denied' in request.GET:
        raise AuthenticationDenied("User denied authentication")

    config = request.registry.settings
    request_token = oauth.Token.from_string(request.session['token'])
    verifier = request.GET.get('oauth_verifier')
    if not verifier:
        raise ThirdPartyFailure("Status %s: %s" % (r.status_code, r.content))
    request_token.set_verifier(verifier)

    # Create the consumer and client, make the request
    consumer = oauth.Consumer(config['velruse.linkedin.consumer_key'],
                              config['velruse.linkedin.consumer_secret'])

    client = oauth.Client(consumer, request_token)
    resp, content = client.request(ACCESS_URL, "POST")
    if resp['status'] != '200':
        raise ThirdPartyFailure("Status %s: %s" % (resp['status'], content))
    access_token = dict(parse_qs(content))

    cred = {'oauthAccessToken': access_token['oauth_token'][0],
            'oauthAccessTokenSecret': access_token['oauth_token_secret'][0]}
    
    # Make a request with the data for more user info
    token = oauth.Token(key=cred['oauthAccessToken'],
                        secret=cred['oauthAccessTokenSecret'])
    client = oauth.Client(consumer, token)
    profile_url = 'http://api.linkedin.com/v1/people/~'
    profile_url += ':(first-name,last-name,id,date-of-birth,picture-url)'
    profile_url += '?format=json'
    resp, content = client.request(profile_url)

    if resp['status'] != '200':
        raise ThirdPartyFailure("Status %s: %s" % (resp['status'], content))
    data = loads(content)

    # Setup the normalized contact info
    profile = {}
    profile['providerName'] = 'LinkedIn'
    profile['displayName'] = data['firstName'] + data['lastName']
    profile['name'] = {
        'givenName': data['firstName'],
        'familyName': data['lastName'],
        'formatted': data['firstName'] + data['lastName']
    }
    profile['identifier'] = data['id']

    # Create and raise our AuthenticationComplete exception with the
    # appropriate data to be passed
    complete = AuthenticationComplete()
    complete.profile = profile
    complete.credentials = cred
    raise complete
Пример #2
0
def bitbucket_process(request):
    """Process the bitbucket redirect"""
    if 'denied' in request.GET:
        raise AuthenticationDenied("User denied authentication")

    config = request.registry.settings
    request_token = oauth.Token.from_string(request.session['token'])
    verifier = request.GET.get('oauth_verifier')
    if not verifier:
        raise ThirdPartyFailure("Status %s: %s" % (r.status_code, r.content))
    request_token.set_verifier(verifier)

    # Create the consumer and client, make the request
    consumer = oauth.Consumer(config['velruse.bitbucket.consumer_key'],
                              config['velruse.bitbucket.consumer_secret'])

    client = oauth.Client(consumer, request_token)
    resp, content = client.request(ACCESS_URL, "POST")
    if resp['status'] != '200':
        raise ThirdPartyFailure("Status %s: %s" % (resp['status'], content))
    access_token = dict(parse_qs(content))
    
    cred = {'oauthAccessToken': access_token['oauth_token'][0],
            'oauthAccessTokenSecret': access_token['oauth_token_secret'][0]}
    
    # Make a request with the data for more user info
    token = oauth.Token(key=cred['oauthAccessToken'],
                        secret=cred['oauthAccessTokenSecret'])
    
    client = oauth.Client(consumer, token)
    resp, content = client.request(USER_URL)
    user_data = json.loads(content)
    # Setup the normalized contact info
    profile = {}
    profile['providerName'] = 'bitbucket'
    profile['displayName'] = user_data['user']['username']
    profile['identifier'] = 'https://api.bitbucket.org/1.0/users/%s/' % user_data['user']['username']
    profile['name'] = {
                       ''
                       'givenName': user_data['user']['first_name'],
                       'familyName': user_data['user']['last_name']
                       }
    cred = {'oauthAccessToken': access_token['oauth_token'][0],
            'oauthAccessTokenSecret': access_token['oauth_token_secret'][0]}

    # Create and raise our AuthenticationComplete exception with the
    # appropriate data to be passed
    complete = AuthenticationComplete()
    complete.profile = profile
    complete.credentials = cred
    raise complete
Пример #3
0
def github_process(request):
    """Process the github redirect"""
    config = request.registry.settings
    code = request.GET.get('code')
    if not code:
        reason = request.GET.get('error', 'No reason provided.')
        raise AuthenticationDenied(reason)

    # Now retrieve the access token with the code
    access_url = flat_url('https://github.com/login/oauth/access_token',
                          client_id=config['velruse.github.app_id'],
                          client_secret=config['velruse.github.app_secret'],
                          redirect_uri=request.route_url('github_process'),
                          code=code)
    r = requests.get(access_url)
    if r.status_code != 200:
        raise ThirdPartyFailure("Status %s: %s" % (r.status_code, r.content))
    access_token = parse_qs(r.content)['access_token'][0]
    c = r.content

    # Retrieve profile data
    graph_url = flat_url('https://github.com/api/v2/json/user/show',
                         access_token=access_token)
    r = requests.get(graph_url)
    if r.status_code != 200:
        raise ThirdPartyFailure("Status %s: %s" % (r.status_code, r.content))
    data = loads(r.content)['user']

    profile = {}
    profile['providerName'] = 'GitHub'
    profile['displayName'] = data['name']
    profile['identifier'] = profile['preferredUsername'] = data['login']

    if 'email' in data:
        profile['emails'] = [data['email']]
        profile['verifiedEmail'] = data['email']
    
    cred = {'oauthAccessToken': access_token}

    # Create and raise our AuthenticationComplete exception with the
    # appropriate data to be passed
    complete = AuthenticationComplete()
    complete.profile = profile
    complete.credentials = { 'oauthAccessToken': access_token }
    raise complete
Пример #4
0
def facebook_process(request):
    """Process the facebook redirect"""
    if request.GET.get('state') != request.session.get('state'):
        raise CSRFError(
            "CSRF Validation check failed. Request state %s is "
            "not the same as session state %s" %
            (request.GET.get('state'), request.session.get('state')))
    config = request.registry.settings
    code = request.GET.get('code')
    if not code:
        reason = request.GET.get('error_reason', 'No reason provided.')
        raise AuthenticationDenied(reason)

    # Now retrieve the access token with the code
    access_url = flat_url(
        'https://graph.facebook.com/oauth/access_token',
        client_id=config['velruse.facebook.app_id'],
        client_secret=config['velruse.facebook.app_secret'],
        redirect_uri=request.route_url('facebook_process'),
        code=code)
    r = requests.get(access_url)
    if r.status_code != 200:
        raise ThirdPartyFailure("Status %s: %s" % (r.status_code, r.content))
    access_token = parse_qs(r.content)['access_token'][0]

    # Retrieve profile data
    graph_url = flat_url(
        'https://graph.facebook.com/me', access_token=access_token)
    r = requests.get(graph_url)
    if r.status_code != 200:
        raise ThirdPartyFailure("Status %s: %s" % (r.status_code, r.content))
    fb_profile = loads(r.content)
    profile = extract_fb_data(fb_profile)

    # Create and raise our AuthenticationComplete exception with the
    # appropriate data to be passed
    complete = AuthenticationComplete()
    complete.profile = profile
    complete.credentials = {'oauthAccessToken': access_token}
    raise complete
Пример #5
0
def twitter_process(request):
    """Process the Twitter redirect"""
    if 'denied' in request.GET:
        raise AuthenticationDenied("User denied authentication")

    config = request.registry.settings
    request_token = oauth.Token.from_string(request.session['token'])
    verifier = request.GET.get('oauth_verifier')
    if not verifier:
        raise ThirdPartyFailure("Status %s: %s" % (r.status_code, r.content))
    request_token.set_verifier(verifier)

    # Create the consumer and client, make the request
    consumer = oauth.Consumer(config['velruse.twitter.consumer_key'],
                              config['velruse.twitter.consumer_secret'])

    client = oauth.Client(consumer, request_token)
    resp, content = client.request(ACCESS_URL, "POST")
    if resp['status'] != '200':
        raise ThirdPartyFailure("Status %s: %s" % (resp['status'], content))
    access_token = dict(parse_qs(content))
    
    # Setup the normalized contact info
    profile = {}
    profile['providerName'] = 'Twitter'
    profile['displayName'] = access_token['screen_name'][0]
    profile['identifier'] = 'http://twitter.com/?id=%s' % access_token['user_id'][0]
    
    cred = {'oauthAccessToken': access_token['oauth_token'][0],
            'oauthAccessTokenSecret': access_token['oauth_token_secret'][0]}

    # Create and raise our AuthenticationComplete exception with the
    # appropriate data to be passed
    complete = AuthenticationComplete()
    complete.profile = profile
    complete.credentials = cred
    raise complete