当前位置: 首页>>代码示例>>Python>>正文


Python TASClient.authenticate方法代码示例

本文整理汇总了Python中pytas.http.TASClient.authenticate方法的典型用法代码示例。如果您正苦于以下问题:Python TASClient.authenticate方法的具体用法?Python TASClient.authenticate怎么用?Python TASClient.authenticate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pytas.http.TASClient的用法示例。


在下文中一共展示了TASClient.authenticate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TASBackend

# 需要导入模块: from pytas.http import TASClient [as 别名]
# 或者: from pytas.http.TASClient import authenticate [as 别名]
class TASBackend(ModelBackend):

    logger = logging.getLogger(__name__)

    def __init__(self):
        self.tas = TASClient()

    # Create an authentication method
    # This is called by the standard Django login procedure
    def authenticate(self, username=None, password=None, request=None, **kwargs):
        user = None
        if username is not None and password is not None:
            tas_user = None
            if request is not None:
                self.logger.info('Attempting login via TAS for user "%s" from IP "%s"' % (username, request.META.get('REMOTE_ADDR')))
            else:
                self.logger.info('Attempting login via TAS for user "%s" from IP "%s"' % (username, 'unknown'))
            try:
                # Check if this user is valid on the mail server
                if self.tas.authenticate(username, password):
                    tas_user = self.tas.get_user(username=username)
                    self.logger.info('Login successful for user "%s"' % username)
                else:
                    raise ValidationError('Authentication Error', 'Your username or password is incorrect.')
            except Exception as e:
                self.logger.warning(e.args)
                if re.search(r'PendingEmailConfirmation', e.args[1]):
                    raise ValidationError('Please confirm your email address before logging in.')
                else:
                    raise ValidationError(e.args[1])

            if tas_user is not None:
                UserModel = get_user_model()
                try:
                    # Check if the user exists in Django's local database
                    user = UserModel.objects.get(username=username)
                    user.first_name = tas_user['firstName']
                    user.last_name = tas_user['lastName']
                    user.email = tas_user['email']
                    user.save()

                except UserModel.DoesNotExist:
                    # Create a user in Django's local database
                    self.logger.info('Creating local user record for "%s" from TAS Profile' % username)
                    user = UserModel.objects.create_user(
                        username=username,
                        first_name=tas_user['firstName'],
                        last_name=tas_user['lastName'],
                        email=tas_user['email']
                        )

                try:
                    profile = DesignSafeProfile.objects.get(user=user)
                except DesignSafeProfile.DoesNotExist:
                    profile = DesignSafeProfile(user=user)
                    profile.save()

        return user
开发者ID:DesignSafe-CI,项目名称:portal,代码行数:60,代码来源:backends.py

示例2: clean

# 需要导入模块: from pytas.http import TASClient [as 别名]
# 或者: from pytas.http.TASClient import authenticate [as 别名]
 def clean(self):
     cleaned_data = self.cleaned_data
     reset_link = reverse('designsafe_accounts:password_reset')
     tas = TASClient()
     current_password_correct = tas.authenticate(self._username,
                                                 cleaned_data['current_password'])
     if current_password_correct:
         tas_user = tas.get_user(username=self._username)
         pw = cleaned_data['new_password']
         confirm_pw = cleaned_data['confirm_new_password']
         valid, error_message = check_password_policy(tas_user, pw, confirm_pw)
         if not valid:
             self.add_error('new_password', error_message)
             self.add_error('confirm_new_password', error_message)
             raise forms.ValidationError(error_message)
     else:
         err_msg = mark_safe(
             'The current password you provided is incorrect. Please try again. '
             'If you do not remember your current password you can '
             '<a href="%s" tabindex="-1">reset your password</a> with an email '
             'confirmation.' % reset_link)
         self.add_error('current_password', err_msg)
开发者ID:DesignSafe-CI,项目名称:portal,代码行数:24,代码来源:forms.py


注:本文中的pytas.http.TASClient.authenticate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。