mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Implemented auth widget object and auth data validator.
This commit is contained in:
parent
6ee38bcad9
commit
4f2cb40aea
4 changed files with 75 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ from . import base
|
|||
from . import fields
|
||||
from .animation import Animation
|
||||
from .audio import Audio
|
||||
from .auth_widget_data import AuthWidgetData
|
||||
from .callback_game import CallbackGame
|
||||
from .callback_query import CallbackQuery
|
||||
from .chat import Chat, ChatActions, ChatType
|
||||
|
|
@ -56,6 +57,7 @@ __all__ = (
|
|||
'AllowedUpdates',
|
||||
'Animation',
|
||||
'Audio',
|
||||
'AuthWidgetData',
|
||||
'CallbackGame',
|
||||
'CallbackQuery',
|
||||
'Chat',
|
||||
|
|
|
|||
44
aiogram/types/auth_widget_data.py
Normal file
44
aiogram/types/auth_widget_data.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
from aiohttp import web
|
||||
|
||||
from . import base
|
||||
from . import fields
|
||||
|
||||
|
||||
class AuthWidgetData(base.TelegramObject):
|
||||
id: base.Integer = fields.Field()
|
||||
first_name: base.String = fields.Field()
|
||||
last_name: base.String = fields.Field()
|
||||
username: base.String = fields.Field()
|
||||
photo_url: base.String = fields.Field()
|
||||
auth_date: base.String = fields.DateTimeField()
|
||||
hash: base.String = fields.Field()
|
||||
|
||||
@classmethod
|
||||
def parse(cls, request: web.Request) -> 'AuthWidgetData':
|
||||
"""
|
||||
Parse request as Telegram auth widget data.
|
||||
|
||||
:param request:
|
||||
:return: :obj:`AuthWidgetData`
|
||||
:raise :obj:`aiohttp.web.HTTPBadRequest`
|
||||
"""
|
||||
try:
|
||||
query = dict(request.query)
|
||||
query['id'] = int(query['id'])
|
||||
query['auth_date'] = int(query['auth_date'])
|
||||
widget = AuthWidgetData(**query)
|
||||
except (ValueError, KeyError):
|
||||
raise web.HTTPBadRequest(text='Invalid auth data')
|
||||
else:
|
||||
return widget
|
||||
|
||||
def validate(self):
|
||||
return self.bot.check_auth_widget(self.to_python())
|
||||
|
||||
@property
|
||||
def full_name(self):
|
||||
result = self.first_name
|
||||
if self.last_name:
|
||||
result += ' '
|
||||
result += self.last_name
|
||||
return result
|
||||
Loading…
Add table
Add a link
Reference in a new issue