本文整理汇总了Python中sentry.utils.auth.login函数的典型用法代码示例。如果您正苦于以下问题:Python login函数的具体用法?Python login怎么用?Python login使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了login函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _handle_existing_identity
def _handle_existing_identity(self, auth_identity, identity):
# TODO(dcramer): this is very similar to attach
now = timezone.now()
auth_identity.update(
data=self.provider.update_identity(
new_data=identity.get('data', {}),
current_data=auth_identity.data,
),
last_verified=now,
last_synced=now,
)
try:
member = OrganizationMember.objects.get(
user=auth_identity.user,
organization=self.organization,
)
except OrganizationMember.DoesNotExist:
# this is likely the case when someone was removed from the org
# but still has access to rejoin
member = self._handle_new_membership(auth_identity)
else:
if getattr(member.flags, 'sso:invalid') or not getattr(member.flags, 'sso:linked'):
setattr(member.flags, 'sso:invalid', False)
setattr(member.flags, 'sso:linked', True)
member.save()
user = auth_identity.user
user.backend = settings.AUTHENTICATION_BACKENDS[0]
auth.login(self.request, user)
self.clear_session()
return HttpResponseRedirect(auth.get_login_redirect(self.request))
示例2: handle_basic_auth
def handle_basic_auth(self, request, organization):
can_register = features.has('auth:register') or request.session.get('can_register')
op = request.POST.get('op')
login_form = self.get_login_form(request)
if can_register:
register_form = self.get_register_form(request)
else:
register_form = None
if can_register and register_form.is_valid():
user = register_form.save()
defaults = {
'role': 'member',
}
organization.member_set.create(
user=user,
**defaults
)
# HACK: grab whatever the first backend is and assume it works
user.backend = settings.AUTHENTICATION_BACKENDS[0]
auth.login(request, user)
# can_register should only allow a single registration
request.session.pop('can_register', None)
request.session.pop('needs_captcha', None)
return self.redirect(auth.get_login_redirect(request))
elif login_form.is_valid():
auth.login(request, login_form.get_user())
request.session.pop('needs_captcha', None)
return self.redirect(auth.get_login_redirect(request))
elif request.POST and not request.session.get('needs_captcha'):
auth.log_auth_failure(request, request.POST.get('username'))
request.session['needs_captcha'] = 1
login_form = self.get_login_form(request)
login_form.errors.pop('captcha', None)
if can_register:
register_form = self.get_register_form(request)
register_form.errors.pop('captcha', None)
request.session.set_test_cookie()
context = {
'op': op or 'login',
'login_form': login_form,
'register_form': register_form,
'organization': organization,
'CAN_REGISTER': can_register,
}
return self.respond('sentry/organization-login.html', context)
示例3: post
def post(self, request):
"""
Authenticate a User
```````````````````
This endpoint authenticates a user using the provided credentials
through a regular HTTP basic auth system. The response contains
cookies that need to be sent with further requests that require
authentication.
This is primarily used internally in Sentry.
Common example::
curl -X ###METHOD### -u username:password ###URL###
"""
if not request.user.is_authenticated():
return Response(status=400)
# If 2fa login is enabled then we cannot sign in with username and
# password through this api endpoint.
if Authenticator.objects.user_has_2fa(request.user):
return Response({
'2fa_required': True,
'message': 'Cannot sign-in with basic auth when 2fa is enabled.'
}, status=400)
# Must use the real request object that Django knows about
auth.login(request._request, request.user)
return self.get(request)
示例4: _handle_unknown_identity
def _handle_unknown_identity(self, identity):
"""
Flow is activated upon a user logging in to where an AuthIdentity is
not present.
The flow will attempt to answer the following:
- Is there an existing user with the same email address? Should they be
merged?
- Is there an existing user (via authentication) that shoudl be merged?
- Should I create a new user based on this identity?
"""
request = self.request
op = request.POST.get('op')
if not request.user.is_authenticated():
try:
existing_user = auth.find_users(identity['email'])[0]
except IndexError:
existing_user = None
login_form = self._get_login_form(existing_user)
if op == 'confirm' and request.user.is_authenticated():
auth_identity = self._handle_attach_identity(identity)
elif op == 'newuser':
auth_identity = self._handle_new_user(identity)
elif op == 'login' and not request.user.is_authenticated():
# confirm authentication, login
op = None
if login_form.is_valid():
auth.login(request, login_form.get_user())
request.session.pop('needs_captcha', None)
else:
auth.log_auth_failure(request, request.POST.get('username'))
request.session['needs_captcha'] = 1
else:
op = None
if not op:
if request.user.is_authenticated():
return self.respond('sentry/auth-confirm-link.html', {
'identity': identity,
'existing_user': request.user,
})
return self.respond('sentry/auth-confirm-identity.html', {
'existing_user': existing_user,
'identity': identity,
'login_form': login_form,
})
user = auth_identity.user
user.backend = settings.AUTHENTICATION_BACKENDS[0]
auth.login(self.request, user)
self.clear_session()
return HttpResponseRedirect(auth.get_login_redirect(self.request))
示例5: perform_signin
def perform_signin(self, request, user, interface=None):
auth.login(request, user, passed_2fa=True)
rv = HttpResponseRedirect(auth.get_login_redirect(request))
if interface is not None:
interface.authenticator.mark_used()
if not interface.is_backup_interface:
rv.set_cookie(COOKIE_NAME, str(interface.type),
max_age=COOKIE_MAX_AGE, path='/')
return rv
示例6: put
def put(self, request):
"""
Verify a User
`````````````
This endpoint verifies the currently authenticated user (for example, to gain superuser).
:auth: required
"""
if not request.user.is_authenticated():
return Response(status=status.HTTP_401_UNAUTHORIZED)
validator = AuthVerifyValidator(data=request.DATA)
if not validator.is_valid():
return self.respond(validator.errors, status=status.HTTP_400_BAD_REQUEST)
authenticated = False
# See if we have a u2f challenge/response
if 'challenge' in validator.object and 'response' in validator.object:
try:
interface = Authenticator.objects.get_interface(request.user, 'u2f')
if not interface.is_enrolled:
raise LookupError()
challenge = json.loads(validator.object['challenge'])
response = json.loads(validator.object['response'])
authenticated = interface.validate_response(request, challenge, response)
except ValueError:
pass
except LookupError:
pass
# attempt password authentication
else:
authenticated = request.user.check_password(validator.object['password'])
# UI treats 401s by redirecting, this 401 should be ignored
if not authenticated:
return Response({'detail': {'code': 'ignore'}}, status=status.HTTP_403_FORBIDDEN)
try:
# Must use the real request object that Django knows about
auth.login(request._request, request.user)
except auth.AuthUserPasswordExpired:
return Response(
{
'code': 'password-expired',
'message': 'Cannot sign-in with basic auth because password has expired.',
},
status=status.HTTP_403_FORBIDDEN
)
request.user = request._request.user
return self.get(request)
示例7: handle_basic_auth
def handle_basic_auth(self, request):
can_register = features.has('auth:register') or request.session.get('can_register')
op = request.POST.get('op')
# Detect that we are on the register page by url /register/ and
# then activate the register tab by default.
if not op and '/register' in request.path_info and can_register:
op = 'register'
login_form = self.get_login_form(request)
if can_register:
register_form = self.get_register_form(request)
else:
register_form = None
if can_register and register_form.is_valid():
user = register_form.save()
# HACK: grab whatever the first backend is and assume it works
user.backend = settings.AUTHENTICATION_BACKENDS[0]
auth.login(request, user)
# can_register should only allow a single registration
request.session.pop('can_register', None)
request.session.pop('needs_captcha', None)
return self.redirect(auth.get_login_redirect(request))
elif login_form.is_valid():
auth.login(request, login_form.get_user())
request.session.pop('needs_captcha', None)
return self.redirect(auth.get_login_redirect(request))
elif request.POST and not request.session.get('needs_captcha'):
auth.log_auth_failure(request, request.POST.get('username'))
request.session['needs_captcha'] = 1
login_form = self.get_login_form(request)
login_form.errors.pop('captcha', None)
if can_register:
register_form = self.get_register_form(request)
register_form.errors.pop('captcha', None)
request.session.set_test_cookie()
context = {
'op': op or 'login',
'login_form': login_form,
'register_form': register_form,
'CAN_REGISTER': can_register,
}
return self.respond('sentry/login.html', context)
示例8: handle_basic_auth
def handle_basic_auth(self, request):
can_register = features.has('auth:register') or request.session.get('can_register')
op = request.POST.get('op')
# Detect that we are on the register page by url /register/ and
# then activate the register tab by default.
if not op and '/register' in request.path_info and can_register:
op = 'register'
login_form = self.get_login_form(request)
if can_register:
register_form = self.get_register_form(request, initial={
'username': request.session.get('invite_email', '')
})
else:
register_form = None
if can_register and register_form.is_valid():
user = register_form.save()
user.send_confirm_emails(is_new_user=True)
# HACK: grab whatever the first backend is and assume it works
user.backend = settings.AUTHENTICATION_BACKENDS[0]
auth.login(request, user)
# can_register should only allow a single registration
request.session.pop('can_register', None)
request.session.pop('invite_email', None)
return self.redirect(auth.get_login_redirect(request))
elif login_form.is_valid():
user = login_form.get_user()
auth.login(request, user)
if not user.is_active:
return self.redirect(reverse('sentry-reactivate-account'))
return self.redirect(auth.get_login_redirect(request))
request.session.set_test_cookie()
context = {
'op': op or 'login',
'server_hostname': get_server_hostname(),
'login_form': login_form,
'register_form': register_form,
'CAN_REGISTER': can_register,
}
return self.respond('sentry/login.html', context)
示例9: handle_basic_auth
def handle_basic_auth(self, request):
can_register = features.has("auth:register") or request.session.get("can_register")
op = request.POST.get("op")
# Detect that we are on the register page by url /register/ and
# then activate the register tab by default.
if not op and "/register" in request.path_info and can_register:
op = "register"
login_form = self.get_login_form(request)
if can_register:
register_form = self.get_register_form(
request, initial={"username": request.session.get("invite_email", "")}
)
else:
register_form = None
if can_register and register_form.is_valid():
user = register_form.save()
user.send_confirm_emails(is_new_user=True)
# HACK: grab whatever the first backend is and assume it works
user.backend = settings.AUTHENTICATION_BACKENDS[0]
auth.login(request, user)
# can_register should only allow a single registration
request.session.pop("can_register", None)
request.session.pop("invite_email", None)
return self.redirect(auth.get_login_redirect(request))
elif login_form.is_valid():
user = login_form.get_user()
auth.login(request, user)
if not user.is_active:
return self.redirect(reverse("sentry-reactivate-account"))
return self.redirect(auth.get_login_redirect(request))
context = {
"op": op or "login",
"server_hostname": get_server_hostname(),
"login_form": login_form,
"register_form": register_form,
"CAN_REGISTER": can_register,
}
return self.respond("sentry/login.html", context)
示例10: handle_basic_auth
def handle_basic_auth(self, request, organization):
can_register = auth.has_user_registration() or request.session.get('can_register')
op = request.POST.get('op')
login_form = self.get_login_form(request)
if can_register:
register_form = self.get_register_form(request)
else:
register_form = None
if can_register and register_form.is_valid():
user = register_form.save()
user.send_confirm_emails(is_new_user=True)
defaults = {
'role': 'member',
}
organization.member_set.create(
user=user,
**defaults
)
# HACK: grab whatever the first backend is and assume it works
user.backend = settings.AUTHENTICATION_BACKENDS[0]
auth.login(request, user, organization_id=organization.id)
# can_register should only allow a single registration
request.session.pop('can_register', None)
return self.redirect(auth.get_login_redirect(request))
elif login_form.is_valid():
auth.login(request, login_form.get_user(),
organization_id=organization.id)
return self.redirect(auth.get_login_redirect(request))
request.session.set_test_cookie()
context = {
'op': op or 'login',
'login_form': login_form,
'register_form': register_form,
'organization': organization,
'CAN_REGISTER': can_register,
}
return self.respond('sentry/organization-login.html', context)
示例11: test_process_request_user
def test_process_request_user(self):
request = self.request
assert login(request, self.user)
self.middleware.process_request(request)
assert request.user.is_authenticated()
assert request.user == self.user
assert '_nonce' not in request.session
示例12: test_with_nonce
def test_with_nonce(self):
self.user.refresh_session_nonce()
self.user.save()
assert self.user.session_nonce is not None
request = self.make_request()
assert login(request, self.user)
assert request.user == self.user
assert request.session['_nonce'] == self.user.session_nonce
示例13: test_process_request_bad_nonce
def test_process_request_bad_nonce(self):
request = self.request
user = self.user
user.session_nonce = 'xxx'
user.save()
assert login(request, user)
request.session['_nonce'] = 'gtfo'
self.middleware.process_request(request)
assert request.user.is_anonymous()
示例14: test_process_request_good_nonce
def test_process_request_good_nonce(self):
request = self.request
user = self.user
user.session_nonce = 'xxx'
user.save()
assert login(request, user)
self.middleware.process_request(request)
assert request.user.is_authenticated()
assert request.user == self.user
assert request.session['_nonce'] == 'xxx'
示例15: test_process_request_user
def test_process_request_user(self):
request = self.request
assert login(request, self.user)
self.middleware.process_request(request)
assert request.user.is_authenticated()
assert request.user == self.user
assert '_nonce' not in request.session
assert UserIP.objects.filter(
user=self.user,
ip_address='127.0.0.1',
).exists()