Skip to content

Commit 893b03c

Browse files
committed
rename to bind_cache_grant and bind_sqlalchemy
1 parent 615462a commit 893b03c

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

flask_oauthlib/contrib/oauth2.py

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .cache import Cache
1414

1515

16-
__all__ = ('GrantCacheBinding', 'SQLAlchemyBinding')
16+
__all__ = ('bind_cache_grant', 'bind_sqlalchemy')
1717

1818

1919
log = logging.getLogger('flask_oauthlib')
@@ -63,8 +63,32 @@ def keys(self):
6363
return ['client_id', 'code', 'redirect_uri', 'scopes', 'user']
6464

6565

66-
def grant_cache_binding(app, provider, current_user):
67-
cache = Cache(app, 'OAUTH2')
66+
def bind_cache_grant(app, provider, current_user, config_prefix='OAUTH2'):
67+
"""Configures an :class:`OAuth2Provider` instance to use various caching
68+
systems to get and set the grant token. This removes the need to
69+
register :func:`grantgetter` and :func:`grantsetter` yourself.
70+
71+
:param app: Flask application instance
72+
:param provider: :class:`OAuth2Provider` instance
73+
:param current_user: function that returns an :class:`User` object
74+
:param config_prefix: prefix for config
75+
76+
A usage example::
77+
78+
oauth = OAuth2Provider(app)
79+
app.config.update({'OAUTH2_CACHE_TYPE': 'redis'})
80+
81+
bind_cache_grant(app, oauth, current_user)
82+
83+
You can define which cache system you would like to use by setting the
84+
following configuration option::
85+
86+
OAUTH2_CACHE_TYPE = 'null' // memcache, simple, redis, filesystem
87+
88+
For more information on the supported cache systems please visit:
89+
`Cache <http://werkzeug.pocoo.org/docs/contrib/cache/>`_
90+
"""
91+
cache = Cache(app, config_prefix)
6892

6993
@provider.grantsetter
7094
def create_grant(client_id, code, request, *args, **kwargs):
@@ -75,7 +99,7 @@ def create_grant(client_id, code, request, *args, **kwargs):
7599
code=code['code'],
76100
redirect_uri=request.redirect_uri,
77101
scopes=request.scopes,
78-
user=current_user()
102+
user=current_user(),
79103
)
80104
log.debug("Set Grant Token with key {0}".format(grant.key))
81105
cache.set(grant.key, dict(grant))
@@ -94,25 +118,25 @@ def get(client_id, code):
94118
return grant
95119

96120

97-
class SQLAlchemyBinding(object):
121+
def bind_sqlalchemy(provider, session, user=None, client=None,
122+
token=None, grant=None, current_user=None):
98123
"""Configures the given :class:`OAuth2Provider` instance with the
99124
required getters and setters for persistence with SQLAlchemy.
100125
101126
An example of using all models::
102127
103128
oauth = OAuth2Provider(app)
104129
105-
SQLAlchemyBinding(oauth, session, user=User, client=Client,
106-
token=Token, grant=Grant, current_user=current_user)
130+
bind_sqlalchemy(oauth, session, user=User, client=Client,
131+
token=Token, grant=Grant, current_user=current_user)
107132
108133
You can omit any model if you wish to register the functions yourself.
109134
It is also possible to override the functions by registering them
110135
afterwards::
111136
112137
oauth = OAuth2Provider(app)
113138
114-
SQLAlchemyBinding(oauth, session, user=User, client=Client,
115-
token=Token)
139+
bind_sqlalchemy(oauth, session, user=User, client=Client, token=Token)
116140
117141
@oauth.grantgetter
118142
def get_grant(client_id, code):
@@ -141,32 +165,27 @@ def set_token(token, request, *args, **kwargs):
141165
:param token: :class:`Token` model
142166
:param grant: :class:`Grant` model
143167
:param current_user: function that returns a :class:`User` object
144-
145168
"""
146-
147-
def __init__(self, provider, session, user=None, client=None,
148-
token=None, grant=None, current_user=None):
149-
150-
if user:
151-
user_binding = UserBinding(user, session)
152-
provider.usergetter(user_binding.get)
153-
154-
if client:
155-
client_binding = ClientBinding(client, session)
156-
provider.clientgetter(client_binding.get)
157-
158-
if token:
159-
token_binding = TokenBinding(token, session)
160-
provider.tokengetter(token_binding.get)
161-
provider.tokensetter(token_binding.set)
162-
163-
if grant:
164-
if not current_user:
165-
raise ValueError(('`current_user` is required'
166-
'for Grant Binding'))
167-
grant_binding = GrantBinding(grant, session, current_user)
168-
provider.grantgetter(grant_binding.get)
169-
provider.grantsetter(grant_binding.set)
169+
if user:
170+
user_binding = UserBinding(user, session)
171+
provider.usergetter(user_binding.get)
172+
173+
if client:
174+
client_binding = ClientBinding(client, session)
175+
provider.clientgetter(client_binding.get)
176+
177+
if token:
178+
token_binding = TokenBinding(token, session)
179+
provider.tokengetter(token_binding.get)
180+
provider.tokensetter(token_binding.set)
181+
182+
if grant:
183+
if not current_user:
184+
raise ValueError(('`current_user` is required'
185+
'for Grant Binding'))
186+
grant_binding = GrantBinding(grant, session, current_user)
187+
provider.grantgetter(grant_binding.get)
188+
provider.grantsetter(grant_binding.set)
170189

171190

172191
class BaseBinding(object):

tests/oauth2/server.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from flask.ext.sqlalchemy import SQLAlchemy
55
from sqlalchemy.orm import relationship
66
from flask_oauthlib.provider import OAuth2Provider
7-
from flask_oauthlib.contrib.oauth2 import SQLAlchemyBinding
8-
from flask_oauthlib.contrib.oauth2 import grant_cache_binding
7+
from flask_oauthlib.contrib.oauth2 import bind_sqlalchemy
8+
from flask_oauthlib.contrib.oauth2 import bind_cache_grant
99

1010

1111
db = SQLAlchemy()
@@ -119,19 +119,19 @@ def current_user():
119119
def cache_provider(app):
120120
oauth = OAuth2Provider(app)
121121

122-
SQLAlchemyBinding(oauth, db.session, user=User,
123-
token=Token, client=Client)
122+
bind_sqlalchemy(oauth, db.session, user=User,
123+
token=Token, client=Client)
124124

125125
app.config.update({'OAUTH2_CACHE_TYPE': 'simple'})
126-
grant_cache_binding(app, oauth, current_user)
126+
bind_cache_grant(app, oauth, current_user)
127127
return oauth
128128

129129

130130
def sqlalchemy_provider(app):
131131
oauth = OAuth2Provider(app)
132132

133-
SQLAlchemyBinding(oauth, db.session, user=User, token=Token,
134-
client=Client, grant=Grant, current_user=current_user)
133+
bind_sqlalchemy(oauth, db.session, user=User, token=Token,
134+
client=Client, grant=Grant, current_user=current_user)
135135

136136
return oauth
137137

0 commit comments

Comments
 (0)