1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
20 from datetime
import datetime
, timedelta
22 from mediagoblin
.db
.sql
.base
import Base
23 from mediagoblin
.db
.sql
.models
import User
25 from sqlalchemy
import (
26 Column
, Unicode
, Integer
, DateTime
, ForeignKey
, Enum
)
27 from sqlalchemy
.orm
import relationship
29 # Don't remove this, I *think* it applies sqlalchemy-migrate functionality onto
31 from migrate
import changeset
34 class OAuthClient(Base
):
35 __tablename__
= 'oauth__client'
37 id = Column(Integer
, primary_key
=True)
38 created
= Column(DateTime
, nullable
=False,
41 name
= Column(Unicode
)
42 description
= Column(Unicode
)
44 identifier
= Column(Unicode
, unique
=True, index
=True)
45 secret
= Column(Unicode
, index
=True)
47 owner_id
= Column(Integer
, ForeignKey(User
.id))
48 owner
= relationship(User
, backref
='registered_clients')
50 redirect_uri
= Column(Unicode
)
55 name
=u
'oauth__client_type'))
57 def generate_identifier(self
):
58 self
.identifier
= unicode(uuid
.uuid4())
60 def generate_secret(self
):
61 self
.secret
= unicode(
63 unicode(uuid
.uuid4()),
67 return '<{0} {1}:{2} ({3})>'.format(
68 self
.__class
__.__name
__,
70 self
.name
.encode('ascii', 'replace'),
71 self
.owner
.username
.encode('ascii', 'replace'))
74 class OAuthUserClient(Base
):
75 __tablename__
= 'oauth__user_client'
76 id = Column(Integer
, primary_key
=True)
78 user_id
= Column(Integer
, ForeignKey(User
.id))
79 user
= relationship(User
, backref
='oauth_clients')
81 client_id
= Column(Integer
, ForeignKey(OAuthClient
.id))
82 client
= relationship(OAuthClient
, backref
='users')
87 name
=u
'oauth__relation_state'))
90 return '<{0} #{1} {2} [{3}, {4}]>'.format(
91 self
.__class
__.__name
__,
93 self
.state
.encode('ascii', 'replace'),
98 class OAuthToken(Base
):
99 __tablename__
= 'oauth__tokens'
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)
109 user_id
= Column(Integer
, ForeignKey(User
.id), nullable
=False,
111 user
= relationship(User
)
113 client_id
= Column(Integer
, ForeignKey(OAuthClient
.id), nullable
=False)
114 client
= relationship(OAuthClient
)
117 return '<{0} #{1} expires {2} [{3}, {4}]>'.format(
118 self
.__class
__.__name
__,
120 self
.expires
.isoformat(),
125 class OAuthCode(Base
):
126 __tablename__
= 'oauth__codes'
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)
135 user_id
= Column(Integer
, ForeignKey(User
.id), nullable
=False,
137 user
= relationship(User
)
139 client_id
= Column(Integer
, ForeignKey(OAuthClient
.id), nullable
=False)
140 client
= relationship(OAuthClient
)
143 return '<{0} #{1} expires {2} [{3}, {4}]>'.format(
144 self
.__class
__.__name
__,
146 self
.expires
.isoformat(),