コード例 #1
0
def _get_user_or_none(request):
    user_id = request.session.get(USER_SESSION_ID)
    Model = get_model()
    try:
        return Model.objects.get(id=user_id)
    except Model.DoesNotExist:
        return None
コード例 #2
0
def oauth2callback(request):
    t = request.session.get(TEMP_USER_SESSION_ID)
    if not t:
        return redirect_to_login_page()

    state = request.REQUEST['state']
    if not xsrfutil.validate_token(settings.SECRET_KEY, state, t):
        return HttpResponseBadRequest()

    try:
        credential = _get_flow(request).step2_exchange(request.REQUEST)
    except FlowExchangeError:
        return HttpResponseRedirect(reverse(login))

    if credential.invalid:
        return HttpResponseRedirect(reverse(login))

    http = httplib2.Http()
    http = credential.authorize(http)
    _h, c = http.request('https://www.googleapis.com/oauth2/v1/userinfo')
    data = simplejson.loads(c)

    user, _created = get_model().objects.get_or_create(
        email=data['email'],
        defaults=dict(
            name=data.get('name') or data['email'].split('@')[0],
        )
    )
    user.name = data.get('given_name') or user.name
    user.surname = data.get('family_name', '')

    try:
        user.save()
    except ValidationError, e:
        return redirect_to_login_page(e.messages[0])