Beispiel #1
0
def get_user_info_after_auth(request):
    redirect_uri = request.session['gh_redirect_uri']
    del request.session['gh_redirect_uri']
    flow = get_flow(redirect_uri)
    if 'code' not in request.GET:
        return None
    code = request.GET['code']
    credentials = flow.step2_exchange(code)

    http = httplib2.Http(".cache")
    http = credentials.authorize(http)
    (_, content) = http.request("https://api.github.com/user", "GET")
    response = utils.from_json(content.decode('utf-8'))
    user_id = response['login']
    user_name = response['name']

    (_, content) = http.request("https://api.github.com/user/emails", "GET")
    response = utils.from_json(content.decode('utf-8'))
    user_email = None
    for email in response:
        if email['verified'] and email['primary']:
            user_email = email['email']
            break
    if not user_email:
        raise Exception("email address with GitHub not verified")

    return {
        'type': 'github',
        'user_id': user_id,
        'name': '%s (%s)' % (user_id, user_name),
        'info': {
            'email': user_email
        },
        'token': {},
    }
Beispiel #2
0
def get_user_info_after_auth(request):
  args = facebook_get('/oauth/access_token', {
      'client_id' : APP_ID,
      'redirect_uri' : request.session['fb_redirect_uri'],
      'client_secret' : API_SECRET,
      'code' : request.GET['code']
      })

  access_token = utils.from_json(args)['access_token']

  info = utils.from_json(facebook_get('/me', {'access_token':access_token}))

  return {'type': 'facebook', 'user_id' : info['id'], 'name': info.get('name'), 'email': info.get('email'), 'info': info, 'token': {'access_token': access_token}}
Beispiel #3
0
def get_user_info_after_auth(request):
  flow = get_flow(request.session['google-redirect-url'])

  if 'code' not in request.GET:
    return None
  
  code = request.GET['code']
  credentials = flow.step2_exchange(code)

  # the email address is in the credentials, that's how we make sure it's verified
  id_token = credentials.id_token
  if not id_token['email_verified']:
    raise Exception("email address with Google not verified")
   
  email = id_token['email']

  # get the nice name
  http = httplib2.Http()
  http = credentials.authorize(http)
  (resp_headers, content) = http.request("https://people.googleapis.com/v1/people/me?personFields=names", "GET")

  response = utils.from_json(content.decode('utf-8'))

  name = response['names'][0]['displayName']
  
  # watch out, response also contains email addresses, but not sure whether thsoe are verified or not
  # so for email address we will only look at the id_token
  
  return {'type' : 'google', 'user_id': email, 'name': name , 'info': {'email': email}, 'token':{}}
Beispiel #4
0
def get_user_info_after_auth(request):
  tok = request.session['request_token']
  twitter_client = _get_client_by_token(tok)
  access_token = twitter_client.get_access_token()
  request.session['access_token'] = access_token
    
  user_info = utils.from_json(twitter_client.oauth_request('http://api.twitter.com/1/account/verify_credentials.json', args={}, method='GET'))
  
  return {'type': 'twitter', 'user_id' : user_info['screen_name'], 'name': user_info['name'], 'info': user_info, 'token': access_token}
Beispiel #5
0
def user_needs_intervention(user_id, user_info, token):
  """
  check to see if user is following the users we need
  """
  twitter_client = _get_client_by_token(token)
  friendship = utils.from_json(twitter_client.oauth_request('http://api.twitter.com/1/friendships/exists.json', args={'user_a': user_id, 'user_b': USER_TO_FOLLOW}, method='GET'))
  if friendship:
    return None

  return HttpResponseRedirect(reverse(follow_view))
Beispiel #6
0
def get_user_info_after_auth(request):
  args = facebook_get('/oauth/access_token', {
      'client_id' : APP_ID,
      'redirect_uri' : request.session['fb_redirect_uri'],
      'client_secret' : API_SECRET,
      'code' : request.GET['code']
      })

  access_token = cgi.parse_qs(args)['access_token'][0]

  info = utils.from_json(facebook_get('/me', {'access_token':access_token}))

  return {'type': 'facebook', 'user_id' : info['id'], 'name': info['name'], 'email': info['email'], 'info': info, 'token': {'access_token': access_token}}
