Пример #1
0
    def perform_create(self, serializer):
        user = serializer.save(self.request)
        if getattr(settings, 'REST_USE_JWT', False):
            self.token = jwt_encode(user)
        else:
            create_token(self.token_model, user, serializer)

        complete_signup(self.request._request, user,
                        allauth_settings.EMAIL_VERIFICATION, None)
        return user
Пример #2
0
    def perform_create(self, serializer):
        user = serializer.save(self.request)
        if allauth_settings.EMAIL_VERIFICATION != \
                allauth_settings.EmailVerificationMethod.MANDATORY:
            if getattr(settings, 'REST_USE_JWT', False):
                self.access_token, self.refresh_token = jwt_encode(user)
            else:
                create_token(self.token_model, user, serializer)

        complete_signup(self.request._request, user,
                        allauth_settings.EMAIL_VERIFICATION, None)
        return user
Пример #3
0
 def create(self, request):
     data = request.data
     serializer = self.get_serializer(data=data)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     self.user = User.objects.get(username=serializer.data.get('username'))
     if getattr(settings, 'REST_USE_JWT', False):
         self.access_token, self.refresh_token = jwt_encode(self.user)
     else:
         self.token = create_token(self.token_model, self.user,
                                   self.serializer)
     data = login.get_response(self).data
     return Response(data=data, status=status.HTTP_201_CREATED)
Пример #4
0
 def perform_create(self, serializer):
     user = serializer.save(self.request)
     if (allauth_settings.EMAIL_VERIFICATION !=
             allauth_settings.EmailVerificationMethod.MANDATORY):
         if getattr(settings, "REST_USE_JWT", False):
             self.access_token, self.refresh_token = jwt_encode(user)
         else:
             create_token(self.token_model, user, serializer)
     email_address = EmailAddress.objects.get(user=user, email=user.email)
     confirmation_key = EmailConfirmationHMAC(email_address).key
     # TODO Send Email here -> "account-confirm-email/" + confirmation_key
     send_regestration_mail.delay(user.username, user.email,
                                  confirmation_key)
     return user
Пример #5
0
    def perform_create(self, serializer):
        user = serializer.save(self.request)
        if allauth_settings.EMAIL_VERIFICATION != \
                allauth_settings.EmailVerificationMethod.MANDATORY:
            if getattr(settings, 'REST_USE_JWT', False):
                self.access_token, self.refresh_token = jwt_encode(user)
            elif not getattr(settings, 'REST_SESSION_LOGIN', False):
                # Session authentication isn't active either, so this has to be
                #  token authentication
                create_token(self.token_model, user, serializer)

        complete_signup(
            self.request._request,
            user,
            allauth_settings.EMAIL_VERIFICATION,
            None,
        )
        return user
Пример #6
0
def acs(r):
    saml_client = _get_saml_client(get_current_domain(r))
    resp = r.POST.get('SAMLResponse', None)
    next_url = r.session.get('login_next_url', _default_next_url())

    if not resp:
        return HttpResponseRedirect(
            get_reverse([denied, 'denied', 'django_saml2_auth:denied']))

    authn_response = saml_client.parse_authn_request_response(
        resp, entity.BINDING_HTTP_POST)
    if authn_response is None:
        return HttpResponseRedirect(
            get_reverse([denied, 'denied', 'django_saml2_auth:denied']))

    user_identity = authn_response.get_identity()
    if user_identity is None:
        return HttpResponseRedirect(
            get_reverse([denied, 'denied', 'django_saml2_auth:denied']))

    user_email = user_identity[settings.SAML2_AUTH.get(
        'ATTRIBUTES_MAP', {}).get('email', 'Email')][0]
    user_name = user_identity[settings.SAML2_AUTH.get(
        'ATTRIBUTES_MAP', {}).get('username', 'UserName')][0]
    user_first_name = user_identity[settings.SAML2_AUTH.get(
        'ATTRIBUTES_MAP', {}).get('first_name', 'FirstName')][0]
    user_last_name = user_identity[settings.SAML2_AUTH.get(
        'ATTRIBUTES_MAP', {}).get('last_name', 'LastName')][0]

    target_user = None
    is_new_user = False

    try:
        target_user = User.objects.get(email=user_email)
        if settings.SAML2_AUTH.get('TRIGGER', {}).get('BEFORE_LOGIN', None):
            import_string(
                settings.SAML2_AUTH['TRIGGER']['BEFORE_LOGIN'])(user_identity)
    except User.DoesNotExist:
        new_user_should_be_created = settings.SAML2_AUTH.get(
            'CREATE_USER', True)
        if new_user_should_be_created:
            target_user = _create_new_user(user_name, user_email,
                                           user_first_name, user_last_name)
            if settings.SAML2_AUTH.get('TRIGGER', {}).get('CREATE_USER', None):
                import_string(settings.SAML2_AUTH['TRIGGER']['CREATE_USER'])(
                    user_identity)
            is_new_user = True
        else:
            return HttpResponseRedirect(
                get_reverse([denied, 'denied', 'django_saml2_auth:denied']))

    r.session.flush()

    if target_user.is_active:
        target_user.backend = 'django.contrib.auth.backends.ModelBackend'
        get_adapter(r).login(r, target_user)
    else:
        return HttpResponseRedirect(
            get_reverse([denied, 'denied', 'django_saml2_auth:denied']))

    if settings.SAML2_AUTH.get('USE_JWT') is True:
        # We use JWT auth send token to frontend
        jwt_token = jwt_encode(target_user)
        query = '?uid={}&token={}'.format(target_user.id, jwt_token)

        frontend_url = settings.SAML2_AUTH.get('FRONTEND_URL', next_url)

        return HttpResponseRedirect(frontend_url + query)

    if is_new_user:
        try:
            return render(r, 'django_saml2_auth/welcome.html',
                          {'user': r.user})
        except TemplateDoesNotExist:
            return HttpResponseRedirect(next_url)
    else:
        return HttpResponseRedirect(next_url)