2019-01-19 21:00:29 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-02-17 07:17:25 +00:00
|
|
|
from CTFd.models import Teams, Users
|
|
|
|
from CTFd.utils import set_config
|
2019-05-12 01:09:37 +00:00
|
|
|
from tests.helpers import (
|
|
|
|
create_ctfd,
|
|
|
|
destroy_ctfd,
|
|
|
|
login_as_user,
|
|
|
|
login_with_mlc,
|
2020-02-17 07:17:25 +00:00
|
|
|
register_user,
|
2019-05-12 01:09:37 +00:00
|
|
|
)
|
2019-01-19 21:00:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_oauth_not_configured():
|
|
|
|
"""Test that OAuth redirection fails if OAuth settings aren't configured"""
|
|
|
|
app = create_ctfd()
|
|
|
|
with app.app_context():
|
|
|
|
with app.test_client() as client:
|
2019-05-12 01:09:37 +00:00
|
|
|
r = client.get("/oauth", follow_redirects=False)
|
|
|
|
assert r.location == "http://localhost/login"
|
2019-01-19 21:00:29 +00:00
|
|
|
r = client.get(r.location)
|
|
|
|
resp = r.get_data(as_text=True)
|
|
|
|
assert "OAuth Settings not configured" in resp
|
|
|
|
destroy_ctfd(app)
|
|
|
|
|
|
|
|
|
|
|
|
def test_oauth_configured_flow():
|
|
|
|
"""Test that MLC integration works properly but does not allow registration (account creation) if disabled"""
|
|
|
|
app = create_ctfd(user_mode="teams")
|
2019-05-12 01:09:37 +00:00
|
|
|
app.config.update(
|
|
|
|
{
|
|
|
|
"OAUTH_CLIENT_ID": "ctfd_testing_client_id",
|
|
|
|
"OAUTH_CLIENT_SECRET": "ctfd_testing_client_secret",
|
|
|
|
"OAUTH_AUTHORIZATION_ENDPOINT": "http://auth.localhost/oauth/authorize",
|
|
|
|
"OAUTH_TOKEN_ENDPOINT": "http://auth.localhost/oauth/token",
|
|
|
|
"OAUTH_API_ENDPOINT": "http://api.localhost/user",
|
|
|
|
}
|
|
|
|
)
|
2019-01-19 21:00:29 +00:00
|
|
|
with app.app_context():
|
2019-05-12 01:09:37 +00:00
|
|
|
set_config("registration_visibility", "private")
|
2019-01-19 21:00:29 +00:00
|
|
|
assert Users.query.count() == 1
|
|
|
|
assert Teams.query.count() == 0
|
|
|
|
|
|
|
|
client = login_with_mlc(app, raise_for_error=False)
|
|
|
|
|
|
|
|
assert Users.query.count() == 1
|
|
|
|
|
|
|
|
# Users shouldn't be able to register because registration is disabled
|
2019-05-12 01:09:37 +00:00
|
|
|
resp = client.get("http://localhost/login").get_data(as_text=True)
|
|
|
|
assert "Public registration is disabled" in resp
|
2019-01-19 21:00:29 +00:00
|
|
|
|
2019-05-12 01:09:37 +00:00
|
|
|
set_config("registration_visibility", "public")
|
2019-01-19 21:00:29 +00:00
|
|
|
client = login_with_mlc(app)
|
|
|
|
|
|
|
|
# Users should be able to register now
|
|
|
|
assert Users.query.count() == 2
|
2019-05-12 01:09:37 +00:00
|
|
|
user = Users.query.filter_by(email="user@ctfd.io").first()
|
2019-01-19 21:00:29 +00:00
|
|
|
assert user.oauth_id == 1337
|
|
|
|
assert user.team_id == 1
|
|
|
|
|
|
|
|
# Teams should be created
|
|
|
|
assert Teams.query.count() == 1
|
|
|
|
team = Teams.query.filter_by(id=1).first()
|
|
|
|
assert team.oauth_id == 1234
|
|
|
|
|
2019-05-12 01:09:37 +00:00
|
|
|
client.get("/logout")
|
2019-01-19 21:00:29 +00:00
|
|
|
|
|
|
|
# Users should still be able to login if registration is disabled
|
2019-05-12 01:09:37 +00:00
|
|
|
set_config("registration_visibility", "private")
|
2019-01-19 21:00:29 +00:00
|
|
|
client = login_with_mlc(app)
|
|
|
|
with client.session_transaction() as sess:
|
2019-05-12 01:09:37 +00:00
|
|
|
assert sess["id"]
|
|
|
|
assert sess["name"]
|
|
|
|
assert sess["email"]
|
|
|
|
assert sess["nonce"]
|
2019-01-19 21:00:29 +00:00
|
|
|
destroy_ctfd(app)
|
|
|
|
|
|
|
|
|
|
|
|
def test_oauth_login_upgrade():
|
|
|
|
"""Test that users who use MLC after having registered will be associated with their MLC account"""
|
|
|
|
app = create_ctfd(user_mode="teams")
|
2019-05-12 01:09:37 +00:00
|
|
|
app.config.update(
|
|
|
|
{
|
|
|
|
"OAUTH_CLIENT_ID": "ctfd_testing_client_id",
|
|
|
|
"OAUTH_CLIENT_SECRET": "ctfd_testing_client_secret",
|
|
|
|
"OAUTH_AUTHORIZATION_ENDPOINT": "http://auth.localhost/oauth/authorize",
|
|
|
|
"OAUTH_TOKEN_ENDPOINT": "http://auth.localhost/oauth/token",
|
|
|
|
"OAUTH_API_ENDPOINT": "http://api.localhost/user",
|
|
|
|
}
|
|
|
|
)
|
2019-01-19 21:00:29 +00:00
|
|
|
with app.app_context():
|
|
|
|
register_user(app)
|
|
|
|
assert Users.query.count() == 2
|
2019-05-12 01:09:37 +00:00
|
|
|
set_config("registration_visibility", "private")
|
2019-01-19 21:00:29 +00:00
|
|
|
|
|
|
|
# Users should still be able to login
|
|
|
|
client = login_as_user(app)
|
2019-05-12 01:09:37 +00:00
|
|
|
client.get("/logout")
|
2019-01-19 21:00:29 +00:00
|
|
|
|
|
|
|
user = Users.query.filter_by(id=2).first()
|
|
|
|
assert user.oauth_id is None
|
|
|
|
assert user.team_id is None
|
|
|
|
|
|
|
|
login_with_mlc(app)
|
|
|
|
|
|
|
|
assert Users.query.count() == 2
|
|
|
|
|
|
|
|
# Logging in with MLC should insert an OAuth ID and team ID
|
|
|
|
user = Users.query.filter_by(id=2).first()
|
|
|
|
assert user.oauth_id
|
|
|
|
assert user.verified
|
|
|
|
assert user.team_id
|
|
|
|
destroy_ctfd(app)
|