我创建了一个表,用于存储必须通过多个第三方SSO身份验证框架管理身份验证的程序的会话环境。
我要做的是在我的UserEnvironemntSession
对象中创建一个新方法,以便我可以这样做:
from authentication import UserEnvironementSession, User, Environement
#user and environement will be configured as part of a session somewhere and
#This is just an example
user = User.objects.get(username="username")
environment = User.objects.get(pk=2)
ues = UserEnvironmentSession.objects.get(user=user, environment=environement)
user_session = ues.create_user_sso_session()
以上代码将查找给定用户/会话对象的会话,然后为该用户创建新的经过身份验证的会话。
我所做的是在我的UserEnvironementSession
课程中创建了一个新方法,如下所示
class UserEnvironmentSession(models.Model):
user = models.ForeignKey("User", related_name = "userenvsession_user", on_delete=models.CASCADE)
environment = models.ForeignKey("Environment", related_name = "userenvsession_environement", on_delete=models.CASCADE)
session_token = models.CharField(max_length=200)
def create_user_sso_session(self):
'''
Generates a new user object from the UserEnvironmentSession.
It will attempt to set the User.token and authenticate. If the auth token is expired.
It will throw an error that can be used to initiate a new login process.
'''
#Set up our SSO authentication
auth = SSO.authentication.SSOAuthentication()
auth.domain = self.environment.domain
auth.auth_header = self.environment.header
#configure our user with our authenticator and session token
self.user.authenticator = auth
self.user.token = self.session_token
if self.user.is_authenticated:
#If token is vaild is_authenticated will return true. We need to set up a user session with
# our session_token header and set it to our created user object.
import requests
user.session = requests.session()
user.session.headers[self.environment.header] = self.session_token
else:
#If the token is invalid we throw an SSO.errors.LoginFailureError
raise SSO.errors.LoginFailureError("The token for this UserSession has expired, so we need to login again.")
我认为上面的内容会有效,但这会让我产生以下错误
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/rchristensen1/Development/Python/virtualenv/cps/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/Users/rchristensen1/Development/Python/virtualenv/cps/lib/python2.7/site-packages/django/core/management/__init__.py", line 327, in execute
django.setup()
File "/Users/rchristensen1/Development/Python/virtualenv/cps/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/rchristensen1/Development/Python/virtualenv/cps/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/rchristensen1/Development/Python/virtualenv/cps/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/rchristensen1/Development/Python/course_producer_suite/authentication/models.py", line 27, in <module>
class UserEnvironmentSession(models.Model):
File "/Users/rchristensen1/Development/Python/course_producer_suite/authentication/models.py", line 41, in UserEnvironmentSession
auth.domain = self.environment.domain
NameError: name 'self' is not defined