Esempio n. 1
0
 def authenticate(self, token=None, google_id=None):
     try:
         if google_id is not None and validate_token(token, google_id):
             return User.objects.get(profile__google_id=google_id)
         profile = get_google_profile(token)
         google_id = profile['id']
         return User.objects.get(profile__google_id=google_id)
     except User.DoesNotExist:
         return None
Esempio n. 2
0
def oauth_callback(request):
    invite = request.session.get("req")
    if request.method == 'GET':
        code = request.GET.get('code',None)
        if 'google_access_token' in request.session:
            del request.session['google_access_token']
    
        if code is not None:
            request.session['google_oauth_code'] = code
            data =  get_token_from_code(code)
            request.session['google_access_token'] = data['access_token']
            user = authenticate(token=request.session['google_access_token'])
            if user is None:
                profile = get_google_profile(request.session['google_access_token'])
                #if an existing user exists with this email address, update googlie id
                try:
                    user = User.objects.get(email=profile['email'])
                    up = Userprofile.objects.get(user=user)
                    up.google_id = profile["id"]
                    up.save()
                    user = USTLogin(request, token=request.session['google_access_token'], google_id=profile["id"])                    
                    if user:
                        return HttpResponseRedirect("/user")
                except User.DoesNotExist:
                    pass

                form = NewScoutForm({'first_name':profile["given_name"],
                                     'last_name':profile["family_name"],
                                     'nickname':profile["given_name"],
                                     'login_name':profile["email"],
                                     'bd_string':profile.get('birthday',''),
                                     'email':profile["email"],
                                     'google_id':profile["id"],
                                     'position':7,
                                     })
                if form.is_valid():
                    up = form.save()
                    user = USTLogin(request, token=request.session['google_access_token'], google_id=profile["id"])                    
                    return HttpResponseRedirect("/")
                    
                    
                else:
                    return 'oauth.html', {'form':form, 'profile':True}
            else:
                login(request, user)
                return HttpResponseRedirect("/user")
        else:
            print 'in here four'
            form = NewScoutForm(initial={
                                         'position':7,#Guardian
                                         })
            return 'oauth.html', {'form':form}  
        
    elif request.method == 'POST':
        invite = request.session.get("req")

        scoutform = NewScoutForm(request.POST)
       
        if scoutform.is_valid():
            profile = scoutform.save()
                    
            if len(profile.google_id) > 0:
                user = USTLogin(request, token=request.session['google_access_token'], google_id=profile.google_id)                    
 
                #user = authenticate(token=request.session['google_access_token'], google_id=profile.google_id)
            else:
                user = USTLogin(request, username=profile.user.username, password=scoutform.cleaned_data['password'])
                #user = authenticate(username=profile.user.username, password=scoutform.cleaned_data['password'])
            
            return HttpResponseRedirect("/")
        else:
            print 'form errors: %s'%scoutform.errors
            return 'oauth.html', {'form':scoutform}