Commit | Line | Data |
---|---|---|
f46e2a4d JW |
1 | # GNU MediaGoblin -- federated, autonomous media hosting |
2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. | |
3 | # | |
4 | # This program is free software: you can redistribute it and/or modify | |
5 | # it under the terms of the GNU Affero General Public License as published by | |
6 | # the Free Software Foundation, either version 3 of the License, or | |
7 | # (at your option) any later version. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU Affero General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
88a9662b JW |
17 | import uuid |
18 | import bcrypt | |
19 | ||
f46e2a4d JW |
20 | from datetime import datetime, timedelta |
21 | ||
22 | from mediagoblin.db.sql.base import Base | |
23 | from mediagoblin.db.sql.models import User | |
24 | ||
25 | from sqlalchemy import ( | |
88a9662b | 26 | Column, Unicode, Integer, DateTime, ForeignKey, Enum) |
f46e2a4d JW |
27 | from sqlalchemy.orm import relationship |
28 | ||
88a9662b JW |
29 | # Don't remove this, I *think* it applies sqlalchemy-migrate functionality onto |
30 | # the models. | |
31 | from migrate import changeset | |
32 | ||
33 | ||
34 | class OAuthClient(Base): | |
35 | __tablename__ = 'oauth__client' | |
36 | ||
37 | id = Column(Integer, primary_key=True) | |
38 | created = Column(DateTime, nullable=False, | |
39 | default=datetime.now) | |
40 | ||
41 | name = Column(Unicode) | |
42 | description = Column(Unicode) | |
43 | ||
44 | identifier = Column(Unicode, unique=True, index=True) | |
45 | secret = Column(Unicode, index=True) | |
46 | ||
47 | owner_id = Column(Integer, ForeignKey(User.id)) | |
48 | owner = relationship(User, backref='registered_clients') | |
49 | ||
50 | redirect_uri = Column(Unicode) | |
51 | ||
52 | type = Column(Enum( | |
53 | u'confidential', | |
6b8c66d4 JW |
54 | u'public', |
55 | name=u'oauth__client_type')) | |
88a9662b JW |
56 | |
57 | def generate_identifier(self): | |
58 | self.identifier = unicode(uuid.uuid4()) | |
59 | ||
60 | def generate_secret(self): | |
61 | self.secret = unicode( | |
62 | bcrypt.hashpw( | |
63 | unicode(uuid.uuid4()), | |
64 | bcrypt.gensalt())) | |
65 | ||
66 | def __repr__(self): | |
67 | return '<{0} {1}:{2} ({3})>'.format( | |
68 | self.__class__.__name__, | |
69 | self.id, | |
70 | self.name.encode('ascii', 'replace'), | |
71 | self.owner.username.encode('ascii', 'replace')) | |
72 | ||
73 | ||
74 | class OAuthUserClient(Base): | |
75 | __tablename__ = 'oauth__user_client' | |
76 | id = Column(Integer, primary_key=True) | |
77 | ||
78 | user_id = Column(Integer, ForeignKey(User.id)) | |
79 | user = relationship(User, backref='oauth_clients') | |
80 | ||
81 | client_id = Column(Integer, ForeignKey(OAuthClient.id)) | |
82 | client = relationship(OAuthClient, backref='users') | |
83 | ||
84 | state = Column(Enum( | |
85 | u'approved', | |
6b8c66d4 JW |
86 | u'rejected', |
87 | name=u'oauth__relation_state')) | |
88a9662b JW |
88 | |
89 | def __repr__(self): | |
90 | return '<{0} #{1} {2} [{3}, {4}]>'.format( | |
91 | self.__class__.__name__, | |
92 | self.id, | |
93 | self.state.encode('ascii', 'replace'), | |
94 | self.user, | |
95 | self.client) | |
96 | ||
f46e2a4d JW |
97 | |
98 | class OAuthToken(Base): | |
99 | __tablename__ = 'oauth__tokens' | |
100 | ||
101 | id = Column(Integer, primary_key=True) | |
102 | created = Column(DateTime, nullable=False, | |
103 | default=datetime.now) | |
104 | expires = Column(DateTime, nullable=False, | |
105 | default=lambda: datetime.now() + timedelta(days=30)) | |
106 | token = Column(Unicode, index=True) | |
107 | refresh_token = Column(Unicode, index=True) | |
108 | ||
109 | user_id = Column(Integer, ForeignKey(User.id), nullable=False, | |
110 | index=True) | |
111 | user = relationship(User) | |
112 | ||
88a9662b JW |
113 | client_id = Column(Integer, ForeignKey(OAuthClient.id), nullable=False) |
114 | client = relationship(OAuthClient) | |
115 | ||
116 | def __repr__(self): | |
117 | return '<{0} #{1} expires {2} [{3}, {4}]>'.format( | |
118 | self.__class__.__name__, | |
119 | self.id, | |
120 | self.expires.isoformat(), | |
121 | self.user, | |
122 | self.client) | |
123 | ||
f46e2a4d JW |
124 | |
125 | class OAuthCode(Base): | |
126 | __tablename__ = 'oauth__codes' | |
127 | ||
128 | id = Column(Integer, primary_key=True) | |
129 | created = Column(DateTime, nullable=False, | |
130 | default=datetime.now) | |
131 | expires = Column(DateTime, nullable=False, | |
132 | default=lambda: datetime.now() + timedelta(minutes=5)) | |
133 | code = Column(Unicode, index=True) | |
134 | ||
135 | user_id = Column(Integer, ForeignKey(User.id), nullable=False, | |
136 | index=True) | |
137 | user = relationship(User) | |
138 | ||
88a9662b JW |
139 | client_id = Column(Integer, ForeignKey(OAuthClient.id), nullable=False) |
140 | client = relationship(OAuthClient) | |
141 | ||
142 | def __repr__(self): | |
143 | return '<{0} #{1} expires {2} [{3}, {4}]>'.format( | |
144 | self.__class__.__name__, | |
145 | self.id, | |
146 | self.expires.isoformat(), | |
147 | self.user, | |
148 | self.client) | |
149 | ||
f46e2a4d | 150 | |
88a9662b JW |
151 | MODELS = [ |
152 | OAuthToken, | |
153 | OAuthCode, | |
154 | OAuthClient, | |
155 | OAuthUserClient] |