This hook wraps the standard function that Misago uses to validate a Python dict containing the user data extracted from the OAuth 2 server's response.
Should raise a Django's ValidationError if data is invalid.
This hook can be imported from misago.oauth2.hooks:
from misago.oauth2.hooks import validate_user_data_hookdef custom_validate_user_data_filter(
action: ValidateUserDataHookAction,
request: HttpRequest,
user: Optional[User],
user_data: dict,
response_json: dict,
) -> dict:
...A function implemented by a plugin that can be registered in this hook.
Should raise a Django's ValidationError if data is invalid.
Misago function used for filtering the user data, or the next filter function from another plugin.
See the action section for details.
The request object.
A User object associated with user_data["id"] or user_data["email"], or None if it's the user's first time signing in with OAuth and the user's account hasn't been created yet.
A Python dict with user data extracted from the OAuth 2 server's response:
class UserData(TypedDict):
id: str
name: str | None
email: str | None
avatar: str | NoneThis dict will be unfiltered unless it was filtered by an action call or filter_user_data was used by the plugin to filter it.
A Python dict with the unfiltered OAuth 2 server's user info JSON.
A Python dict containing validated user data:
class UserData(TypedDict):
id: str
name: str | None
email: str | None
avatar: str | Nonedef validate_user_data_action(
request: HttpRequest,
user: Optional[User],
user_data: dict,
response_json: dict,
) -> dict:
...Misago function used for validating the user data, or the next filter function from another plugin.
Should raise a Django's ValidationError if data is invalid.
The request object.
A User object associated with user_data["id"] or user_data["email"], or None if it's the user's first time signing in with OAuth and the user's account hasn't been created yet.
A Python dict with user data extracted from the OAuth 2 server's response:
class UserData(TypedDict):
id: str
name: str | None
email: str | None
avatar: str | NoneThis dict will be unfiltered unless it was filtered by an action call or filter_user_data was used by the plugin to filter it.
A Python dict with the unfiltered OAuth 2 server's user info JSON.
A Python dict containing validated user data:
class UserData(TypedDict):
id: str
name: str | None
email: str | None
avatar: str | NoneThe code below implements a custom validator function that extends the standard logic with additional check for a permission to use the forum by the user:
from django.forms import ValidationError
from django.http import HttpRequest
from misago.oauth.hooks import validate_user_data_hook
from misago.users.models import User
@validate_user_data_hook.append_filter
def normalize_gmail_email(
action,
request: HttpRequest,
user: User | None,
user_data: dict,
response_json: dict,
) -> dict:
# Prevent user from completing the OAuth 2 flow unless they are a member
# of the "forum" group
if (
not response_json.get("groups")
or not isinstance(response_json["groups"], list)
or not "forum" in response_json["groups"]
):
raise ValidationError("You don't have a permission to use the forums.")
# Call the next function in chain
return action(user_data, request, user, user_data, response_json)