Changeset 9:41f264620073 in OKFNAnnotator (for Zope)
- Timestamp:
- Feb 12, 2015, 6:46:55 PM (10 years ago)
- Branch:
- default
- Tags:
- tip
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
AuthTokenGenerator.py
r8 r9 45 45 def index_html(self, user='anonymous'): 46 46 """returns authentication token for user (Zope style)""" 47 if self._user_allowed(user=user): 48 token = self._generate_token(user) 47 zUser = self._allowed_user(user=user) 48 logging.debug("allowed user: %s"%repr(zUser)) 49 if zUser: 50 token = self._generate_token(zUser) 49 51 # set CORS headers 50 52 origin = self.REQUEST.getHeader("Origin", None) … … 71 73 72 74 self.REQUEST.RESPONSE.setHeader("Access-Control-Allow-Credentials", "true") 73 if self._user_allowed(user=user, password=password): 74 token = self._generate_token(user) 75 zUser = self._allowed_user(user=user, password=password) 76 logging.debug("allowed user: %s"%repr(zUser)) 77 if zUser: 78 token = self._generate_token(zUser) 75 79 logging.debug("token for user %s: %s"%(user, token)) 76 80 self.REQUEST.RESPONSE.setHeader("Content-Type", "text/plain") … … 80 84 return "Please Authenticate!" 81 85 82 def _ user_allowed(self, user=None, password=None):86 def _allowed_user(self, user=None, password=None): 83 87 # check the login 84 88 if user == 'anonymous': … … 91 95 if authname == user: 92 96 # user is logged in 93 return auth name97 return authuser 94 98 95 99 if password: … … 108 112 return None 109 113 110 def _generate_token(self, user _id):114 def _generate_token(self, user): 111 115 #return JSON-token 112 116 issue_time = datetime.datetime.now(UTC).replace(microsecond=0) 117 if isinstance(user, basestring): 118 # not a real User object 119 user_id = user 120 else: 121 user_id = user.getUserName() 122 123 payload = { 124 'consumerKey':self.consumer_key, 125 'userId':user_id, 126 'issuedAt':issue_time.isoformat(), 127 'ttl':self.tokenTtl} 113 128 114 return jwt.encode({ 115 'consumerKey': self.consumer_key, 116 'userId': user_id, 117 'issuedAt': issue_time.isoformat(), 118 'ttl': self.tokenTtl 119 }, self.consumer_secret) 129 if hasattr(user, '_getLDAPGroups'): 130 # add groups from LDAP 131 groups = user._getLDAPGroups() 132 payload['memberOf'] = groups 133 134 logging.debug("token payload=%s"%repr(payload)) 135 return jwt.encode(payload, self.consumer_secret) 120 136 121 137
Note: See TracChangeset
for help on using the changeset viewer.