Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
129 lines (108 loc) · 3.98 KB
/
Copy pathapp.py
File metadata and controls
129 lines (108 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: utf-8 -*-
#
# This file is part of the Toolforge Flask + OAuth WSGI tutorial
#
# Copyright (C) 2017 Bryan Davis and contributors
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""Example Flask application which can authenticate the user via OAuth."""
import flask
import mwoauth
import os
import werkzeug.middleware.proxy_fix
import yaml
# Create the Flask application
app = flask.Flask(__name__)
# Add the ProxyFix middleware which reads X-Forwarded-* headers
app.wsgi_app = werkzeug.middleware.proxy_fix.ProxyFix(app.wsgi_app)
# Load configuration from YAML file(s).
# See default_config.yaml for more information
__dir__ = os.path.dirname(__file__)
app.config.update(
yaml.safe_load(open(os.path.join(__dir__, "default_config.yaml")))
)
try:
app.config.update(
yaml.safe_load(open(os.path.join(__dir__, "config.yaml")))
)
except IOError:
# It is ok if there is no local config file
pass
@app.route("/")
def index():
"""Application landing page."""
username = flask.session.get("username", None)
return flask.render_template("index.html", username=username)
@app.route("/login")
def login():
"""Initiate an OAuth login.
Call the MediaWiki server to get request secrets and then redirect the
user to the MediaWiki server to sign the request.
"""
consumer_token = mwoauth.ConsumerToken(
app.config["CONSUMER_KEY"], app.config["CONSUMER_SECRET"]
)
try:
redirect, request_token = mwoauth.initiate(
app.config["OAUTH_MWURI"], consumer_token
)
except Exception:
app.logger.exception("mwoauth.initiate failed")
flask.flash("OAuth handshake failed.", "danger")
flask.redirect(flask.url_for("index"))
else:
flask.session["request_token"] = dict(
zip(request_token._fields, request_token)
)
return flask.redirect(redirect)
@app.route("/oauth-callback")
def oauth_callback():
"""OAuth handshake callback.
Validate the response from the MediaWiki server and call the MediaWiki API
to get information about the user that has been authenticated.
"""
if "request_token" not in flask.session:
flask.flash("OAuth callback failed. Are cookies disabled?", "danger")
return flask.redirect(flask.url_for("index"))
consumer_token = mwoauth.ConsumerToken(
app.config["CONSUMER_KEY"], app.config["CONSUMER_SECRET"]
)
try:
access_token = mwoauth.complete(
app.config["OAUTH_MWURI"],
consumer_token,
mwoauth.RequestToken(**flask.session["request_token"]),
flask.request.query_string,
)
identity = mwoauth.identify(
app.config["OAUTH_MWURI"], consumer_token, access_token
)
except Exception:
app.logger.exception("OAuth autnetication failed")
flask.flask("OAuth authentication failed.")
else:
flask.session["access_token"] = dict(
zip(access_token._fields, access_token)
)
flask.session["username"] = identity["username"]
flask.flash(
"You were signed in, %s!" % identity["username"], "success"
)
return flask.redirect(flask.url_for("index"))
@app.route("/logout")
def logout():
"""Log the user out by clearing their session."""
flask.session.clear()
flask.flash("You have been logged out.", "info")
return flask.redirect(flask.url_for("index"))