{"id":371166,"date":"2024-05-21T04:41:54","date_gmt":"2024-05-21T04:41:54","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=371166"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=371166","title":{"rendered":"<span>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u043e\u0439\u0434\u0438\u0442\u0435 \u0447\u0435\u0440\u0435\u0437 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a-\u0442\u043e \u043c\u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u043e\u0441\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c\u0443 \u0441\u0430\u0439\u0442\u0443. \u042f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0441\u0435\u0440\u0432\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0441\u0438\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0432\u043e\u0439\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c \u0438 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043b\u044e\u0434\u0435\u0439 \u0438\u0437 \u0431\u0435\u043b\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430. \u041d\u0438\u0447\u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e, \u043d\u043e \u0432\u0434\u0440\u0443\u0433 \u043a\u043e\u043c\u0443-\u0442\u043e \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c66\/996\/6b5\/c669966b5a0d409c7521779dbbc91411.png\" width=\"2080\" height=\"1160\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c66\/996\/6b5\/c669966b5a0d409c7521779dbbc91411.png\"\/><\/figure>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430 \u0431\u044b\u043b\u0430 \u0442\u0430\u043a\u0430\u044f: \u043d\u0430 \u0437\u0430\u043c\u0435\u043d\u0443 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c\u0443 \u0441\u0430\u0439\u0442\u0443 \u2014 \u043d\u0430\u0431\u043e\u0440\u0443 HTML-\u0441\u0442\u0440\u0430\u043d\u0438\u0446, \u0440\u0430\u0437\u0434\u0430\u044e\u0449\u0435\u043c\u0443\u0441\u044f \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u2014 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 HTML-\u043a\u0438, \u0435\u0441\u043b\u0438 \u0434\u043e\u0441\u0442\u0443\u043f \u0440\u0430\u0437\u0440\u0435\u0448\u0451\u043d.<\/p>\n<p>\u042f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a FastAPI. \u041a\u043e\u0434 \u0438 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u043f\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0443 \u0435\u0441\u0442\u044c <a href=\"https:\/\/github.com\/tm-a-t\/telegram-auth-wall\" rel=\"noopener noreferrer nofollow\">\u043d\u0430 \u0413\u0438\u0442\u0445\u0430\u0431\u0435,<\/a> \u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code>site\/     \u0437\u0434\u0435\u0441\u044c \u043b\u0435\u0436\u0430\u0442 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0444\u0430\u0439\u043b\u044b \u0441\u0430\u0439\u0442\u0430  src\/     templates\/         login.html     __init__.py     auth.py     staticfiles.py     vars.py     whitelist.py<\/code><\/pre>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e\u0431\u044a\u044f\u0441\u043d\u044e.<\/p>\n<p>\u041c\u043e\u0439 \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b \u043f\u043e\u0434\u043e\u0439\u0434\u0451\u0442 \u0432\u0430\u043c, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u0430\u0448 \u0441\u0430\u0439\u0442 \u043d\u0435 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439, \u043d\u043e \u0442\u043e\u0436\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d \u043d\u0430 FastAPI \u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u0440\u0443\u0433\u0443\u044e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e.<\/p>\n<h2>\u0428\u0430\u0433 1. \u0412\u0445\u043e\u0434 \u0447\u0435\u0440\u0435\u0437 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c<\/h2>\n<p>\u041f\u043e\u0441\u0435\u0442\u0438\u0442\u0435\u043b\u044f\u043c \u0441\u0430\u0439\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0435\u0449\u0451 \u043d\u0435 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043b\u0438\u0441\u044c, \u044f \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0441 \u043f\u0440\u043e\u0441\u044c\u0431\u043e\u0439 \u0432\u043e\u0439\u0442\u0438 \u0438 \u043a\u043d\u043e\u043f\u043a\u043e\u0439 \u00ab\u0412\u043e\u0439\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c\u00bb. \u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442, \u043a\u0430\u043a \u043d\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0435 \u0432\u044b\u0448\u0435, \u0438 \u043b\u0435\u0436\u0438\u0442 \u0432 <code>src\/templates\/login.html<\/code>.<\/p>\n<p>\u0421\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043d\u043e\u043f\u043a\u0443 \u00ab\u0412\u043e\u0439\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c\u00bb \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/core.telegram.org\/widgets\/login\" rel=\"noopener noreferrer nofollow\">\u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 Telegram Widget.<\/a> \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c \u043b\u044e\u0431\u0438\u0442 \u0438\u0437\u043e\u0431\u0440\u0435\u0442\u0430\u0442\u044c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u044b \u2014 \u043d\u0438 OAuth, \u043d\u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u044b \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u043d \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442.<\/p>\n<p>\u0412\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c-\u0431\u043e\u0442\u0430. \u0412\u043e \u0432\u0440\u0435\u043c\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0431\u0443\u0434\u0443\u0442 \u0432\u0438\u0434\u0435\u0442\u044c: \u00ab\u0412\u044b \u0432\u0445\u043e\u0434\u0438\u0442\u0435 \u043d\u0430 \u0441\u0430\u0439\u0442 <a href=\"http:\/\/example.com\" rel=\"noopener noreferrer nofollow\">example.com<\/a> \u0447\u0435\u0440\u0435\u0437 \u0431\u043e\u0442\u0430 ExampleBot\u00bb. \u0414\u0430, \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043c\u0435\u0442\u044c \u0431\u043e\u0442\u0430 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u043e\u0435, \u0437\u0430\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c \u0447\u0435\u043a\u0431\u043e\u043a\u0441: \u00ab\u0420\u0430\u0437\u0440\u0435\u0448\u0430\u044e \u0431\u043e\u0442\u0443 \u043f\u0438\u0441\u0430\u0442\u044c \u043c\u043d\u0435 \u0432 \u043b\u0438\u0447\u043a\u0443\u00bb.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/da3\/eab\/2a6\/da3eab2a6f8183e4471beb2dccd89b51.png\" width=\"740\" height=\"728\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/da3\/eab\/2a6\/da3eab2a6f8183e4471beb2dccd89b51.png\"\/><\/figure>\n<details class=\"spoiler\">\n<summary>\u041a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0431\u043e\u0442\u0430 \u0438 \u043a\u043d\u043e\u043f\u043a\u0443<\/summary>\n<div class=\"spoiler__content\">\n<p><a href=\"https:\/\/handbook.tmat.me\/ru\/dev\/botfather\" rel=\"noopener noreferrer nofollow\">\u0417\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0439\u0442\u0435 \u0431\u043e\u0442\u0430.<\/a> \u0412 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 \u0431\u043e\u0442\u0430 \u0432 BotFather \u0443\u043a\u0430\u0436\u0438\u0442\u0435 \u0434\u043e\u043c\u0435\u043d \u0441\u0430\u0439\u0442\u0430: \u0434\u043b\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0434\u043e\u0439\u0434\u0451\u0442 127.0.0.1 \u0438\u043b\u0438 0.0.0.0.<\/p>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 <a href=\"https:\/\/core.telegram.org\/widgets\/login#widget-configuration\" rel=\"noopener noreferrer nofollow\">\u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043d\u043e\u043f\u043a\u0443 \u0434\u043b\u044f\u00a0\u0432\u0445\u043e\u0434\u0430<\/a> \u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/github.com\/tm-a-t\/telegram-auth-wall\/blob\/master\/src\/templates\/login.html\" rel=\"noopener noreferrer nofollow\">\u0432\u0435\u0441\u044c \u043c\u043e\u0439 \u0448\u0430\u0431\u043b\u043e\u043d \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438.<\/a><\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0435 \u043a\u043d\u043e\u043f\u043a\u0443 \u0438 \u0441\u043b\u0435\u0434\u0443\u0435\u0442\u0435 \u043c\u043e\u0435\u043c\u0443 \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u0443 \u0434\u0430\u043b\u044c\u0448\u0435, \u0432 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 redirect to url \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u044c <code>\/auth\/telegram-callback?next={{ next_path }}<\/code>. \u0428\u0430\u0431\u043b\u043e\u043d\u0438\u0437\u0430\u0442\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043c\u0435\u043d\u044f\u0442\u044c <code>{{ next_path }}<\/code> \u0432 \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u0439 HTML-\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043d\u0430 \u0443\u0440\u043b, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<\/div>\n<\/details>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0430 callback url; \u0441\u0435\u0440\u0432\u0435\u0440 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043a\u0443\u043a\u0438. \u0412 \u043a\u0443\u043a\u0438 \u044f \u0445\u0440\u0430\u043d\u044e id \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c\u0435, \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 <a href=\"https:\/\/jwt.io\/\" rel=\"noopener noreferrer nofollow\">\u0432 JWT.<\/a> \u0421\u0435\u0440\u0432\u0435\u0440 \u043d\u0435 \u0445\u0440\u0430\u043d\u0438\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0432\u043e\u0448\u0435\u0434\u0448\u0438\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u0445: \u0432\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u2014 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0447\u0442\u043e id \u0432 \u0431\u0435\u043b\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435.<\/p>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e \u043d\u0443\u0436\u043d\u044b\u0435 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b:<\/p>\n<pre><code class=\"python\"># src\/vars.py  import hashlib import os  JWT_SECRET_KEY = os.environ['JWT_SECRET_KEY'] BOT_TOKEN_HASH = hashlib.sha256(os.environ['BOT_TOKEN'].encode()) COOKIE_NAME = 'auth-token'<\/code><\/pre>\n<p>\u0418 \u0441\u043e\u0437\u0434\u0430\u044e \u043c\u043e\u0434\u0443\u043b\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f FastAPI:<\/p>\n<pre><code class=\"python\"># src\/auth.py  import hmac from typing import Annotated  from fastapi import APIRouter, Query from fastapi.requests import Request from fastapi.responses import PlainTextResponse, RedirectResponse from joserfc import jwt  from src.vars import BOT_TOKEN_HASH, JWT_SECRET_KEY, COOKIE_NAME  auth_router = APIRouter()   @auth_router.get('\/telegram-callback') async def telegram_callback(         request: Request,         user_id: Annotated[int, Query(alias='id')],         query_hash: Annotated[str, Query(alias='hash')],         next_url: Annotated[str, Query(alias='next')] = '\/', ):     params = request.query_params.items()     data_check_string = '\\n'.join(sorted(f'{x}={y}' for x, y in params if x not in ('hash', 'next')))     computed_hash = hmac.new(BOT_TOKEN_HASH.digest(), data_check_string.encode(), 'sha256').hexdigest()     is_correct = hmac.compare_digest(computed_hash, query_hash)     if not is_correct:         return PlainTextResponse('Authorization failed. Please try again', status_code=401)      token = jwt.encode({'alg': 'HS256'}, {'k': user_id}, JWT_SECRET_KEY)     response = RedirectResponse(next_url)     response.set_cookie(key=COOKIE_NAME, value=token)     return response   @auth_router.get('\/logout') async def logout():     response = RedirectResponse('\/')     response.delete_cookie(key=COOKIE_NAME)     return response<\/code><\/pre>\n<h2>\u0428\u0430\u0433 2. \u0411\u0435\u043b\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a<\/h2>\n<p>\u0421\u043f\u0438\u0441\u043e\u043a \u043b\u044e\u0434\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u044f \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u044e \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u0432\u043e\u0435\u043c\u0443 \u0441\u0430\u0439\u0442\u0443, \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u2014 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0431\u044b\u043b\u043e \u0437\u0430\u0445\u0430\u0440\u0434\u043a\u043e\u0434\u0438\u0442\u044c \u0438\u0445 id. \u0412\u0441\u0435 \u043d\u0443\u0436\u043d\u044b\u0435 \u043b\u044e\u0434\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u0442 \u0432 \u043e\u0434\u043d\u043e\u043c \u0447\u0430\u0442\u0435, \u0442\u0430\u043a \u0447\u0442\u043e \u0441\u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0438\u0445 \u0431\u044b\u043b\u043e \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u043e.<\/p>\n<pre><code class=\"python\"># src\/whitelist.py  WHITELIST_IDS = [     254210206,     36265675,     1937983145, ]<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u0421\u043a\u0440\u0438\u043f\u0442, \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0449\u0438\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0447\u0430\u0442\u0430<\/summary>\n<div class=\"spoiler__content\">\n<p>\u041a\u043e\u0434 \u043f\u043e\u0434 TGPy:<\/p>\n<pre><code class=\"python\">ids = [] async for user in client.iter_participants(\"your chat title\"):   ids.append(user.id) ids<\/code><\/pre>\n<p><a href=\"https:\/\/tgpy.tmat.me\" rel=\"noopener noreferrer nofollow\">TGPy<\/a> \u2014 \u044d\u0442\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u043d\u0430\u043c\u0438 \u043e\u043f\u0435\u043d\u0441\u043e\u0440\u0441\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0440\u0430\u0437\u043e\u0432\u044b\u0445 \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432 \u0432 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c\u0435.<\/p>\n<\/div>\n<\/details>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0432\u0430\u043c \u043d\u0435 \u043f\u043e\u0434\u043e\u0439\u0434\u0451\u0442 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a. \u0412\u043c\u0435\u0441\u0442\u043e \u043d\u0435\u0433\u043e \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c id \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u043e\u0442\u0430: \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432 \u0447\u0430\u0442\u0435.<\/p>\n<h2>\u0428\u0430\u0433 3. \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430. \u0412 <code>__init__.py<\/code> \u044f \u0441\u043e\u0437\u0434\u0430\u044e \u0441\u0430\u043c\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 FastAPI. \u0423\u0440\u043b\u044b, \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u043d\u0430 <code>auth\/<\/code>, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u0432\u044b\u0448\u0435.<\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f middleware \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443. \u041e\u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u0440\u043e\u0441 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443:<\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0443\u0440\u043b \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \/auth\/, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0435\u0442 \u043a\u0443\u043a\u0438 \u0438\u043b\u0438 \u0432 \u043a\u0443\u043a\u0438 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u2014 \u043e\u0442\u0434\u0430\u0451\u043c <code>login.html<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0441\u0451 \u043e\u043a, \u043e\u0442\u0434\u0430\u0451\u043c \u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0442\u0434\u0430\u0442\u044c.<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\"># app\/__init__.py  import urllib.parse  from fastapi import FastAPI from fastapi.requests import Request from fastapi.templating import Jinja2Templates from joserfc import jwt from joserfc.errors import JoseError  from src.auth import auth_router from src.vars import COOKIE_NAME, JWT_SECRET_KEY from src.whitelist import WHITELIST_IDS  app = FastAPI() templates = Jinja2Templates('src\/templates')  app.mount('\/auth', auth_router)   @app.middleware('http') async def middleware(request: Request, call_next):     response = await call_next(request)     if request.url.path.startswith('\/auth\/'):         return response      url_safe_path = urllib.parse.quote(request.url.path, safe='')     template_context = {'request': request, 'next_path': url_safe_path}     login_wall = templates.TemplateResponse('login.html', template_context)      token = request.cookies.get(COOKIE_NAME)     if not token:         return login_wall      try:         token_parts = jwt.decode(token, JWT_SECRET_KEY)     except JoseError:         return login_wall      user_id = token_parts.claims['k']     if user_id not in WHITELIST_IDS:         return login_wall      return response<\/code><\/pre>\n<h2>\u0428\u0430\u0433 4. \u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0444\u0430\u0439\u043b\u044b<\/h2>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0448\u0430\u0433 \u2014 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u0444\u0430\u0439\u043b\u044b \u0441\u0430\u0439\u0442\u0430.<\/p>\n<h3>\u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431<\/h3>\n<p>\u041d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u0441\u0442\u0440\u043e\u0447\u0435\u043a:<\/p>\n<pre><code class=\"python\"># app\/__init__.py  from fastapi.staticfiles import StaticFiles  ...  app = FastAPI() templates = Jinja2Templates('src\/templates') static_files = StaticFiles(directory='site\/')  app.mount('\/auth', auth_router) app.mount('\/', static_files, name='static')  ...<\/code><\/pre>\n<h3>\u0421\u043f\u043e\u0441\u043e\u0431 \u0441 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\u043c .html \u0438\u0437 \u0443\u0440\u043b\u043e\u0432<\/h3>\n<p>\u0425\u043e\u0441\u0442\u0438\u043d\u0433 \u043c\u043e\u0435\u0433\u043e \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0441\u0430\u0439\u0442\u0430 \u0443\u0434\u0430\u043b\u044f\u043b \u0438\u0437 \u0430\u0434\u0440\u0435\u0441\u043e\u0432 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f <code>.html<\/code>. \u041e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c, \u0447\u0442\u043e FastAPI \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438 \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u0443\u043c\u0435\u0435\u0442, \u0442\u0430\u043a \u0447\u0442\u043e \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f.<\/p>\n<p>FastAPI \u043e\u0441\u043d\u043e\u0432\u0430\u043d \u043d\u0430 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0435 Starlette, \u0438 \u043a\u043b\u0430\u0441\u0441 StaticFiles \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u0442\u0442\u0443\u0434\u0430. \u0423 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u043e\u043f\u0446\u0438\u044f <code>html=True<\/code>: \u043e\u043d\u0430 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0435 \u043f\u0443\u0442\u0438 \u0432\u0438\u0434\u0430 <code>\/path\/to\/dir\/index.html<\/code> \u0432 \u0443\u0440\u043b\u044b \u0432\u0438\u0434\u0430 <code>\/path\/to\/dir\/<\/code>. \u0423 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0432 \u0443\u0440\u043b\u0430\u0445 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f (<code>\/path\/to\/dir\/file.html<\/code>), \u0442\u0430\u043a \u0447\u0442\u043e \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441.<\/p>\n<pre><code class=\"python\"># src\/staticfiles.py  import os import typing  from fastapi.staticfiles import StaticFiles   class HTMLStaticFiles(StaticFiles):     def __init__(self, **kwargs):         super().__init__(**kwargs, html=True)      def lookup_path(             self, path: str     ) -> typing.Tuple[str, typing.Optional[os.stat_result]]:         if not path.endswith('.html'):             full_path, stat_result = super().lookup_path(path + '.html')             if stat_result:                 return full_path, stat_result          return super().lookup_path(path)<\/code><\/pre>\n<pre><code class=\"python\"># app\/__init__.py  from src.staticfiles import HTMLStaticFiles  ...  app = FastAPI() templates = Jinja2Templates('src\/templates') static_files = HTMLStaticFiles(directory='static\/')  app.mount('\/auth', auth_router) app.mount('\/', static_files, name='static')  ...<\/code><\/pre>\n<h2>\u041a\u0430\u043a \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c<\/h2>\n<p>\u041f\u0440\u043e\u0435\u043a\u0442 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u043b\u0438 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u043e\u0435 FastAPI-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435. <\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0431\u044b\u0441\u0442\u0440\u043e \u043f\u043e\u0442\u044b\u043a\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c-\u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043d\u0430 80 \u043f\u043e\u0440\u0442\u0435:<\/p>\n<pre><code class=\"bash\">sudo BOT_TOKEN=your_token_here JWT_SECRET_KEY=your_random_string uvicorn src:app --reload --port 80<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u043d\u0430\u0435\u0442\u0435, \u043a\u0430\u043a \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0438\u043b\u0438 \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u0434, \u2014 \u0431\u0443\u0434\u0443 \u0440\u0430\u0434 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0432\u0430\u0448\u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438.<\/p>\n<p><a href=\"https:\/\/github.com\/tm-a-t\/telegram-auth-wall\" rel=\"noopener noreferrer nofollow\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u043d\u0430 \u0413\u0438\u0442\u0445\u0430\u0431\u0435.<\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/801121\/\"> https:\/\/habr.com\/ru\/articles\/801121\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a-\u0442\u043e \u043c\u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u043e\u0441\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c\u0443 \u0441\u0430\u0439\u0442\u0443. \u042f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0441\u0435\u0440\u0432\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0441\u0438\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0432\u043e\u0439\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c \u0438 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043b\u044e\u0434\u0435\u0439 \u0438\u0437 \u0431\u0435\u043b\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430. \u041d\u0438\u0447\u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e, \u043d\u043e \u0432\u0434\u0440\u0443\u0433 \u043a\u043e\u043c\u0443-\u0442\u043e \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f.<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430 \u0431\u044b\u043b\u0430 \u0442\u0430\u043a\u0430\u044f: \u043d\u0430 \u0437\u0430\u043c\u0435\u043d\u0443 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c\u0443 \u0441\u0430\u0439\u0442\u0443 \u2014 \u043d\u0430\u0431\u043e\u0440\u0443 HTML-\u0441\u0442\u0440\u0430\u043d\u0438\u0446, \u0440\u0430\u0437\u0434\u0430\u044e\u0449\u0435\u043c\u0443\u0441\u044f \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u2014 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 HTML-\u043a\u0438, \u0435\u0441\u043b\u0438 \u0434\u043e\u0441\u0442\u0443\u043f \u0440\u0430\u0437\u0440\u0435\u0448\u0451\u043d.<\/p>\n<p>\u042f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a FastAPI. \u041a\u043e\u0434 \u0438 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u043f\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0443 \u0435\u0441\u0442\u044c <a href=\"https:\/\/github.com\/tm-a-t\/telegram-auth-wall\" rel=\"noopener noreferrer nofollow\">\u043d\u0430 \u0413\u0438\u0442\u0445\u0430\u0431\u0435,<\/a> \u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code>site\/     \u0437\u0434\u0435\u0441\u044c \u043b\u0435\u0436\u0430\u0442 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0444\u0430\u0439\u043b\u044b \u0441\u0430\u0439\u0442\u0430  src\/     templates\/         login.html     __init__.py     auth.py     staticfiles.py     vars.py     whitelist.py<\/code><\/pre>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e\u0431\u044a\u044f\u0441\u043d\u044e.<\/p>\n<p>\u041c\u043e\u0439 \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b \u043f\u043e\u0434\u043e\u0439\u0434\u0451\u0442 \u0432\u0430\u043c, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u0430\u0448 \u0441\u0430\u0439\u0442 \u043d\u0435 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439, \u043d\u043e \u0442\u043e\u0436\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d \u043d\u0430 FastAPI \u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u0440\u0443\u0433\u0443\u044e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e.<\/p>\n<h2>\u0428\u0430\u0433 1. \u0412\u0445\u043e\u0434 \u0447\u0435\u0440\u0435\u0437 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c<\/h2>\n<p>\u041f\u043e\u0441\u0435\u0442\u0438\u0442\u0435\u043b\u044f\u043c \u0441\u0430\u0439\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0435\u0449\u0451 \u043d\u0435 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043b\u0438\u0441\u044c, \u044f \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0441 \u043f\u0440\u043e\u0441\u044c\u0431\u043e\u0439 \u0432\u043e\u0439\u0442\u0438 \u0438 \u043a\u043d\u043e\u043f\u043a\u043e\u0439 \u00ab\u0412\u043e\u0439\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c\u00bb. \u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442, \u043a\u0430\u043a \u043d\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0435 \u0432\u044b\u0448\u0435, \u0438 \u043b\u0435\u0436\u0438\u0442 \u0432 <code>src\/templates\/login.html<\/code>.<\/p>\n<p>\u0421\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043d\u043e\u043f\u043a\u0443 \u00ab\u0412\u043e\u0439\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c\u00bb \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/core.telegram.org\/widgets\/login\" rel=\"noopener noreferrer nofollow\">\u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 Telegram Widget.<\/a> \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c \u043b\u044e\u0431\u0438\u0442 \u0438\u0437\u043e\u0431\u0440\u0435\u0442\u0430\u0442\u044c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u044b \u2014 \u043d\u0438 OAuth, \u043d\u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u044b \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u043d \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442.<\/p>\n<p>\u0412\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c-\u0431\u043e\u0442\u0430. \u0412\u043e \u0432\u0440\u0435\u043c\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0431\u0443\u0434\u0443\u0442 \u0432\u0438\u0434\u0435\u0442\u044c: \u00ab\u0412\u044b \u0432\u0445\u043e\u0434\u0438\u0442\u0435 \u043d\u0430 \u0441\u0430\u0439\u0442 <a href=\"http:\/\/example.com\" rel=\"noopener noreferrer nofollow\">example.com<\/a> \u0447\u0435\u0440\u0435\u0437 \u0431\u043e\u0442\u0430 ExampleBot\u00bb. \u0414\u0430, \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043c\u0435\u0442\u044c \u0431\u043e\u0442\u0430 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u043e\u0435, \u0437\u0430\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c \u0447\u0435\u043a\u0431\u043e\u043a\u0441: \u00ab\u0420\u0430\u0437\u0440\u0435\u0448\u0430\u044e \u0431\u043e\u0442\u0443 \u043f\u0438\u0441\u0430\u0442\u044c \u043c\u043d\u0435 \u0432 \u043b\u0438\u0447\u043a\u0443\u00bb.<\/p>\n<figure class=\"full-width\"><\/figure>\n<details class=\"spoiler\">\n<summary>\u041a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0431\u043e\u0442\u0430 \u0438 \u043a\u043d\u043e\u043f\u043a\u0443<\/summary>\n<div class=\"spoiler__content\">\n<p><a href=\"https:\/\/handbook.tmat.me\/ru\/dev\/botfather\" rel=\"noopener noreferrer nofollow\">\u0417\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0439\u0442\u0435 \u0431\u043e\u0442\u0430.<\/a> \u0412 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 \u0431\u043e\u0442\u0430 \u0432 BotFather \u0443\u043a\u0430\u0436\u0438\u0442\u0435 \u0434\u043e\u043c\u0435\u043d \u0441\u0430\u0439\u0442\u0430: \u0434\u043b\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0434\u043e\u0439\u0434\u0451\u0442 127.0.0.1 \u0438\u043b\u0438 0.0.0.0.<\/p>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 <a href=\"https:\/\/core.telegram.org\/widgets\/login#widget-configuration\" rel=\"noopener noreferrer nofollow\">\u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043d\u043e\u043f\u043a\u0443 \u0434\u043b\u044f\u00a0\u0432\u0445\u043e\u0434\u0430<\/a> \u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/github.com\/tm-a-t\/telegram-auth-wall\/blob\/master\/src\/templates\/login.html\" rel=\"noopener noreferrer nofollow\">\u0432\u0435\u0441\u044c \u043c\u043e\u0439 \u0448\u0430\u0431\u043b\u043e\u043d \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438.<\/a><\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0435 \u043a\u043d\u043e\u043f\u043a\u0443 \u0438 \u0441\u043b\u0435\u0434\u0443\u0435\u0442\u0435 \u043c\u043e\u0435\u043c\u0443 \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u0443 \u0434\u0430\u043b\u044c\u0448\u0435, \u0432 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 redirect to url \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u044c <code>\/auth\/telegram-callback?next={{ next_path }}<\/code>. \u0428\u0430\u0431\u043b\u043e\u043d\u0438\u0437\u0430\u0442\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043c\u0435\u043d\u044f\u0442\u044c <code>{{ next_path }}<\/code> \u0432 \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u0439 HTML-\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043d\u0430 \u0443\u0440\u043b, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<\/div>\n<\/details>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0430 callback url; \u0441\u0435\u0440\u0432\u0435\u0440 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043a\u0443\u043a\u0438. \u0412 \u043a\u0443\u043a\u0438 \u044f \u0445\u0440\u0430\u043d\u044e id \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c\u0435, \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 <a href=\"https:\/\/jwt.io\/\" rel=\"noopener noreferrer nofollow\">\u0432 JWT.<\/a> \u0421\u0435\u0440\u0432\u0435\u0440 \u043d\u0435 \u0445\u0440\u0430\u043d\u0438\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0432\u043e\u0448\u0435\u0434\u0448\u0438\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u0445: \u0432\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u2014 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0447\u0442\u043e id \u0432 \u0431\u0435\u043b\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435.<\/p>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e \u043d\u0443\u0436\u043d\u044b\u0435 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b:<\/p>\n<pre><code class=\"python\"># src\/vars.py  import hashlib import os  JWT_SECRET_KEY = os.environ['JWT_SECRET_KEY'] BOT_TOKEN_HASH = hashlib.sha256(os.environ['BOT_TOKEN'].encode()) COOKIE_NAME = 'auth-token'<\/code><\/pre>\n<p>\u0418 \u0441\u043e\u0437\u0434\u0430\u044e \u043c\u043e\u0434\u0443\u043b\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f FastAPI:<\/p>\n<pre><code class=\"python\"># src\/auth.py  import hmac from typing import Annotated  from fastapi import APIRouter, Query from fastapi.requests import Request from fastapi.responses import PlainTextResponse, RedirectResponse from joserfc import jwt  from src.vars import BOT_TOKEN_HASH, JWT_SECRET_KEY, COOKIE_NAME  auth_router = APIRouter()   @auth_router.get('\/telegram-callback') async def telegram_callback(         request: Request,         user_id: Annotated[int, Query(alias='id')],         query_hash: Annotated[str, Query(alias='hash')],         next_url: Annotated[str, Query(alias='next')] = '\/', ):     params = request.query_params.items()     data_check_string = '\\n'.join(sorted(f'{x}={y}' for x, y in params if x not in ('hash', 'next')))     computed_hash = hmac.new(BOT_TOKEN_HASH.digest(), data_check_string.encode(), 'sha256').hexdigest()     is_correct = hmac.compare_digest(computed_hash, query_hash)     if not is_correct:         return PlainTextResponse('Authorization failed. Please try again', status_code=401)      token = jwt.encode({'alg': 'HS256'}, {'k': user_id}, JWT_SECRET_KEY)     response = RedirectResponse(next_url)     response.set_cookie(key=COOKIE_NAME, value=token)     return response   @auth_router.get('\/logout') async def logout():     response = RedirectResponse('\/')     response.delete_cookie(key=COOKIE_NAME)     return response<\/code><\/pre>\n<h2>\u0428\u0430\u0433 2. \u0411\u0435\u043b\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a<\/h2>\n<p>\u0421\u043f\u0438\u0441\u043e\u043a \u043b\u044e\u0434\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u044f \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u044e \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u0432\u043e\u0435\u043c\u0443 \u0441\u0430\u0439\u0442\u0443, \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u2014 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0431\u044b\u043b\u043e \u0437\u0430\u0445\u0430\u0440\u0434\u043a\u043e\u0434\u0438\u0442\u044c \u0438\u0445 id. \u0412\u0441\u0435 \u043d\u0443\u0436\u043d\u044b\u0435 \u043b\u044e\u0434\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u0442 \u0432 \u043e\u0434\u043d\u043e\u043c \u0447\u0430\u0442\u0435, \u0442\u0430\u043a \u0447\u0442\u043e \u0441\u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0438\u0445 \u0431\u044b\u043b\u043e \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u043e.<\/p>\n<pre><code class=\"python\"># src\/whitelist.py  WHITELIST_IDS = [     254210206,     36265675,     1937983145, ]<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u0421\u043a\u0440\u0438\u043f\u0442, \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0449\u0438\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0447\u0430\u0442\u0430<\/summary>\n<div class=\"spoiler__content\">\n<p>\u041a\u043e\u0434 \u043f\u043e\u0434 TGPy:<\/p>\n<pre><code class=\"python\">ids = [] async for user in client.iter_participants(\"your chat title\"):   ids.append(user.id) ids<\/code><\/pre>\n<p><a href=\"https:\/\/tgpy.tmat.me\" rel=\"noopener noreferrer nofollow\">TGPy<\/a> \u2014 \u044d\u0442\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u043d\u0430\u043c\u0438 \u043e\u043f\u0435\u043d\u0441\u043e\u0440\u0441\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0440\u0430\u0437\u043e\u0432\u044b\u0445 \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432 \u0432 \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c\u0435.<\/p>\n<\/div>\n<\/details>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0432\u0430\u043c \u043d\u0435 \u043f\u043e\u0434\u043e\u0439\u0434\u0451\u0442 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a. \u0412\u043c\u0435\u0441\u0442\u043e \u043d\u0435\u0433\u043e \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c id \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u043e\u0442\u0430: \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432 \u0447\u0430\u0442\u0435.<\/p>\n<h2>\u0428\u0430\u0433 3. \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430. \u0412 <code>__init__.py<\/code> \u044f \u0441\u043e\u0437\u0434\u0430\u044e \u0441\u0430\u043c\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 FastAPI. \u0423\u0440\u043b\u044b, \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u043d\u0430 <code>auth\/<\/code>, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u0432\u044b\u0448\u0435.<\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f middleware \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443. \u041e\u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u0440\u043e\u0441 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443:<\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0443\u0440\u043b \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \/auth\/, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0435\u0442 \u043a\u0443\u043a\u0438 \u0438\u043b\u0438 \u0432 \u043a\u0443\u043a\u0438 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u2014 \u043e\u0442\u0434\u0430\u0451\u043c <code>login.html<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0441\u0451 \u043e\u043a, \u043e\u0442\u0434\u0430\u0451\u043c \u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0442\u0434\u0430\u0442\u044c.<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\"># app\/__init__.py  import urllib.parse  from fastapi import FastAPI from fastapi.requests import Request from fastapi.templating import Jinja2Templates from joserfc import jwt from joserfc.errors import JoseError  from src.auth import auth_router from src.vars import COOKIE_NAME, JWT_SECRET_KEY from src.whitelist import WHITELIST_IDS  app = FastAPI() templates = Jinja2Templates('src\/templates')  app.mount('\/auth', auth_router)   @app.middleware('http') async def middleware(request: Request, call_next):     response = await call_next(request)     if request.url.path.startswith('\/auth\/'):         return response      url_safe_path = urllib.parse.quote(request.url.path, safe='')     template_context = {'request': request, 'next_path': url_safe_path}     login_wall = templates.TemplateResponse('login.html', template_context)      token = request.cookies.get(COOKIE_NAME)     if not token:         return login_wall      try:         token_parts = jwt.decode(token, JWT_SECRET_KEY)     except JoseError:         return login_wall      user_id = token_parts.claims['k']     if user_id not in WHITELIST_IDS:         return login_wall      return response<\/code><\/pre>\n<h2>\u0428\u0430\u0433 4. \u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0444\u0430\u0439\u043b\u044b<\/h2>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0448\u0430\u0433 \u2014 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u0444\u0430\u0439\u043b\u044b \u0441\u0430\u0439\u0442\u0430.<\/p>\n<h3>\u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431<\/h3>\n<p>\u041d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u0441\u0442\u0440\u043e\u0447\u0435\u043a:<\/p>\n<pre><code class=\"python\"># app\/__init__.py  from fastapi.staticfiles import StaticFiles  ...  app = FastAPI() templates = Jinja2Templates('src\/templates') static_files = StaticFiles(directory='site\/')  app.mount('\/auth', auth_router) app.mount('\/', static_files, name='static')  ...<\/code><\/pre>\n<h3>\u0421\u043f\u043e\u0441\u043e\u0431 \u0441 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\u043c .html \u0438\u0437 \u0443\u0440\u043b\u043e\u0432<\/h3>\n<p>\u0425\u043e\u0441\u0442\u0438\u043d\u0433 \u043c\u043e\u0435\u0433\u043e \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0441\u0430\u0439\u0442\u0430 \u0443\u0434\u0430\u043b\u044f\u043b \u0438\u0437 \u0430\u0434\u0440\u0435\u0441\u043e\u0432 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f <code>.html<\/code>. \u041e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c, \u0447\u0442\u043e FastAPI \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438 \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u0443\u043c\u0435\u0435\u0442, \u0442\u0430\u043a \u0447\u0442\u043e \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f.<\/p>\n<p>FastAPI \u043e\u0441\u043d\u043e\u0432\u0430\u043d \u043d\u0430 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0435 Starlette, \u0438 \u043a\u043b\u0430\u0441\u0441 StaticFiles \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u0442\u0442\u0443\u0434\u0430. \u0423 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u043e\u043f\u0446\u0438\u044f <code>html=True<\/code>: \u043e\u043d\u0430 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0435 \u043f\u0443\u0442\u0438 \u0432\u0438\u0434\u0430 <code>\/path\/to\/dir\/index.html<\/code> \u0432 \u0443\u0440\u043b\u044b \u0432\u0438\u0434\u0430 <code>\/path\/to\/dir\/<\/code>. \u0423 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0432 \u0443\u0440\u043b\u0430\u0445 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f (<code>\/path\/to\/dir\/file.html<\/code>), \u0442\u0430\u043a \u0447\u0442\u043e \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441.<\/p>\n<pre><code class=\"python\"># src\/staticfiles.py  import os import typing  from fastapi.staticfiles import StaticFiles   class HTMLStaticFiles(StaticFiles):     def __init__(self, **kwargs):         super().__init__(**kwargs, html=True)      def lookup_path(             self, path: str     ) -> typing.Tuple[str, typing.Optional[os.stat_result]]:         if not path.endswith('.html'):             full_path, stat_result = super().lookup_path(path + '.html')             if stat_result:                 return full_path, stat_result          return super().lookup_path(path)<\/code><\/pre>\n<pre><code class=\"python\"># app\/__init__.py  from src.staticfiles import HTMLStaticFiles  ...  app = FastAPI() templates = Jinja2Templates('src\/templates') static_files = HTMLStaticFiles(directory='static\/')  app.mount('\/auth', auth_router) app.mount('\/', static_files, name='static')  ...<\/code><\/pre>\n<h2>\u041a\u0430\u043a \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c<\/h2>\n<p>\u041f\u0440\u043e\u0435\u043a\u0442 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u043b\u0438 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u043e\u0435 FastAPI-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435. <\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0431\u044b\u0441\u0442\u0440\u043e \u043f\u043e\u0442\u044b\u043a\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c-\u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043d\u0430 80 \u043f\u043e\u0440\u0442\u0435:<\/p>\n<pre><code class=\"bash\">sudo BOT_TOKEN=your_token_here JWT_SECRET_KEY=your_random_string uvicorn src:app --reload --port 80<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u043d\u0430\u0435\u0442\u0435, \u043a\u0430\u043a \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0438\u043b\u0438 \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u0434, \u2014 \u0431\u0443\u0434\u0443 \u0440\u0430\u0434 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0432\u0430\u0448\u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438.<\/p>\n<p><a href=\"https:\/\/github.com\/tm-a-t\/telegram-auth-wall\" rel=\"noopener noreferrer nofollow\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u043d\u0430 \u0413\u0438\u0442\u0445\u0430\u0431\u0435.<\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/801121\/\"> https:\/\/habr.com\/ru\/articles\/801121\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-371166","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/371166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=371166"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/371166\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=371166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=371166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=371166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}