mwoauth.flask¶
-
class
mwoauth.flask.
MWOAuth
(host, consumer_token, user_agent=None, default_next='index', render_logout=None, render_indentify=None, render_error=None, **kwargs)[source]¶ Implements a basic MediaWiki OAuth pattern with a set of routes * /mwoauth/initiate – Starts an OAuth handshake * /mwoauth/callback – Completes an OAuth handshake * /mwoauth/identify – Gets identity information about an authorized user * /mwoauth/logout – Dicards OAuth tokens and user identity
There’s also a convenient decorator provided
authorized()
. When applied to a routing function, this decorator will redirect non-authorized users to /mwoauth/initiate with a ”?next=” that will return them to the originating route once authorization is completed.Example: from flask import Flask import mwoauth import mwoauth.flask app = Flask(__name__) @app.route("/") def index(): return "Hello world" flask_mwoauth = mwoauth.flask.MWOAuth( "https://en.wikipedia.org", mwoauth.ConsumerToken("...", "...")) app.register_blueprint(flask_mwoauth.bp) @app.route("/my_settings/") @mwoauth.flask.authorized def my_settings(): return flask_mwoauth.identity()
Parameters: - host : str
The host name (including protocol) of the MediaWiki wiki to use for the OAuth handshake.
- consumer_token :
The consumer token information
- user_agent : str
A User-Agent header to include with requests. A warning will be logged is this is not set.
- default_next : str
Where should the user be redirected after an OAuth handshake when no ‘?next=’ param is provided
- render_logout : func
A method that renders the logout page seen at /mwoauth/logout
- render_identify : func
A method that renders the identify page seen at /mwoauth/identify. Takes one positional argument – the identity dictionary returned by MediaWiki.
- render_error : func
A method that renders an error. Takes two arguements:
- message : str (The error message)
- status : int (The https status number)
- **kwargs : dict
Parameters to be passed to
flask.Blueprint
during its construction.
mwoauth.ConsumerToken
-
mwapi_session
(*args, **kwargs)[source]¶ Create
mwapi.Session
that is authorized for the current user.args and kwargs are passed directly to
mwapi.Session
-
requests_session
(*args, **kwargs)[source]¶ Create
requests.Session
that is authorized for the current user.args and kwargs are passed directly to
requests.Session
Wrap a flask route. Ensure that the user has authorized via OAuth or redirect the user to the authorization endpoint with a delayed redirect back to the originating endpoint.