Akron | ca9bd98 | 2016-12-06 16:59:57 +0100 | [diff] [blame] | 1 | from flask_login import UserMixin |
| 2 | import jwt |
| 3 | |
| 4 | |
| 5 | __author__ = 'hanl' |
| 6 | |
| 7 | |
| 8 | def decrypt_openid(secret, token=None): |
| 9 | values = {} |
| 10 | if token: |
| 11 | values = jwt.decode(jwt=token, key=secret) |
| 12 | print "decoded values %s" % str(values) |
| 13 | return values |
| 14 | |
| 15 | |
| 16 | def encrypt_string(secret, *args): |
| 17 | jwt_string = jwt.encode(args, key=secret) |
| 18 | print "encoded string %s" % str(jwt_string) |
| 19 | return jwt_string |
| 20 | |
| 21 | |
| 22 | |
| 23 | class User(UserMixin): |
| 24 | ''' |
| 25 | the user object |
| 26 | ''' |
| 27 | |
| 28 | def __init__(self, username, password=None, email=None, firstName=None, lastName=None, address=None, |
| 29 | institution=None, |
| 30 | phone=None, country=None): |
| 31 | if not username: |
| 32 | raise ValueError("the username must be set") |
| 33 | self.username = username |
| 34 | self.password = password |
| 35 | self.email = email |
| 36 | self.firstName = firstName |
| 37 | self.lastName = lastName |
| 38 | self.address = address |
| 39 | self.institution = institution |
| 40 | self.phone = phone |
| 41 | self.country = country |
| 42 | |
| 43 | def has_details(self): |
| 44 | if self.firstName and self.lastName and self.email: |
| 45 | return True |
| 46 | return False |
| 47 | |
| 48 | def is_admin(self): |
| 49 | ''' |
| 50 | to be used for admin purposes |
| 51 | ''' |
| 52 | return False |
| 53 | |
| 54 | def get_id(self): |
| 55 | ''' |
| 56 | :return: id reference to retrieve user object from middleware |
| 57 | ''' |
| 58 | return unicode(self.username) |
| 59 | |
| 60 | def get_full_name(self): |
| 61 | if self.firstName and self.lastName: |
| 62 | return u' '.join([self.firstName, self.lastName]) |
| 63 | return None |