Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(external-login): add redirect_uri validation #407

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions seacatauth/external_login/handler/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ async def pair_external_account(self, request):
"""
redirect_uri = request.query.get("redirect_uri")
provider_type = request.match_info["provider_type"]
client_id = self.ExternalLoginService.get_client_id(redirect_uri)
if not await self.ExternalLoginService.validate_client_id_and_redirect_uri(redirect_uri, client_id):
return aiohttp.web.HTTPBadRequest()
authorization_url = await self.ExternalLoginService.initialize_pairing_external_account(
provider_type, redirect_uri)
return aiohttp.web.HTTPFound(authorization_url)
Expand All @@ -91,6 +94,9 @@ async def login_with_external_account(self, request):
"""
redirect_uri = request.query.get("redirect_uri")
provider_type = request.match_info["provider_type"]
client_id = self.ExternalLoginService.get_client_id(redirect_uri)
if not await self.ExternalLoginService.validate_client_id_and_redirect_uri(redirect_uri, client_id):
return aiohttp.web.HTTPBadRequest()
authorization_url = await self.ExternalLoginService.initialize_login_with_external_account(
provider_type, redirect_uri)
return aiohttp.web.HTTPFound(authorization_url)
Expand All @@ -112,6 +118,9 @@ async def sign_up_with_external_account(self, request):
"""
redirect_uri = request.query.get("redirect_uri")
provider_type = request.match_info["provider_type"]
client_id = self.ExternalLoginService.get_client_id(redirect_uri)
if not await self.ExternalLoginService.validate_client_id_and_redirect_uri(redirect_uri, client_id):
return aiohttp.web.HTTPBadRequest()
try:
authorization_url = await self.ExternalLoginService.initialize_signup_with_external_account(
provider_type, redirect_uri)
Expand Down
21 changes: 21 additions & 0 deletions seacatauth/external_login/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

import asab
import asab.web.rest
import urllib.parse

from .. import exceptions, AuditLogger
from ..last_activity import EventCode
from .utils import AuthOperation
from .providers import create_provider, GenericOAuth2Login
from seacatauth.client.service import validate_redirect_uri
from .storage import ExternalLoginStateStorage, ExternalLoginAccountStorage
from .exceptions import (
LoginWithExternalAccountError,
Expand Down Expand Up @@ -69,6 +71,7 @@ async def initialize(self, app):
self.RoleService = app.get_service("seacatauth.RoleService")
self.LastActivityService = app.get_service("seacatauth.LastActivityService")
self.CookieService = app.get_service("seacatauth.CookieService")
self.ClientService = app.get_service("seacatauth.ClientService")

for provider in self.Providers.values():
await provider.initialize(app)
Expand Down Expand Up @@ -553,3 +556,21 @@ def _get_final_redirect_uri(self, state: dict):
return state["redirect_uri"]
# No redirect_uri was specified; redirect to default URL
return self.DefaultRedirectUri

def get_client_id(self, redirect_uri: str):
parsed = urllib.parse.urlparse(redirect_uri)._asdict()
parsed_dict = dict(urllib.parse.parse_qsl(parsed["query"]))

return parsed_dict.get("client_id")

async def validate_client_id_and_redirect_uri(self, redirect_uri: str, client_id: str):
try:
client = await self.ClientService.get(client_id)
except KeyError:
L.error("Client not found in external login.", struct_data={"client_id": client_id})
return False
if not validate_redirect_uri(redirect_uri, client["redirect_uris"], client.get("redirect_uri_validation_method")):
L.error("Invalid redirect uri for external login.", struct_data={"redirect_uri": redirect_uri, "client_id": client_id})
return False

return True