Workout With Friends Stay fit with a little motivation

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Properties
tokens.py
Go to the documentation of this file.
1 from __future__ import unicode_literals
2 import hashlib
3 import uuid
4 from sqlalchemy.ext.declarative import declared_attr
5 from sqlalchemy.orm import relationship, synonym
6 from sqlalchemy.schema import Column, ForeignKey
7 from sqlalchemy.types import CHAR, DateTime, Integer
8 from wowf.lib.utils import current_timestamp
9 from wowf.models import Base
10
11
12 class Token(object):
13
14 id = Column(Integer(unsigned=True), primary_key=True)
15 token = Column(CHAR(36), nullable=False, unique=True, default=lambda: str(uuid.uuid4()))
16 created_at = Column(DateTime, nullable=False, default=current_timestamp)
17 expires_at = Column(DateTime, nullable=False)
18
19 @declared_attr
20 def user_id(cls):
21 return Column(
22 Integer(unsigned=True), ForeignKey('users.id', ondelete='cascade'),
23 nullable=False)
24
25 @declared_attr
26 def user(cls):
27 return relationship('User', lazy='joined')
28
29 def __init__(self, user, lifetime):
30 self.user = user
31 self.expires_at = current_timestamp() + lifetime
32
33 def __unicode__(self):
34 return self.token
35
36 @classmethod
37 def create(cls, user, lifetime):
38 return super(Token, cls).create(user=user, lifetime=lifetime)
39
40 @classmethod
41 def get_by_token(cls, token):
42 return cls.query.filter(cls.token==token).first()
43
44 @classmethod
45 def delete_expired(cls):
46 cls.query.filter(cls.expires_at<current_timestamp()).delete()
47
48 def is_valid(self):
49 return self.user and current_timestamp() < self.expires_at
50
51
52 ##
53 #
54 # Remember me login token.
55 #
56 class LoginToken(Token, Base):
57
58 __tablename__ = 'login_tokens'
59 _user_agent = Column('user_agent', CHAR(32), nullable=False)
60
61 def _get_user_agent(self):
62 return self._user_agent
63
64 ##
65 #
66 # Hash the given user agent.
67 #
68 def _set_user_agent(self, user_agent):
69 self._user_agent = hashlib.md5(user_agent).hexdigest()
70
71 def _check_user_agent(self, user_agent):
72 return self.user_agent == hashlib.md5(user_agent).hexdigest()
73
74 user_agent = synonym('_user_agent', descriptor=property(_get_user_agent, _set_user_agent))
75
76 def __init__(self, user, lifetime, user_agent):
77 super(LoginToken, self).__init__(user, lifetime)
78 self.user_agent = user_agent
79
80 @classmethod
81 def create(cls, user, lifetime, user_agent):
82 return super(Token, cls).create(user=user, lifetime=lifetime, user_agent=user_agent)
83
84 def is_valid(self, user_agent):
85 return Token.is_valid(self) and self._check_user_agent(user_agent)
86
87
88 ##
89 #
90 # Reset password token.
91 #
92 class PasswordToken(Token, Base):
93
94 __tablename__ = 'password_tokens'
95
96
97 ##
98 #
99 # Invite new user token.
100 #
101 # Invitations need not necessarily come from any specific user, so the user
102 # is optional.
103 #
104 class InviteToken(Token, Base):
105
106 __tablename__ = 'invite_tokens'
107 user_id = Column(
108 Integer(unsigned=True), ForeignKey('users.id', ondelete='cascade'),
109 nullable=True)
110
111 def is_valid(self):
112 return current_timestamp() < self.expires_at
113