def create(request): try: form = forms.CreateForm(request.POST) data = common.validate_form( form, ('username', 'email', 'openid', 'password')) try: user = models.User.objects.create_user(data['username'], data['email'], data['password']) except db.IntegrityError: raise common.DuplicateUserError(username=data['username']) models.Auth.objects.create(openid_url=data['openid'], user=user) try: send_mail(CREATE_SUBJECT, '', settings.ADMIN_EMAIL, [user.email], html_message=CREATE_MESSAGE.format( login_url=settings.LOGIN_URL, admin_email=settings.ADMIN_EMAIL)) except Exception: logger.exception('Error sending confirmation email') pass except WPSError as e: logger.exception('Error creating account') return common.failed(str(e)) else: return common.success('Successfully created account for "{}"'.format( data['username']))
def user_login(request): try: form = forms.LoginForm(request.POST) data = common.validate_form(form, ('username', 'password')) logger.info('Attempting to login user {}'.format(data['username'])) user = authenticate(request, username=data['username'], password=data['password']) if user is None: raise common.AuthenticationError(user=data['username']) login(request, user) except WPSError as e: logger.exception('Error logging user in') return common.failed(str(e)) else: data = common.user_to_json(user) data['expires'] = request.session.get_expiry_date() return common.success(data)
def login_mpc(request): try: common.authentication_required(request) form = forms.MPCForm(request.POST) data = common.validate_form(form, ('username', 'password')) logger.info('Authenticating MyProxyClient for {}'.format( data['username'])) services = openid.services(request.user.auth.openid_url, (URN_MPC, )) g = re.match('socket://(.*):(.*)', services[0].server_url) if g is None or len(g.groups()) != 2: raise MPCEndpointParseError() host, port = g.groups() from OpenSSL import SSL MyProxyClient.SSL_METHOD = SSL.TLSv1_2_METHOD try: m = MyProxyClient(hostname=host, caCertDir=settings.WPS_CA_PATH) c = m.logon(data['username'], data['password'], bootstrap=True) except Exception as e: raise common.AuthenticationError(user=data['username']) logger.info( 'Authenticated with MyProxyClient backend for user {}'.format( data['username'])) request.user.auth.update('myproxyclient', c) except WPSError as e: logger.exception('Error authenticating MyProxyClient') return common.failed(str(e)) else: metrics.track_login(metrics.WPS_MPC_LOGIN_SUCCESS, request.user.auth.openid_url) return common.success({ 'type': request.user.auth.type, 'api_key': request.user.auth.api_key }) finally: if not request.user.is_anonymous: metrics.track_login(metrics.WPS_MPC_LOGIN, request.user.auth.openid_url)
def user_login_openid(request): try: form = forms.OpenIDForm(request.POST) data = common.validate_form(form, ('openid_url', )) url = openid.begin(request, data['openid_url']) except WPSError as e: logger.exception('Error logging user in with OpenID') return common.failed(str(e)) else: return common.success({'redirect': url})
def user_login_openid(request): try: form = forms.OpenIDForm(request.POST) data = common.validate_form(form, ('openid_url', 'next')) url = openid.begin(request, **data) except WPSError as e: logger.exception('Error logging user in with OpenID') return common.failed(str(e)) else: return common.success({'redirect': url}) finally: if 'openid_url' in request.POST: metrics.track_login(metrics.WPS_OPENID_LOGIN, request.POST['openid_url'])