-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_app.py
97 lines (73 loc) · 2.49 KB
/
test_app.py
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
""" Basic test app for Authl, implemented using Flask.
Run it locally with:
poetry install --dev
FLASK_APP=test poetry run flask run
"""
import logging
import os
import flask
import authl.flask
logging.basicConfig(level=logging.DEBUG)
LOGGER = logging.getLogger(__name__)
app = flask.Flask('authl-test')
app.secret_key = "testing isn't secret"
def on_login(verified):
LOGGER.info("Got login: %s", verified)
flask.session['profile'] = verified.profile
if verified.identity == 'test:override':
return "This user gets a special override"
authl.flask.setup(
app,
{
'EMAIL_SENDMAIL': lambda message: print(message.get_payload(decode=True).decode('utf-8')),
'EMAIL_FROM': 'nobody@example.com',
'EMAIL_SUBJECT': 'Log in to authl test',
'EMAIL_CHECK_MESSAGE': 'Use the link printed to the test console',
'EMAIL_EXPIRE_TIME': 60,
'INDIEAUTH_CLIENT_ID': authl.flask.client_id,
'INDIEAUTH_PENDING_TTL': 10,
'TEST_ENABLED': True,
'FEDIVERSE_NAME': 'authl testing',
'FEDIVERSE_HOMEPAGE': 'https://github.com/PlaidWeb/Authl',
'TWITTER_CLIENT_KEY': os.environ.get('TWITTER_CLIENT_KEY'),
'TWITTER_CLIENT_SECRET': os.environ.get('TWITTER_CLIENT_SECRET'),
'TWITTER_REQUEST_EMAIL': True,
},
tester_path='/check_url',
on_verified=on_login
)
@app.route('/logout/')
@app.route('/logout/<path:redir>')
def logout(redir=''):
""" Log out from the thing """
LOGGER.info("Logging out")
LOGGER.info("Redir: %s", redir)
LOGGER.info("Request path: %s", flask.request.path)
flask.session.clear()
return flask.redirect('/' + redir)
@app.route('/')
@app.route('/page')
@app.route('/page/')
@app.route('/page/<path:page>')
def index(page=''):
""" Just displays a very basic login form """
LOGGER.info("Session: %s", flask.session)
LOGGER.info("Request path: %s", flask.request.path)
if 'me' in flask.session:
return flask.render_template_string(
r"""
<p>Hello {{profile.name or me}}.
Want to <a href="{{url_for('logout', redir=request.path[1:])}}">log out</a>?</p>
{% if profile %}
<p>Profile data:</p>
<ul>
{% for k,v in profile.items() %}
<li>{{k}}: {{v}}</li>
{% endfor %}
</ul>
{% endif %}""",
me=flask.session['me'],
profile=flask.session.get('profile')
)
return 'You are not logged in. Want to <a href="{login}">log in</a>?'.format(
login=flask.url_for('authl.login', redir=flask.request.path[1:]))