Beispiel #7
0
def user_needs_intervention(user_id, user_info, token):
    """
    check to see if user is following the users we need
    """
    twitter_client = _get_client_by_token(token)
    friendship = utils.from_json(
        twitter_client.oauth_request(
            "http://api.twitter.com/1/friendships/exists.json",
            args={"user_a": user_id, "user_b": USER_TO_FOLLOW},
            method="GET",
        )
    )
    if friendship:
        return None

    return HttpResponseRedirect(reverse(FOLLOW_VIEW_URL_NAME))
Beispiel #8
0
def user_needs_intervention(user_id, user_info, token):
    """
  check to see if user is following the users we need
  """
    twitter_client = _get_client_by_token(token)
    friendship = utils.from_json(
        twitter_client.oauth_request(
            'http://api.twitter.com/1/friendships/exists.json',
            args={
                'user_a': user_id,
                'user_b': USER_TO_FOLLOW
            },
            method='GET'))
    if friendship:
        return None

    return HttpResponseRedirect(reverse(follow_view))
Beispiel #9
0
def get_user_info_after_auth(request):
    tok = request.session["request_token"]
    twitter_client = _get_client_by_token(tok)
    access_token = twitter_client.get_access_token()
    request.session["access_token"] = access_token

    user_info = utils.from_json(
        twitter_client.oauth_request("http://api.twitter.com/1/account/verify_credentials.json", args={}, method="GET")
    )

    return {
        "type": "twitter",
        "user_id": user_info["screen_name"],
        "name": user_info["name"],
        "info": user_info,
        "token": access_token,
    }
Beispiel #10
0
def get_user_info_after_auth(request):
    tok = request.session['request_token']
    twitter_client = _get_client_by_token(tok)
    access_token = twitter_client.get_access_token()
    request.session['access_token'] = access_token

    user_info = utils.from_json(
        twitter_client.oauth_request(
            'http://api.twitter.com/1/account/verify_credentials.json',
            args={},
            method='GET'))

    return {
        'type': 'twitter',
        'user_id': user_info['screen_name'],
        'name': user_info['name'],
        'info': user_info,
        'token': access_token
    }
Beispiel #11
0
def get_user_info_after_auth(request):
    tok = request.session["request_token"]
    twitter_client = _get_client_by_token(tok)
    access_token = twitter_client.get_access_token()
    request.session["access_token"] = access_token

    user_info = utils.from_json(
        twitter_client.oauth_request(
            "http://api.twitter.com/1/account/verify_credentials.json",
            args={},
            method="GET",
        )
    )

    return {
        "type": "twitter",
        "user_id": user_info["screen_name"],
        "name": user_info["name"],
        "info": user_info,
        "token": access_token,
    }
Beispiel #12
0
def get_user_info_after_auth(request):
    args = live_get(
        "/oauth/access_token",
        {
            "client_id": APP_ID,
            "redirect_uri": request.session["fb_redirect_uri"],
            "client_secret": APP_SECRET,
            "code": request.GET["code"],
        },
    )

    access_token = urllib.parse.parse_qs(args)["access_token"][0]

    info = utils.from_json(live_get("/me", {"access_token": access_token}))

    return {
        "type": "facebook",
        "user_id": info["id"],
        "name": info["name"],
        "info": info,
        "token": {
            "access_token": access_token
        },
    }
Beispiel #13
0
def get_user_groups(user):
  groups_raw = utils.from_json(facebook_get('/me/groups', {'access_token':user.token['access_token']}))
  return groups_raw['data']    
