Skip to content

Commit

Permalink
Support recaptcha enterprise
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfies committed Jan 15, 2025
1 parent 93a5679 commit fd05eb0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
26 changes: 20 additions & 6 deletions discord/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,6 @@ class CaptchaRequired(HTTPException):
service: :class:`str`
The CAPTCHA service to use. Usually ``hcaptcha``.
.. versionadded:: 2.1
sitekey: :class:`str`
The CAPTCHA sitekey to use.
.. versionadded:: 2.1
rqdata: Optional[:class:`str`]
The enterprise hCaptcha request data.
Expand All @@ -249,21 +245,39 @@ class CaptchaRequired(HTTPException):
rqtoken: Optional[:class:`str`]
The enterprise hCaptcha request token.
.. versionadded:: 2.1
should_serve_invisible: :class:`bool`
Whether the CAPTCHA should be invisible.
.. versionadded:: 2.1
"""

RECAPTCHA_SITEKEY: Final[str] = '6Lef5iQTAAAAAKeIvIY-DeexoO3gj7ryl9rLMEnn'
RECAPTCHA_ENTERPRISE_SITEKEY: Final[str] = '6LeYqFcqAAAAAD6iZesmNgVulsO4PkpBdr6NVG6M'

__slots__ = ('errors', 'service', 'sitekey')

def __init__(self, response: _ResponseType, message: CaptchaPayload):
super().__init__(response, {'code': -1, 'message': 'Captcha required'})
self.json: CaptchaPayload = message
self.errors: List[str] = message['captcha_key']
self.service: CaptchaService = message.get('captcha_service', 'hcaptcha')
self.sitekey: str = message.get('captcha_sitekey') or self.RECAPTCHA_SITEKEY
self.service: CaptchaService = message.get('captcha_service', 'recaptcha')
self._sitekey: str | None = message.get('captcha_sitekey')
self.rqdata: Optional[str] = message.get('captcha_rqdata')
self.rqtoken: Optional[str] = message.get('captcha_rqtoken')
self.should_serve_invisible: bool = message.get('should_serve_invisible', False)

@property
def sitekey(self) -> str:
""":class:`str`: The CAPTCHA sitekey to use.
.. versionadded:: 2.1
"""
if self._sitekey is not None:
return self._sitekey
elif self.service == 'recaptcha_enterprise':
return self.RECAPTCHA_ENTERPRISE_SITEKEY
return self.RECAPTCHA_SITEKEY


class InvalidData(ClientException):
Expand Down
5 changes: 3 additions & 2 deletions discord/types/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ class Error(TypedDict):
errors: NotRequired[FormErrors]


CaptchaService = Literal['hcaptcha', 'recaptcha']
CaptchaService = Literal['hcaptcha', 'recaptcha', 'recaptcha_enterprise']


class CaptchaRequired(TypedDict):
captcha_key: List[str]
captcha_service: CaptchaService
captcha_service: NotRequired[CaptchaService]
captcha_sitekey: Optional[str]
captcha_rqdata: NotRequired[str]
captcha_rqtoken: NotRequired[str]
should_serve_invisible: NotRequired[bool]

0 comments on commit fd05eb0

Please sign in to comment.