Exemple #1
0
def login(request, service=None):

    #   Check for errors
    #+++++++++++++++++++
    if request.GET.get('error'):
        return 'Access Denied'

    auth = Authentication(request)
    if auth.is_logged_in:
        return redirect('www:profile')

    if None == service:
        return redirect('www:index')

    request.session['redirect'] = 'www:profile'

    try:
        return auth.login_step_1(
            service,
            request.build_absolute_uri(
                reverse('www:oauth', kwargs={'service': service})))

    except FIRSTAuthError as e:
        return HttpResponse(('Error: {}<br /><a href="/#login">Try logging '
                             'in again</a>').format(e))
Exemple #2
0
def register(request):
    '''
    Required: handle
    Get name and email from sign in service
    '''

    #   Check for errors
    #+++++++++++++++++++
    if 'error' in request.GET:
        return HttpResponse('Access Denied')

    auth = Authentication(request)
    if request.method == 'POST':
        if not request.POST.get('service') or not request.POST.get('handle'):
            return redirect('www:index', _anchor='registration')

        #   TODO: Input Validation
        request.session['redirect'] = 'www:register'
        if not re.match('^[A-Za-z_\d]+$', request.POST.get('handle')):
            return HttpResponse('Invalid handle')

        request.session['handle'] = request.POST.get('handle')
        service = request.POST.get('service')

        try:
            return auth.login_step_1(
                service, reverse('www:oauth', kwargs={'service': service}))

        except FIRSTAuthError as e:
            return HttpResponse(
                ('Error: {}<br /><a href="/#login">Try logging '
                 'in again</a>').format(e))

        return HttpResponse('No post data provided')

    if request.method == 'GET':
        if auth.is_logged_in:
            if ('info' not in request.session
                    or 'email' not in request.session['info']):
                raise FIRSTAuthError('Email not set')

            user = Authentication.get_user_data(
                request.session['info']['email'])
            if not user:
                if 'handle' in request.session:
                    user = auth.register_user()
                    if not user:
                        return HttpResponse('Error creating user')

                return redirect(reverse('www:profile'))

            return HttpResponse('User already exists')

        return HttpResponse('Not logged in')