Skip to content

Commit 0754420

Browse files
committed
authorized is now a base function
1 parent b446d5d commit 0754420

File tree

2 files changed

+69
-22
lines changed

2 files changed

+69
-22
lines changed

examples/flask_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
user_agent="Demo mwoauth.flask server.")
4444
app.register_blueprint(flask_mwoauth.bp)
4545

46+
4647
@app.route("/")
4748
def index():
4849
return "logged in as: " + \
@@ -55,7 +56,7 @@ def index():
5556

5657

5758
@app.route("/my_recent_edits")
58-
@flask_mwoauth.authorized
59+
@mwoauth.flask.authorized
5960
def my_recent_edits():
6061
username = flask_mwoauth.identify()['username']
6162
enwiki_session = flask_mwoauth.mwapi_session('https://en.wikipedia.org')

mwoauth/flask.py

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
"""
2+
.. autoclass:: mwoauth.flask.MWOAuth
3+
:members:
4+
:member-order: bysource
5+
6+
.. autofunction:: mwoauth.flask.authorized
7+
"""
8+
19
import logging
210
from functools import wraps
311

@@ -6,7 +14,6 @@
614

715
from six.moves.urllib.parse import urljoin
816

9-
from . import defaults
1017
from .errors import OAuthException
1118
from .handshaker import Handshaker
1219
from .tokens import AccessToken, RequestToken
@@ -28,6 +35,29 @@ class MWOAuth:
2835
/mwoauth/initiate with a "?next=" that will return them to the originating
2936
route once authorization is completed.
3037
38+
:Example:
39+
.. code-block:: python
40+
41+
from flask import Flask
42+
import mwoauth
43+
import mwoauth.flask
44+
45+
app = Flask(__name__)
46+
47+
@app.route("/")
48+
def index():
49+
return "Hello world"
50+
51+
flask_mwoauth = mwoauth.flask.MWOAuth(
52+
"https://en.wikipedia.org",
53+
mwoauth.ConsumerToken("...", "..."))
54+
app.register_blueprint(flask_mwoauth.bp)
55+
56+
@app.route("/my_settings/")
57+
@flask_mwoauth.authorized
58+
def my_settings():
59+
return flask_mwoauth.identity()
60+
3161
:Parameters:
3262
host : str
3363
The host name (including protocol) of the MediaWiki wiki to use
@@ -48,10 +78,9 @@ class MWOAuth:
4878
by MediaWiki.
4979
render_error : func
5080
A method that renders an error. Takes two arguements:
51-
message : str
52-
The error message
53-
status : int
54-
The https status number
81+
82+
* message : str (The error message)
83+
* status : int (The https status number)
5584
**kwargs : dict
5685
Parameters to be passed to :class:`flask.Blueprint` during
5786
its construction.
@@ -76,7 +105,7 @@ def mwoauth_initiate():
76105
"""
77106
Starts an OAuth handshake.
78107
"""
79-
mw_authorizer_url, request_token = self.get_handshaker().initiate()
108+
mw_authorizer_url, request_token = self._handshaker().initiate()
80109
rt_session_key = _str(request_token.key) + "_request_token"
81110
next_session_key = _str(request_token.key) + "_next"
82111

@@ -110,7 +139,7 @@ def mwoauth_callback():
110139

111140
# Complete the handshake
112141
try:
113-
access_token = self.get_handshaker().complete(
142+
access_token = self._handshaker().complete(
114143
RequestToken(**session[rt_session_key]),
115144
_str(request.query_string))
116145
except OAuthException as e:
@@ -124,7 +153,7 @@ def mwoauth_callback():
124153
dict(zip(access_token._fields, access_token))
125154

126155
# Identify the user
127-
identity = self.get_handshaker().identify(access_token)
156+
identity = self._handshaker().identify(access_token)
128157
session['mwoauth_identity'] = identity
129158

130159
# Redirect to wherever we're supposed to go
@@ -134,7 +163,7 @@ def mwoauth_callback():
134163
return redirect(url_for(self.default_next))
135164

136165
@self.bp.route("/mwoauth/identify/")
137-
@self.authorized
166+
@authorized
138167
def mwoauth_identify():
139168
"""
140169
Returns user information if authenticated
@@ -154,7 +183,7 @@ def mwoauth_logout():
154183
else:
155184
return self.render_logout()
156185

157-
def get_handshaker(self):
186+
def _handshaker(self):
158187
if not self.handshaker:
159188
full_callback = urljoin(
160189
request.url_root, url_for("mwoauth.mwoauth_callback"))
@@ -170,12 +199,24 @@ def identify():
170199
return session.get('mwoauth_identity')
171200

172201
def mwapi_session(self, *args, **kwargs):
202+
"""
203+
Creates :class:`mwapi.Session` that is authorized for the current
204+
user.
205+
206+
`args` and `kwargs` are passed directly to :class:`mwapi.Session`
207+
"""
173208
import mwapi
174209
auth1 = self.generate_auth()
175210
return mwapi.Session(*args, **kwargs, user_agent=self.user_agent,
176211
auth=auth1)
177212

178213
def requests_session(self, *args, **kwargs):
214+
"""
215+
Creates :class:`requests.Session` that is authorized for the current
216+
user.
217+
218+
`args` and `kwargs` are passed directly to :class:`requests.Session`
219+
"""
179220
import requests
180221
auth1 = self.generate_auth()
181222
return requests.Session(*args, **kwargs, auth=auth1)
@@ -192,18 +233,23 @@ def generate_auth(self):
192233
raise OAuthException(
193234
"Cannot generate auth. User has not authorized.")
194235

195-
@staticmethod
196-
def authorized(route):
197-
@wraps(route)
198-
def authorized_route(*args, **kwargs):
199-
if 'mwoauth_access_token' in session:
200-
return route(*args, **kwargs)
201-
else:
202-
return redirect(
203-
url_for('mwoauth.mwoauth_initiate') +
204-
"?next=" + request.endpoint)
205236

206-
return authorized_route
237+
def authorized(route):
238+
"""
239+
Wraps a flask route. Ensures that the user has authorized via OAuth or
240+
redirects the user to the authorization endpoint with a delayed redirect
241+
back to the originating endpoint.
242+
"""
243+
@wraps(route)
244+
def authorized_route(*args, **kwargs):
245+
if 'mwoauth_access_token' in session:
246+
return route(*args, **kwargs)
247+
else:
248+
return redirect(
249+
url_for('mwoauth.mwoauth_initiate') +
250+
"?next=" + request.endpoint)
251+
252+
return authorized_route
207253

208254

209255
def generic_logout():

0 commit comments

Comments
 (0)