Skip to content

Commit

Permalink
GH-3: Introduce endpoint for unauthorized page
Browse files Browse the repository at this point in the history
  • Loading branch information
markhobson committed Oct 12, 2023
1 parent 2e00d88 commit 33ce2c0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 6 additions & 1 deletion schemes/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ def callback() -> BaseResponse:
user = oauth.govuk.userinfo(token=token)

if user["email"] not in current_app.extensions["users"]:
return Response("<h1>Unauthorized</h1>", status=401)
return redirect(url_for("auth.unauthorized"))

session["user"] = user
session["id_token"] = token["id_token"]
return redirect(url_for("home.index"))


@bp.route("/unauthorized")
def unauthorized() -> Response:
return Response("<h1>Unauthorized</h1>", status=401)


@bp.route("/logout")
def logout() -> BaseResponse:
id_token = session["id_token"]
Expand Down
8 changes: 7 additions & 1 deletion tests/integration/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ def test_callback_redirects_to_home(client: FlaskClient) -> None:
assert response.status_code == 302 and response.location == "/home"


def test_callback_when_unauthorized_shows_unauthorized(client: FlaskClient) -> None:
def test_callback_when_unauthorized_redirects_to_unauthorized(client: FlaskClient) -> None:
current_app.extensions["users"].append("[email protected]")
_given_oidc_returns_token_response({"id_token": "jwt"})
_given_oidc_returns_user_info(UserInfo({"email": "[email protected]"}))

response = client.get("/auth")

assert response.status_code == 302 and response.location == "/auth/unauthorized"


def test_unauthorized(client: FlaskClient) -> None:
response = client.get("/auth/unauthorized")

assert response.status_code == 401 and response.text == "<h1>Unauthorized</h1>"


Expand Down

0 comments on commit 33ce2c0

Please sign in to comment.