91bd0fc63778ae137acf6dd06f7aa068d2c8fd39
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
)
56 def generate_identifier(self
):
57 self
.identifier
= unicode(uuid
.uuid4())
59 def generate_secret(self
):
60 self
.secret
= unicode(
62 unicode(uuid
.uuid4()),
66 return '<{0} {1}:{2} ({3})>'.format(
67 self
.__class
__.__name
__,
69 self
.name
.encode('ascii', 'replace'),
70 self
.owner
.username
.encode('ascii', 'replace'))
73 class OAuthUserClient(Base
):
74 __tablename__
= 'oauth__user_client'
75 id = Column(Integer
, primary_key
=True)
77 user_id
= Column(Integer
, ForeignKey(User
.id))
78 user
= relationship(User
, backref
='oauth_clients')
80 client_id
= Column(Integer
, ForeignKey(OAuthClient
.id))
81 client
= relationship(OAuthClient
, backref
='users')
88 return '<{0} #{1} {2} [{3}, {4}]>'.format(
89 self
.__class
__.__name
__,
91 self
.state
.encode('ascii', 'replace'),
96 class OAuthToken(Base
):
97 __tablename__
= 'oauth__tokens'
99 id = Column(Integer
, primary_key
=True)
100 created
= Column(DateTime
, nullable
=False,
101 default
=datetime
.now
)
102 expires
= Column(DateTime
, nullable
=False,
103 default
=lambda: datetime
.now() + timedelta(days
=30))
104 token
= Column(Unicode
, index
=True)
105 refresh_token
= Column(Unicode
, index
=True)
107 user_id
= Column(Integer
, ForeignKey(User
.id), nullable
=False,
109 user
= relationship(User
)
111 client_id
= Column(Integer
, ForeignKey(OAuthClient
.id), nullable
=False)
112 client
= relationship(OAuthClient
)
115 return '<{0} #{1} expires {2} [{3}, {4}]>'.format(
116 self
.__class
__.__name
__,
118 self
.expires
.isoformat(),
123 class OAuthCode(Base
):
124 __tablename__
= 'oauth__codes'
126 id = Column(Integer
, primary_key
=True)
127 created
= Column(DateTime
, nullable
=False,
128 default
=datetime
.now
)
129 expires
= Column(DateTime
, nullable
=False,
130 default
=lambda: datetime
.now() + timedelta(minutes
=5))
131 code
= Column(Unicode
, index
=True)
133 user_id
= Column(Integer
, ForeignKey(User
.id), nullable
=False,
135 user
= relationship(User
)
137 client_id
= Column(Integer
, ForeignKey(OAuthClient
.id), nullable
=False)
138 client
= relationship(OAuthClient
)
141 return '<{0} #{1} expires {2} [{3}, {4}]>'.format(
142 self
.__class
__.__name
__,
144 self
.expires
.isoformat(),