Beispiel #14
0
def get_user_info_after_auth(request):
    redirect_uri = request.session['clever-redirect-url']
    del request.session['clever-redirect-url']
    flow = get_flow(redirect_uri)

    code = request.GET['code']

    # do the POST manually, because OAuth2WebFlow can't do auth header for token exchange
    http = httplib2.Http(".cache")
    auth_header = "Basic %s" % base64.b64encode(settings.CLEVER_CLIENT_ID +
                                                ":" +
                                                settings.CLEVER_CLIENT_SECRET)
    resp_headers, content = http.request(
        "https://clever.com/oauth/tokens",
        "POST",
        urllib.parse.urlencode({
            "code": code,
            "grant_type": "authorization_code",
            "redirect_uri": redirect_uri
        }),
        headers={
            'Authorization': auth_header,
            'Content-Type': "application/x-www-form-urlencoded"
        })

    token_response = utils.from_json(content)
    access_token = token_response['access_token']

    # package the credentials
    credentials = OAuth2Credentials(access_token, settings.CLEVER_CLIENT_ID,
                                    settings.CLEVER_CLIENT_SECRET, None, None,
                                    None, None)

    # get the nice name
    http = credentials.authorize(http)
    (resp_headers, content) = http.request("https://api.clever.com/me", "GET")

    # {"type":"student","data":{"id":"563395179f7408755c0006b7","district":"5633941748c07c0100000aac","type":"student","created":"2015-10-30T16:04:39.262Z","credentials":{"district_password":"******","district_username":"******"},"dob":"1998-11-01T00:00:00.000Z","ell_status":"Y","email":"*****@*****.**","gender":"F","grade":"9","hispanic_ethnicity":"Y","last_modified":"2015-10-30T16:04:39.274Z","location":{"zip":"11433"},"name":{"first":"Diane","last":"Schmeler","middle":"J"},"race":"Asian","school":"5633950c62fc41c041000005","sis_id":"738733110","state_id":"114327752","student_number":"738733110"},"links":[{"rel":"self","uri":"/me"},{"rel":"canonical","uri":"/v1.1/students/563395179f7408755c0006b7"},{"rel":"district","uri":"/v1.1/districts/5633941748c07c0100000aac"}]}
    response = utils.from_json(content)

    user_id = response['data']['id']
    user_name = "%s %s" % (response['data']['name']['first'],
                           response['data']['name']['last'])
    user_type = response['type']
    user_district = response['data']['district']
    user_grade = response['data'].get('grade', None)

    print(content)

    # watch out, response also contains email addresses, but not sure whether thsoe are verified or not
    # so for email address we will only look at the id_token

    return {
        'type': 'clever',
        'user_id': user_id,
        'name': user_name,
        'info': {
            "district": user_district,
            "type": user_type,
            "grade": user_grade
        },
        'token': {
            'access_token': access_token
        }
    }
Beispiel #15
0
def get_user_info_after_auth(request):
    """
  args = facebook_get('/oauth/access_token', {
      'client_id' : APP_ID,
      'redirect_uri' : request.session['fb_redirect_uri'],
      'client_secret' : API_SECRET,
      'code' : request.GET['code']
      })
  """
    logger.debug('get_user_info_after_auth')
    mstring = []
    for key in request.GET.iterkeys():
        valuelist = request.GET.getlist(key)
        mstring.extend(['%s=%s' % (key, val) for val in valuelist])
        logger.debug('*' + key + '*=*' + val + '*')
    msg = ','.join(mstring)
    logger.debug(msg)
    try:
        logger.debug('clienttype: ' + request.GET['client_type'])
        if request.GET['client_type'] == 'androidnfcapp':
            return get_user_info_after_auth_androidClient(request)
    except Exception:
        pass
    logger.info('ahora el dni_post_step2')
    args = dnie_post_step2(
        request, '/api/v1/tokens/', {
            'grant_type': 'authorization_code',
            'code': request.GET['code'],
            'client_id': 'testclient',
            'client_secret': 'testpassword'
        })
    logger.info(args)
    #     'client_id' : '1',
    #     #'client_id' : APP_ID,
    #     'redirect_uri' : request.session['dnie_redirect_uri'],
    #     #'client_secret' : API_SECRET,
    #     'client_secret' : 'ddd',
    #     'code' : request.GET['code']
    #     })

    #access_token = cgi.parse_qs(args)['access_token'][0]

    from helios_auth import utils
    access_token_req = utils.from_json(args)
    access_token = access_token_req['access_token']

    info = utils.from_json(
        dnie_post_step2(request, '/web/me', {'access_token': access_token}))
    #info = {'user_id': '53159931P'}

    # Lo ideal es sacar la info del web/me, pero temporalmente vamos a hacer un workaround
    # Tenemos el DNIe, asi que miramos en la BD si existe algun Usuario con ese DNIe
    reqdnie = request.GET['dnie']
    voters = Voter.get_by_voter_id(reqdnie)
    if (len(voters) > 0):
        voter = voters[0]
        info = {
            'id': reqdnie,
            'name': voter.voter_name,
            'email': voter.voter_email
        }

    #return {'type': 'facebook', 'user_id' : info['id'], 'name': info.get('name'), 'email': info.get('email'), 'info': info, 'token': {'access_token': access_token}}
    return {
        'type': 'dnie',
        'user_id': info['id'],
        'name': info.get('name'),
        'email': info.get('email'),
        'info': info,
        'token': {
            'access_token': access_token
        }
    }
Beispiel #16
0
def get_user_groups(user):
  groups_raw = utils.from_json(facebook_get('/me/groups', {'access_token':user.token['access_token']}))
  return groups_raw['data']