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/>.
17 from datetime
import datetime
, timedelta
18 from sqlalchemy
import (MetaData
, Table
, Column
,
19 Integer
, Unicode
, Enum
, DateTime
, ForeignKey
)
20 from sqlalchemy
.ext
.declarative
import declarative_base
22 from mediagoblin
.db
.sql
.util
import RegisterMigration
23 from mediagoblin
.db
.sql
.models
import User
29 class OAuthClient_v0(declarative_base()):
30 __tablename__
= 'oauth__client'
32 id = Column(Integer
, primary_key
=True)
33 created
= Column(DateTime
, nullable
=False,
36 name
= Column(Unicode
)
37 description
= Column(Unicode
)
39 identifier
= Column(Unicode
, unique
=True, index
=True)
40 secret
= Column(Unicode
, index
=True)
42 owner_id
= Column(Integer
, ForeignKey(User
.id))
43 redirect_uri
= Column(Unicode
)
48 name
=u
'oauth__client_type'))
51 class OAuthUserClient_v0(declarative_base()):
52 __tablename__
= 'oauth__user_client'
53 id = Column(Integer
, primary_key
=True)
55 user_id
= Column(Integer
, ForeignKey(User
.id))
56 client_id
= Column(Integer
, ForeignKey(OAuthClient_v0
.id))
61 name
=u
'oauth__relation_state'))
64 class OAuthToken_v0(declarative_base()):
65 __tablename__
= 'oauth__tokens'
67 id = Column(Integer
, primary_key
=True)
68 created
= Column(DateTime
, nullable
=False,
70 expires
= Column(DateTime
, nullable
=False,
71 default
=lambda: datetime
.now() + timedelta(days
=30))
72 token
= Column(Unicode
, index
=True)
73 refresh_token
= Column(Unicode
, index
=True)
75 user_id
= Column(Integer
, ForeignKey(User
.id), nullable
=False,
78 client_id
= Column(Integer
, ForeignKey(OAuthClient_v0
.id), nullable
=False)
81 return '<{0} #{1} expires {2} [{3}, {4}]>'.format(
82 self
.__class
__.__name
__,
84 self
.expires
.isoformat(),
89 class OAuthCode_v0(declarative_base()):
90 __tablename__
= 'oauth__codes'
92 id = Column(Integer
, primary_key
=True)
93 created
= Column(DateTime
, nullable
=False,
95 expires
= Column(DateTime
, nullable
=False,
96 default
=lambda: datetime
.now() + timedelta(minutes
=5))
97 code
= Column(Unicode
, index
=True)
99 user_id
= Column(Integer
, ForeignKey(User
.id), nullable
=False,
102 client_id
= Column(Integer
, ForeignKey(OAuthClient_v0
.id), nullable
=False)
105 @RegisterMigration(1, MIGRATIONS
)
106 def remove_and_replace_token_and_code(db
):
107 metadata
= MetaData(bind
=db
.bind
)
109 token_table
= Table('oauth__tokens', metadata
, autoload
=True,
110 autoload_with
=db
.bind
)
114 code_table
= Table('oauth__codes', metadata
, autoload
=True,
115 autoload_with
=db
.bind
)
119 OAuthClient_v0
.__table__
.create(db
.bind
)
120 OAuthUserClient_v0
.__table__
.create(db
.bind
)
121 OAuthToken_v0
.__table__
.create(db
.bind
)
122 OAuthCode_v0
.__table__
.create(db
.bind
)