Mercurial > p > roundup > code
comparison doc/rest.txt @ 8022:f023b66c297d
doc: update JWT example to use utcnow from anypy_datetime_
datetime.datetime.utcnow() is deprecated. Use replacement utcnow()
from roundup.anypy.datetime_.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Tue, 04 Jun 2024 11:33:51 -0400 |
| parents | 372517700dad |
| children | 57ef20b6c003 |
comparison
equal
deleted
inserted
replaced
| 8021:98429efb80cb | 8022:f023b66c297d |
|---|---|
| 2074 @Routing.route("/jwt/issue", 'POST') | 2074 @Routing.route("/jwt/issue", 'POST') |
| 2075 @_data_decorator | 2075 @_data_decorator |
| 2076 def generate_jwt(self, input): | 2076 def generate_jwt(self, input): |
| 2077 """Create a JSON Web Token (jwt) | 2077 """Create a JSON Web Token (jwt) |
| 2078 """ | 2078 """ |
| 2079 import datetime | |
| 2079 import jwt | 2080 import jwt |
| 2080 import datetime | 2081 from roundup.anypy.datetime_ import utcnow |
| 2081 from roundup.anypy.strings import b2s | 2082 from roundup.anypy.strings import b2s |
| 2082 | 2083 |
| 2083 # require basic auth to generate a token | 2084 # require basic auth to generate a token |
| 2084 # At some point we can support a refresh token. | 2085 # At some point we can support a refresh token. |
| 2085 # maybe a jwt with the "refresh": True claim generated | 2086 # maybe a jwt with the "refresh": True claim generated |
| 2113 user_roles = list(self.db.user.get_roles(self.db.getuid())) | 2114 user_roles = list(self.db.user.get_roles(self.db.getuid())) |
| 2114 | 2115 |
| 2115 claim= { 'sub': self.db.getuid(), | 2116 claim= { 'sub': self.db.getuid(), |
| 2116 'iss': self.db.config.TRACKER_WEB, | 2117 'iss': self.db.config.TRACKER_WEB, |
| 2117 'aud': self.db.config.TRACKER_WEB, | 2118 'aud': self.db.config.TRACKER_WEB, |
| 2118 'iat': datetime.datetime.utcnow(), | 2119 'iat': utcnow(), |
| 2119 } | 2120 } |
| 2120 | 2121 |
| 2121 lifetime = 0 | 2122 lifetime = 0 |
| 2122 if 'lifetime' in input: | 2123 if 'lifetime' in input: |
| 2123 if input['lifetime'].value != 'unlimited': | 2124 if input['lifetime'].value != 'unlimited': |
| 2128 " lifetime in seconds. Got %s."%input['lifetime'].value) | 2129 " lifetime in seconds. Got %s."%input['lifetime'].value) |
| 2129 else: | 2130 else: |
| 2130 lifetime = datetime.timedelta(seconds=86400) # 1 day by default | 2131 lifetime = datetime.timedelta(seconds=86400) # 1 day by default |
| 2131 | 2132 |
| 2132 if lifetime: # if lifetime = 0 make unlimited by omitting exp claim | 2133 if lifetime: # if lifetime = 0 make unlimited by omitting exp claim |
| 2133 claim['exp'] = datetime.datetime.utcnow() + lifetime | 2134 claim['exp'] = utcnow() + lifetime |
| 2134 | 2135 |
| 2135 newroles = [] | 2136 newroles = [] |
| 2136 if 'roles' in input: | 2137 if 'roles' in input: |
| 2137 for role in [ r.lower() for r in input['roles'].value ]: | 2138 for role in [ r.lower() for r in input['roles'].value ]: |
| 2138 if role not in rolenames: | 2139 if role not in rolenames: |
