6
6
from core import db
7
7
import bcrypt
8
8
9
+
9
10
class User (db .Model ):
10
11
""" User which will be querying resources from the API.
11
12
12
13
:param db.Model: Base class for database models.
13
14
"""
14
- id = db .Column (db .Integer , primary_key = True )
15
+
16
+ id = db .Column (db .Integer , primary_key = True )
15
17
username = db .Column (db .String (40 ), unique = True )
16
- hashpw = db .Column (db .String (80 ))
18
+ hashpw = db .Column (db .String (80 ))
17
19
18
20
@staticmethod
19
21
def find_with_password (username , password , * args , ** kwargs ):
20
- """ Query the User collection for a record with matching username and password hash.
21
- If only a username is supplied, find the first matching document with that username.
22
+ """ Query the User collection for a record with matching username and
23
+ password hash. If only a username is supplied, find the first matching
24
+ document with that username.
22
25
23
26
:param username: Username of the user.
24
27
:param password: Password of the user.
@@ -28,8 +31,11 @@ def find_with_password(username, password, *args, **kwargs):
28
31
user = User .query .filter_by (username = username ).first ()
29
32
if user and password :
30
33
encodedpw = password .encode ('utf-8' )
31
- userhash = user .hashpw .encode ('utf-8' )
32
- return User .query .filter (User .username == username , User .hashpw == bcrypt .hashpw (encodedpw , userhash )).first ()
34
+ userhash = user .hashpw .encode ('utf-8' )
35
+ return User .query .filter (
36
+ User .username == username ,
37
+ User .hashpw == bcrypt .hashpw (encodedpw , userhash )
38
+ ).first ()
33
39
else :
34
40
return user
35
41
@@ -113,7 +119,7 @@ def find(id):
113
119
def delete (self ):
114
120
""" Delete existing token. """
115
121
db .session .delete (self )
116
- db .session ( commit )
122
+ db .session . commit ( )
117
123
return self
118
124
119
125
@staticmethod
@@ -144,16 +150,17 @@ class Token(db.Model):
144
150
145
151
:param db.Model: Base class for database models.
146
152
"""
147
- id = db .Column (db .Integer , primary_key = True )
148
- client_id = db .Column (db .String (40 ), db .ForeignKey ('client.client_id' ), nullable = False )
149
- client = db .relationship ('Client' )
150
- user_id = db .Column (db .Integer , db .ForeignKey ('user.id' ))
151
- user = db .relationship ('User' )
152
- token_type = db .Column (db .String (40 ))
153
- access_token = db .Column (db .String (255 ), unique = True )
153
+ id = db .Column (db .Integer , primary_key = True )
154
+ client_id = db .Column (db .String (40 ), db .ForeignKey ('client.client_id' ),
155
+ nullable = False )
156
+ client = db .relationship ('Client' )
157
+ user_id = db .Column (db .Integer , db .ForeignKey ('user.id' ))
158
+ user = db .relationship ('User' )
159
+ token_type = db .Column (db .String (40 ))
160
+ access_token = db .Column (db .String (255 ), unique = True )
154
161
refresh_token = db .Column (db .String (255 ), unique = True )
155
- expires = db .Column (db .DateTime )
156
- scopes = ['' ]
162
+ expires = db .Column (db .DateTime )
163
+ scopes = ['' ]
157
164
158
165
@staticmethod
159
166
def find (access_token = None , refresh_token = None ):
@@ -172,8 +179,10 @@ def find(access_token=None, refresh_token=None):
172
179
def save (token , request , * args , ** kwargs ):
173
180
""" Save a new token to the database.
174
181
175
- :param token: Token dictionary containing access and refresh tokens, plus token type.
176
- :param request: Request dictionary containing information about the client and user.
182
+ :param token: Token dictionary containing access and refresh tokens,
183
+ plus token type.
184
+ :param request: Request dictionary containing information about the
185
+ client and user.
177
186
:param *args: Variable length argument list.
178
187
:param **kwargs: Arbitrary keyword arguments.
179
188
"""
@@ -186,7 +195,7 @@ def save(token, request, *args, **kwargs):
186
195
[db .session .delete (t ) for t in toks ]
187
196
188
197
expires_in = token .pop ('expires_in' )
189
- expires = datetime .utcnow () + timedelta (seconds = expires_in )
198
+ expires = datetime .utcnow () + timedelta (seconds = expires_in )
190
199
191
200
tok = Token (
192
201
access_token = token ['access_token' ],
0 commit comments