{"id":471593,"date":"2025-08-22T09:00:36","date_gmt":"2025-08-22T09:00:36","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=471593"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=471593","title":{"rendered":"<span>\u041a\u043e\u0440\u043e\u0442\u043a\u043e \u043f\u0440\u043e \u0432\u0438\u0434\u044b \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/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>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430 \u0442\u0435\u043c, \u043a\u0442\u043e \u043e\u0441\u043e\u0431\u043e \u043d\u0435 \u0437\u0430\u043c\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u043b\u0441\u044f, \u043a\u0430\u043a \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0432 \u0431\u0435\u043a\u0435\u043d\u0434\u0435, \u043d\u043e \u0445\u043e\u0447\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u043e \u0432\u044a\u0435\u0445\u0430\u0442\u044c \u0432 \u0442\u0435\u043c\u0443. \u041c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u0433\u0440\u0443\u0436\u0430\u0442\u044c\u0441\u044f \u0432 \u0441\u0430\u043c\u044b\u0435 \u043d\u0435\u0434\u0440\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043d\u043e \u0442\u043e\u0447\u043d\u043e \u043f\u043e\u0439\u043c\u0435\u043c \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438 \u043a\u0430\u043a \u043f\u0440\u0438\u043a\u0440\u0443\u0442\u0438\u0442\u044c \u044d\u0442\u043e \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435. \u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c python \u0438 FastAPI, \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u0430\u043a \u043f\u0440\u043e\u0449\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c, \u043d\u043e \u044d\u0442\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0438 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u043e\u0432\/\u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u0432.<\/p>\n<blockquote>\n<p><em>\u042d\u0442\u043e \u0442\u043e\u043f \u0432\u0438\u0434\u043e\u0432 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e API \u0434\u043b\u044f \u0431\u0435\u043a\u0435\u043d\u0434 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432. \u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b, \u043d\u043e \u044d\u0442\u0438 &#8212; \u043e\u0441\u043d\u043e\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0432 99% \u0441\u043b\u0443\u0447\u0430\u0435\u0432.<\/em><\/p>\n<\/blockquote>\n<details class=\"spoiler\">\n<summary>\u0427\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0434\u0430\u043b\u044c\u0448\u0435<\/summary>\n<div class=\"spoiler__content\">\n<ul>\n<li>\n<p><strong>Basic Auth<\/strong> \u2014 \u0431\u044b\u0441\u0442\u0440\u043e, \u043f\u0440\u043e\u0441\u0442\u043e, \u043d\u043e \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043b\u043e\u0445\u0430\u044f \u0438\u0434\u0435\u044f<\/p>\n<\/li>\n<li>\n<p><strong>Session<\/strong> \u2014 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0445 HTML-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439<\/p>\n<\/li>\n<li>\n<p><strong>JWT<\/strong> \u2014 \u043b\u0443\u0447\u0448\u0438\u0439 \u0432\u044b\u0431\u043e\u0440 \u0434\u043b\u044f REST API \u0438 SPA, \u0435\u0441\u043b\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c<\/p>\n<\/li>\n<li>\n<p><strong>OAuth2<\/strong> \u2014 must-have \u0434\u043b\u044f \u0432\u0445\u043e\u0434\u0430 \u0447\u0435\u0440\u0435\u0437 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b, \u0434\u0435\u043b\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0430\u0432<\/p>\n<\/li>\n<li>\n<p><strong>Keycloak<\/strong> \u2014 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u0435\u043d, \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u0435\u043d <strong>SSO<\/strong>, <strong>MFA<\/strong>, <strong>LDAP<\/strong>, <strong>Google Login<\/strong> \u0438 <strong>\u0446\u0435\u043d\u0442\u0440\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438<\/strong><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/details>\n<blockquote>\n<p><em>\u0412\u0441\u0435 \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u043d\u0438\u0436\u0435, \u044f \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b \u0432 \u0432\u0438\u0434\u0435\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u0434\u043b\u044f \u0442\u0432\u043e\u0435\u0433\u043e \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430! \u0412\u044b\u0431\u0438\u0440\u0430\u0439 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0442\u0435\u0431\u0435 \u0432\u0438\u0434 \u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430 \u0438 \u0438\u0437\u0443\u0447\u0430\u0439!<\/em><\/p>\n<\/blockquote>\n<details class=\"spoiler\">\n<summary>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u043e\u043c \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/summary>\n<div class=\"spoiler__content\">\n<ol>\n<li>\n<p>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0437\u0443\u0447\u0438 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043f\u043e \u0442\u0435\u043c\u0435<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438 \u0434\u0435\u043c\u043e \u043f\u0440\u0438\u043c\u0435\u0440, \u044d\u0442\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u0434\u043b\u044f \u0442\u0435\u0431\u044f \u044d\u0442\u043e\u0442 \u0432\u0438\u0434 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>\u0421\u0434\u0435\u043b\u0430\u0439 \u0434\u043e\u043c\u0430\u0448\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u0435 \u0438\u0437 \u0444\u0430\u0439\u043b\u0438\u043a\u0430 tasks.md \u0432 \u044d\u0442\u043e\u0439 \u0432\u0435\u0442\u043a\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <a href=\"https:\/\/github.com\/takentui\/authorization_types\/blob\/fastapi-basic-auth\/tasks.md\" rel=\"noopener noreferrer nofollow\">tasks.md<\/a><\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u0431\u0435\u0440\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u0448\u0438\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u0441 \u0433\u0443\u0433\u043b\u043e\u043c, \u0438\u043b\u0438 \u043f\u0438\u0448\u0438 \u043c\u043d\u0435 \u0432 \u0442\u0435\u043b\u0435\u0433\u0443<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/details>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/68a5969f79b5b6ab9ace586c\" data-style=\"\" id=\"68a5969f79b5b6ab9ace586c\" width=\"\"><\/div>\n<p>\u0418\u0442\u0430\u043a \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043c \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043d\u044f\u0442\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0434\u0430\u043b\u044c\u0448\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u044f\u0437\u044b\u043a\u0435. <\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/1a6\/8d2\/943\/1a68d29438e25ba1b8e67c9ea30a2537.png\" alt=\"image.png\" title=\"\u0422\u0440\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0440\u0435\u0441\u0443\u0440\u0441\u0443\" width=\"2130\" height=\"195\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/1a6\/8d2\/943\/1a68d29438e25ba1b8e67c9ea30a2537.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/1a6\/8d2\/943\/1a68d29438e25ba1b8e67c9ea30a2537.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0422\u0440\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0440\u0435\u0441\u0443\u0440\u0441\u0443<\/figcaption><\/div>\n<\/figure>\n<h2>\u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f (Identification)<\/h2>\n<p><strong>\u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/strong> \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0435, \u0442.\u0435. \u0437\u0430\u044f\u0432\u043b\u044f\u0435\u0442, \u043a\u0442\u043e \u043e\u043d \u0435\u0441\u0442\u044c.<\/p>\n<p><strong>\u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0447\u0435\u0440\u0442\u044b:<\/strong><\/p>\n<ul>\n<li>\n<p>\u042d\u0442\u043e <strong>\u0437\u0430\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u043b\u0438\u0447\u043d\u043e\u0441\u0442\u0438<\/strong>, \u0430 \u043d\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430<\/p>\n<\/li>\n<li>\n<p>\u0421\u0430\u043c\u043e \u043f\u043e \u0441\u0435\u0431\u0435 <strong>\u043d\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u0432\u044b \u0442\u043e\u0442, \u0437\u0430 \u043a\u043e\u0433\u043e \u0441\u0435\u0431\u044f \u0432\u044b\u0434\u0430\u0451\u0442\u0435<\/strong><\/p>\n<\/li>\n<li>\n<p>\u0412\u0441\u0435\u0433\u0434\u0430 <strong>\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438<\/strong><\/p>\n<\/li>\n<\/ul>\n<p><strong>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0424\u043e\u0440\u043c\u044b \u043b\u043e\u0433\u0438\u043d\u0430 (<code>username<\/code> \u0438\u043b\u0438 <code>email<\/code>)<\/p>\n<\/li>\n<li>\n<p>JWT \u2014 \u043f\u043e\u043b\u0435 <code>sub<\/code> (subject) \u2014 \u044d\u0442\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u043a\u043e\u0439 \u0442\u043e \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0431\u0430\u043d\u043a\u043e\u0432\u0441\u043a\u0430\u044f \u043a\u0430\u0440\u0442\u0430, \u043f\u0440\u043e\u043f\u0443\u0441\u043a)<\/p>\n<\/li>\n<\/ul>\n<h2>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f (Authentication)<\/h2>\n<p><strong>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/strong> \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043b\u0438\u0447\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0432\u044b <strong>\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0442\u043e\u0442, \u0437\u0430 \u043a\u043e\u0433\u043e \u0441\u0435\u0431\u044f \u0432\u044b\u0434\u0430\u0451\u0442\u0435<\/strong>.<\/p>\n<p><strong>\u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u0430\u0440\u043e\u043b\u044c \/ Pin code<\/p>\n<\/li>\n<li>\n<p>\u0422\u043e\u043a\u0435\u043d \/ \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442<\/p>\n<\/li>\n<li>\n<p>\u041e\u0434\u043d\u043e\u0440\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043e\u0434 (OTP)<\/p>\n<\/li>\n<li>\n<p>\u0411\u0438\u043e\u043c\u0435\u0442\u0440\u0438\u044f (\u043b\u0438\u0446\u043e, \u043f\u0430\u043b\u0435\u0446)<\/p>\n<\/li>\n<\/ul>\n<h2>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f (Authorization)<\/h2>\n<p><strong>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f<\/strong> \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f, \u043a\u0430\u043a\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u043f\u043e\u0441\u043b\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438.<\/p>\n<p><strong>\u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0444\u043e\u0440\u043c\u044b:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0420\u043e\u043b\u0438 (<code>admin<\/code>, <code>user<\/code>, <code>moderator<\/code>)<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0430\u0432\u0430 (<code>read<\/code>, <code>write<\/code>, <code>delete<\/code>)<\/p>\n<\/li>\n<li>\n<p>Scopes (\u0432 OAuth2)<\/p>\n<\/li>\n<li>\n<p>ACL (access control lists)<\/p>\n<\/li>\n<\/ul>\n<p><strong>\ud83e\udde0 \u041a\u0430\u043a \u043b\u0435\u0433\u043a\u043e \u0437\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u044c:<\/strong><\/p>\n<blockquote>\n<p><strong><em>\u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f &#8212; <\/em><\/strong><em>\u044d\u0442\u043e &#171;\u043f\u043e\u043a\u0430\u0436\u0438 \u043a\u0442\u043e \u0442\u044b&#187;<\/em><strong><em>, <\/em><\/strong><br \/><strong><em>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/em><\/strong><em> \u2014 \u044d\u0442\u043e \u201c\u0434\u043e\u043a\u0430\u0436\u0438, \u0447\u0442\u043e \u0442\u044b \u2014 \u044d\u0442\u043e \u0442\u044b\u201d,<\/em><br \/><strong><em>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f<\/em><\/strong><em> \u2014 \u201c\u0447\u0442\u043e \u0442\u0435\u0431\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043e\u201d<\/em><\/p>\n<\/blockquote>\n<h3>Base64<\/h3>\n<p>\u0415\u0449\u0435 \u043e\u0434\u043d\u0430 \u0448\u0442\u0443\u043a\u0430 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430 \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c.<\/p>\n<p><strong>Base64<\/strong> \u2014 \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u0442\u0435\u043a\u0441\u0442 \u0438\u043b\u0438 \u0431\u0430\u0439\u0442\u044b) \u0432 ASCII-\u0441\u0438\u043c\u0432\u043e\u043b\u044b, \u0447\u0442\u043e\u0431\u044b \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0443 (\u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430\u0445, URL, JSON \u0438 \u0442.\u043f.).<\/p>\n<blockquote>\n<p><em>\u041d\u0435 \u0437\u0430\u0449\u0438\u0449\u0430\u0435\u0442, \u043d\u0435 \u0448\u0438\u0444\u0440\u0443\u0435\u0442, \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0431\u0430\u0439\u0442\u044b \u043f\u0440\u0438\u0433\u043e\u0434\u043d\u044b\u043c\u0438 \u0434\u043b\u044f HTTP.<\/em><\/p>\n<\/blockquote>\n<p><strong>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043d\u0430\u0431\u043e\u0440 <strong>\u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 A-Z, a-z, 0-9, + \u0438 \/<\/strong>.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u0445\u043e\u0434 \u0432\u0441\u0435\u0433\u0434\u0430 <strong>\u0441\u0442\u0440\u043e\u043a\u0430<\/strong>, \u043f\u0440\u0438\u0433\u043e\u0434\u043d\u0430\u044f \u0434\u043b\u044f HTTP \u0438 JSON.<\/p>\n<\/li>\n<li>\n<p>\u0427\u0430\u0441\u0442\u043e \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 <code>=<\/code> (\u043f\u0430\u0434\u0434\u0438\u043d\u0433 \u0434\u043b\u044f \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u044f).<\/p>\n<\/li>\n<\/ul>\n<h4>\ud83e\uddd1\u200d\ud83d\udcbb \u041f\u0440\u0438\u043c\u0435\u0440\u044b:<\/h4>\n<pre><code class=\"python\"># \u041a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438: import base64  text = \"hello:world\" encoded = base64.b64encode(text.encode()).decode() print(encoded)  # aGVsbG86d29ybGQ=  # \u0414\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435: decoded = base64.b64decode(encoded).decode() print(decoded)  # hello:world <\/code><\/pre>\n<p><strong>\u26a0\ufe0f \u0412\u0430\u0436\u043d\u043e:<\/strong><\/p>\n<ul>\n<li>\n<p><strong>Base64 \u2014 \u043d\u0435 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435!<\/strong><br \/><em>\u041b\u044e\u0431\u043e\u0439 \u043c\u043e\u0436\u0435\u0442 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e. \u042d\u0442\u043e \u043d\u0435 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c, \u0430 \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438.<\/em><\/p>\n<\/li>\n<li>\n<p><strong>\u0414\u043b\u044f \u0437\u0430\u0449\u0438\u0442\u044b<\/strong> (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 JWT) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <strong>\u043f\u043e\u0434\u043f\u0438\u0441\u044c<\/strong>, \u0430 \u043d\u0435 base64.<\/p>\n<\/li>\n<\/ul>\n<h2>HTTP<\/h2>\n<p>\u0412\u0441\u0435 \u0447\u0442\u043e \u043d\u0430\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u0443\u0442 \u0437\u043d\u0430\u0442\u044c &#8212; \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b HTTP \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 <em>\u043a\u043b\u0438\u0435\u043d\u0442-\u0441\u0435\u0440\u0432\u0435\u0440<\/em>. \u041a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0435\u0433\u043e \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441, \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 \u0435\u0433\u043e \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0443.<\/p>\n<p>\u0423 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0435\u0441\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 (headers) \u0438 \u043c\u044b \u0432 \u043d\u0438\u0445 \u043c\u043e\u0436\u0435\u043c \u0447\u0442\u043e \u0442\u043e \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0414\u0430\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u0438 \u0447\u0442\u043e \u043c\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c.<\/p>\n<h2>Basic auth<\/h2>\n<p><strong>Basic Auth<\/strong> \u2014 \u044d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0432 \u043a\u0430\u0436\u0434\u043e\u043c HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0432 \u0432\u0438\u0434\u0435 base64-\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438.<\/p>\n<p>\u0418\u0434\u0435\u0430\u043b\u044c\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 <strong>\u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0438 \u0431\u044b\u0441\u0442\u0440\u043e<\/strong>, \u043d\u043e <strong>\u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0435\u0433\u043e \u0432 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0431\u0435\u0437 HTTPS.<\/strong><\/p>\n<blockquote>\n<p>Basic Auth \u2014 \u044d\u0442\u043e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0437\u043c \u0432 \u0447\u0438\u0441\u0442\u043e\u043c \u0432\u0438\u0434\u0435.<br \/><em>\u042d\u0442\u043e \u043d\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u201c\u043d\u0430 \u0433\u043e\u0434\u044b\u201d \u2014 \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u201c\u0432\u043e\u0442\u043a\u043d\u0443\u0442\u044c \u043b\u043e\u0433\u0438\u043d \u0437\u0430 30 \u0441\u0435\u043a\u0443\u043d\u0434\u201d.<\/em><\/p>\n<\/blockquote>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e45\/48c\/ebd\/e4548cebdbd3548b42590d14de4a804a.png\" alt=\"image.png\" title=\"\u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\" width=\"1208\" height=\"226\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/e45\/48c\/ebd\/e4548cebdbd3548b42590d14de4a804a.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e45\/48c\/ebd\/e4548cebdbd3548b42590d14de4a804a.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/figcaption><\/div>\n<\/figure>\n<h4>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/h4>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u0432\u043e\u0434\u0438\u0442 \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c<\/p>\n<\/li>\n<li>\n<p>\u042d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0432 base64: <code>username:password<\/code><br \/>Base64 <strong>\u043d\u0435 \u0448\u0438\u0444\u0440\u0443\u0435\u0442<\/strong>, \u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442 \u2192 \u043d\u0443\u0436\u0435\u043d HTTPS<\/p>\n<\/li>\n<li>\n<p>\u0412 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a:<\/p>\n<pre><code>Authorization: Basic YWRtaW46c2VjcmV0=<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043b\u043e\u0433\u0438\u043d\/\u043f\u0430\u0440\u043e\u043b\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0438\u043b\u0438 \u0447\u0435\u0440\u0435\u0437 middleware<\/p>\n<blockquote>\n<p><strong><em>Middleware<\/em><\/strong><em> \u2014 \u044d\u0442\u043e <\/em><strong><em>\u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0439 \u0441\u043b\u043e\u0439<\/em><\/strong><em>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 <\/em><strong><em>\u0434\u043e<\/em><\/strong><em> (\u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435) \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/em><\/p>\n<\/blockquote>\n<\/li>\n<\/ol>\n<h4>\u2705 \u041f\u043b\u044e\u0441\u044b:<\/h4>\n<ul>\n<li>\n<p>\u041d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0437\u0430 1 \u043c\u0438\u043d\u0443\u0442\u0443 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 <code><strong>Nginx<\/strong><\/code> \u0438\u043b\u0438 <code><strong>FastAPI<\/strong><\/code>)<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043a\u0443\u043a\u0438, \u0431\u0430\u0437\u044b \u0441\u0435\u0441\u0441\u0438\u0439, \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u043e\u043a\u0435\u043d\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442 HTTP \u0441 1990-\u0445, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u0445<\/p>\n<\/li>\n<\/ul>\n<h4>\u274c \u041c\u0438\u043d\u0443\u0441\u044b:<\/h4>\n<ul>\n<li>\n<p>\u0414\u0430\u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u043b\u043e\u0433\u0438\u043d\u0430 \u2014 \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u0443\u0445\u043e\u0434\u044f\u0442 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435<\/p>\n<\/li>\n<li>\n<p>\u201c\u0412\u044b\u0439\u0442\u0438\u201d \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u2014 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0447\u0438\u0441\u0442\u0438\u0432 \u043a\u044d\u0448 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430 \u0438\u043b\u0438 \u0442\u043e\u043a\u0435\u043d<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0445\u0432\u0430\u0442 \u0442\u0440\u0430\u0444\u0438\u043a\u0430 = \u043a\u0440\u0430\u0436\u0430 \u043f\u0430\u0440\u043e\u043b\u044f<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u0442 \u0440\u043e\u043b\u0435\u0439, scopes, \u0441\u0435\u0441\u0441\u0438\u0439<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u0442 revoke, \u043b\u043e\u0433\u043e\u0432, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044f, \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0438<\/p>\n<\/li>\n<\/ul>\n<h4>\u0413\u0434\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u043e:<\/h4>\n<p>\u2705 <strong>\u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442<\/strong>:<\/p>\n<ul>\n<li>\n<p>\u0410\u0434\u043c\u0438\u043d-\u043f\u0430\u043d\u0435\u043b\u0438 \u201c\u0434\u043b\u044f \u0441\u0432\u043e\u0438\u0445\u201d<\/p>\n<\/li>\n<li>\n<p>\u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b (\u0437\u0430 VPN)<\/p>\n<\/li>\n<li>\n<p>\u0411\u044b\u0441\u0442\u0440\u044b\u0435 MVP, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 API<\/p>\n<\/li>\n<\/ul>\n<p>\u274c <strong>\u041d\u0435<\/strong> <strong>\u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442<\/strong>:<\/p>\n<ul>\n<li>\n<p>\u041f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0435 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>SPA \u0438 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u044b<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0434\u0430\u043a\u0448\u043d \u0431\u0435\u0437 HTTPS<\/p>\n<\/li>\n<\/ul>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432 FastAPI:<\/h4>\n<p><a href=\"https:\/\/github.com\/takentui\/authorization_types\/tree\/fastapi-basic-auth\" rel=\"noopener noreferrer nofollow\">\u0414\u0435\u043c\u043e \u043f\u0440\u0438\u043c\u0435\u0440<\/a> <br \/><a href=\"https:\/\/fastapi.tiangolo.com\/advanced\/security\/http-basic-auth\/?h=basic+auth\" rel=\"noopener noreferrer nofollow\">\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f<\/a><\/p>\n<pre><code class=\"python\">from fastapi import FastAPI, Depends from fastapi.security import HTTPBasic, HTTPBasicCredentials  app = FastAPI() security = HTTPBasic()  @app.get(\"\/secret-resource\") def read_secret(credentials: HTTPBasicCredentials = Depends(security)):     if credentials.username == \"admin\" and credentials.password == \"secret\":         return {\"message\": \"Welcome!\"}     return {\"error\": \"Unauthorized\"}<\/code><\/pre>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/68f\/aad\/658\/68faad6587803f9a79a588f1256dd74d.png\" alt=\"UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439\" title=\"UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439\" width=\"1754\" height=\"1328\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/68f\/aad\/658\/68faad6587803f9a79a588f1256dd74d.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/68f\/aad\/658\/68faad6587803f9a79a588f1256dd74d.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439<\/figcaption><\/div>\n<\/figure>\n<h3>\u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0432 NGINX<\/h3>\n<p>\u26a0\ufe0f\u00a0<em>\u042d\u0442\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0447\u0430\u0441\u0442\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f basic auth<\/em><\/p>\n<p><a href=\"https:\/\/github.com\/takentui\/authorization_types\/tree\/nginx-basic-auth\" rel=\"noopener noreferrer nofollow\">\u0414\u0435\u043c\u043e \u043f\u0440\u0438\u043c\u0435\u0440<\/a><br \/><a href=\"https:\/\/docs.nginx.com\/nginx\/admin-guide\/security-controls\/configuring-http-basic-authentication\/\" rel=\"noopener noreferrer nofollow\">\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f<\/a><\/p>\n<blockquote>\n<p>Nginx \u043c\u043e\u0436\u0435\u0442 \u0441\u0430\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u0434\u043e \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u043f\u0430\u0434\u0451\u0442 \u0432 backend (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, FastAPI, Django \u0438 \u0442.\u0434.).<\/p>\n<\/blockquote>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b1b\/717\/06a\/b1b71706a72aa2f5540196e5aa0b9615.png\" alt=\"image.png\" title=\"\u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 basic-auth \u0432\u043c\u0435\u0441\u0442\u0435 c nginx\" width=\"2166\" height=\"834\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/b1b\/717\/06a\/b1b71706a72aa2f5540196e5aa0b9615.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b1b\/717\/06a\/b1b71706a72aa2f5540196e5aa0b9615.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 basic-auth \u0432\u043c\u0435\u0441\u0442\u0435 c nginx<\/figcaption><\/div>\n<\/figure>\n<p>\u042d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e:<\/p>\n<ul>\n<li>\n<p>\u0417\u0430\u0449\u0438\u0442\u0438\u0442\u044c dev-\u0441\u0435\u0440\u0432\u0435\u0440, staging \u0438\u043b\u0438 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 API<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u201c\u0437\u0430\u0433\u043b\u0443\u0448\u043a\u0443\u201d \u043d\u0430 admin-\u043f\u0430\u043d\u0435\u043b\u044c<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435 \u043b\u0435\u0437\u0442\u044c \u0432 \u043a\u043e\u0434 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432\u043e\u043e\u0431\u0449\u0435<\/p>\n<\/li>\n<\/ul>\n<h4>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/h4>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0437\u0430\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u0437\u0430\u0449\u0438\u0449\u0451\u043d\u043d\u044b\u0439 URL<\/p>\n<\/li>\n<li>\n<p>\u0411\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043e\u043a\u043d\u043e \u0432\u0432\u043e\u0434\u0430 \u043b\u043e\u0433\u0438\u043d\u0430\/\u043f\u0430\u0440\u043e\u043b\u044f<\/p>\n<\/li>\n<li>\n<p>Nginx \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0438\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <strong>\u0444\u0430\u0439\u043b\u0430 <\/strong><code><strong>.htpasswd<\/strong><\/code><\/p>\n<\/li>\n<li>\n<p>\u0422\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043e\u0442\u0434\u0430\u0451\u0442 \u0434\u043e\u0441\u0442\u0443\u043f<\/p>\n<\/li>\n<\/ol>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/029\/88e\/d46\/02988ed468246b6d715bca80eac5cca5.png\" alt=\"UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439\" title=\"UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439\" width=\"1578\" height=\"1382\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/029\/88e\/d46\/02988ed468246b6d715bca80eac5cca5.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/029\/88e\/d46\/02988ed468246b6d715bca80eac5cca5.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439<\/figcaption><\/div>\n<\/figure>\n<h4>\u0412\u0430\u0436\u043d\u043e:<\/h4>\n<ul>\n<li>\n<p>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <strong>HTTPS<\/strong> \u2014 \u0438\u043d\u0430\u0447\u0435 \u043b\u043e\u0433\u0438\u043d\/\u043f\u0430\u0440\u043e\u043b\u044c \u043b\u0435\u0442\u044f\u0442 \u0432 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u043c \u0432\u0438\u0434\u0435<\/p>\n<\/li>\n<li>\n<p>\u041e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043b\u043e\u0433\u0438\u043d\/\u043f\u0430\u0440\u043e\u043b\u044c \u0443 \u0432\u0441\u0435\u0445 (\u0431\u0435\u0437 \u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f)<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u0442 \u0441\u0435\u0441\u0441\u0438\u0439, \u0440\u043e\u043b\u0435\u0439, \u0440\u0430\u0437\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u2014 \u0442\u043e\u043b\u044c\u043a\u043e &#171;\u0434\u043e\u043f\u0443\u0449\u0435\u043d \u0438\u043b\u0438 \u043d\u0435\u0442&#187;<\/p>\n<\/li>\n<\/ul>\n<h2>Session auth<\/h2>\n<p><a href=\"https:\/\/github.com\/takentui\/authorization_types\/tree\/cookie-auth\" rel=\"noopener noreferrer nofollow\">\u0414\u0435\u043c\u043e \u043f\u0440\u0438\u043c\u0435\u0440<\/a><\/p>\n<p>\u042d\u0442\u043e \u0441\u0442\u0430\u0440\u044b\u0439, \u043f\u0440\u043e\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 <strong>\u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445<\/strong> (<strong>Django<\/strong>, <strong>Flask<\/strong>, <strong>Rails<\/strong> \u0438 \u0434\u0440.)<\/p>\n<p>\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b session based auth<\/p>\n<ol>\n<li>\n<p><strong>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043b\u043e\u0433\u0438\u043d\u0438\u0442\u0441\u044f<\/strong> \u2014 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043b\u043e\u0433\u0438\u043d\/\u043f\u0430\u0440\u043e\u043b\u044c<\/p>\n<p>\u2192 <code>POST \/login<\/code> \u0441 <code>{\"username\": \"...\", \"password\": \"...\"}<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0435\u0440\u0432\u0435\u0440 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0438\u0445<\/strong> \u0438 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 <strong>\u0441\u0435\u0441\u0441\u0438\u044e<\/strong><\/p>\n<p>\u2192 \u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438\u043b\u0438 \u0431\u0430\u0437\u0435 \u0441\u0432\u044f\u0437\u044c \u0441\u0435\u0441\u0441\u0438\u0438 \u0441 \u0443\u0447\u0435\u0442\u043a\u043e\u0439<\/p>\n<\/li>\n<li>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 <strong>\u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u0442 \u0441\u0435\u0441\u0441\u0438\u0438 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 ID<\/strong> (\u043e\u0431\u044b\u0447\u043d\u043e \u2014 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430)<\/p>\n<p>\u2192 \u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c session_id<\/p>\n<\/li>\n<li>\n<p>\u042d\u0442\u043e\u0442 ID \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0432 <strong>cookie<\/strong><\/p>\n<p>\u2192 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440,<\/p>\n<pre><code class=\"python\">HTTP\/1.1 200 OK Set-Cookie: sessionid=abc123; HttpOnly; Path=\/; Secure<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 <strong>\u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438<\/strong> \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 cookie<\/p>\n<p>\u2192 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440<\/p>\n<pre><code class=\"python\">GET \/profile HTTP\/1.1 Host: example.com Cookie: sessionid=abc123<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u043f\u043e ID \u043d\u0430\u0445\u043e\u0434\u0438\u0442 \u0441\u0435\u0441\u0441\u0438\u044e \u0438 \u201c\u0443\u0437\u043d\u0430\u0451\u0442\u201d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/p>\n<p>\u2192 \u0441\u0432\u0435\u0440\u044f\u0435\u043c \u0441\u0435\u0441\u0441\u0438\u044e \u0432 \u0431\u0430\u0437\u0435 \u0438 \u0432\u044b\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c \u0443\u0447\u0435\u0442\u043a\u0443 \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u0430 \u0441\u0435\u0441\u0441\u0438\u044f<\/p>\n<\/li>\n<\/ol>\n<h4>\u2705 \u041f\u043b\u044e\u0441\u044b<\/h4>\n<ul>\n<li>\n<p><strong>\u041f\u0440\u043e\u0441\u0442\u043e\u0442\u0430 \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f:<\/strong> \u043b\u0435\u0433\u043a\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0430 \u043c\u043d\u043e\u0433\u0438\u0445 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430\u0445<\/p>\n<\/li>\n<li>\n<p><strong>\u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c:<\/strong> \u0441\u0435\u0441\u0441\u0438\u043e\u043d\u043d\u044b\u0439 ID \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435, \u0442\u043e\u043a\u0435\u043d \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0447\u0443\u0432\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0440\u043e\u0441\u0442\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435:<\/strong> \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0430\u043d\u043d\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0435\u0441\u0441\u0438\u0438, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438<\/p>\n<\/li>\n<li>\n<p><strong>\u041c\u0435\u043d\u044c\u0448\u0430\u044f \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442:<\/strong> \u0432\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435<\/p>\n<\/li>\n<li>\n<p><strong>\u0423\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u043e\u0441\u0442\u044c \u043a XSS-\u0430\u0442\u0430\u043a\u0430\u043c:<\/strong> \u043f\u0440\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441 HttpOnly cookies<\/p>\n<\/li>\n<\/ul>\n<h4>\u274c \u041c\u0438\u043d\u0443\u0441\u044b<\/h4>\n<ul>\n<li>\n<p><strong>\u0425\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f:<\/strong> \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0435\u0441\u0441\u0438\u0439 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 (\u043f\u0430\u043c\u044f\u0442\u044c\/\u0411\u0414\/Redis)<\/p>\n<\/li>\n<li>\n<p><strong>\u041c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u044c:<\/strong> \u0441\u043b\u043e\u0436\u043d\u0435\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043c\u0435\u0436\u0434\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u043c\u0438<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441 CORS:<\/strong> \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u043f\u0440\u0438 \u043a\u0440\u043e\u0441\u0441-\u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445<\/p>\n<\/li>\n<li>\n<p><strong>\u0423\u044f\u0437\u0432\u0438\u043c\u043e\u0441\u0442\u044c \u043a CSRF-\u0430\u0442\u0430\u043a\u0430\u043c:<\/strong> \u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0440\u044b \u0437\u0430\u0449\u0438\u0442\u044b<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c:<\/strong> \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0443 \u0441\u0435\u0441\u0441\u0438\u0439<\/p>\n<\/li>\n<\/ul>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432 FastAPI:<\/h4>\n<p>\u0412\u043e\u0442 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 FastAPI:<\/p>\n<blockquote>\n<p>\u042d\u0442\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u0435\u0441\u0441\u0438\u043e\u043d\u043d\u043e\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c FastAPI \u0438 Redis \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0435\u0441\u0441\u0438\u0439.<\/p>\n<\/blockquote>\n<p><strong>\u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b:<\/strong><\/p>\n<ol>\n<li>\n<p>\u0421\u0435\u0441\u0441\u0438\u043e\u043d\u043d\u044b\u0439 ID \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a <code>UUID<\/code> \u0438 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 <code>Redis<\/code> \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0436\u0438\u0437\u043d\u0438<\/p>\n<\/li>\n<li>\n<p>\u041a\u0443\u043a\u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0441 \u0444\u043b\u0430\u0433\u0430\u043c\u0438 <code>HttpOnly<\/code> \u0438 <code>SameSite<\/code> \u0434\u043b\u044f \u0437\u0430\u0449\u0438\u0442\u044b<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u044b: \u0432\u0445\u043e\u0434, \u0432\u044b\u0445\u043e\u0434 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0441\u0435\u0441\u0441\u0438\u044f \u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0437 <code>Redis<\/code> \u0438 \u043a\u0443\u043a\u0438 \u043e\u0447\u0438\u0449\u0430\u044e\u0442\u0441\u044f<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"python\">from fastapi import FastAPI, Depends, HTTPException, status, Response, Request from fastapi.security import HTTPBasic, HTTPBasicCredentials from pydantic import BaseModel import uuid import redis from typing import Optional  app = FastAPI() security = HTTPBasic()  # \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a Redis r = redis.Redis(host='localhost', port=6379, db=0) SESSION_EXPIRE = 3600  # 1 \u0447\u0430\u0441  # \u041c\u043e\u0434\u0435\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 class User(BaseModel):     username: str     password: str  class UserInDB(User):     hashed_password: str  # \u0417\u0430\u0433\u043b\u0443\u0448\u043a\u0430 \u0434\u043b\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 fake_users_db = {     \"user1\": {         \"username\": \"user1\",         \"hashed_password\": \"password1\"  # \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u0440\u043e\u043b\u0435\u0439     } }  # \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0443\u0447\u0435\u0442\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 def get_current_user(credentials: HTTPBasicCredentials = Depends(security)):     user = fake_users_db.get(credentials.username)     if not user or user[\"hashed_password\"] != credentials.password:         raise HTTPException(             status_code=status.HTTP_401_UNAUTHORIZED,             detail=\"\u041d\u0435\u0432\u0435\u0440\u043d\u044b\u0435 \u0443\u0447\u0435\u0442\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\",             headers={\"WWW-Authenticate\": \"Basic\"},         )     return user  # \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u0441\u0435\u0441\u0441\u0438\u0438 def get_user_by_session(request: Request):     session_id = request.cookies.get(\"session_id\")     if not session_id:         return None          username = r.get(f\"session:{session_id}\")     if not username:         return None          return fake_users_db.get(username.decode())  # \u041c\u0430\u0440\u0448\u0440\u0443\u0442\u044b @app.post(\"\/login\") def login(response: Response, user: User = Depends(get_current_user)):     # \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0435\u0441\u0441\u0438\u0438     session_id = str(uuid.uuid4())     r.setex(f\"session:{session_id}\", SESSION_EXPIRE, user[\"username\"])          # \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 cookie     response.set_cookie(         key=\"session_id\",         value=session_id,         httponly=True,         max_age=SESSION_EXPIRE,         samesite=\"lax\",         secure=True  # \u0412 production \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c True     )          return {\"message\": \"\u0423\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u0432\u0445\u043e\u0434\"}  @app.get(\"\/me\") def read_user(user: Optional[dict] = Depends(get_user_by_session)):     if not user:         raise HTTPException(             status_code=status.HTTP_401_UNAUTHORIZED,             detail=\"\u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f\"         )     return {\"username\": user[\"username\"]}  @app.post(\"\/logout\") def logout(response: Response, request: Request):     session_id = request.cookies.get(\"session_id\")     if session_id:         r.delete(f\"session:{session_id}\")          response.delete_cookie(key=\"session_id\")     return {\"message\": \"\u0412\u044b\u0445\u043e\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d \u0443\u0441\u043f\u0435\u0448\u043d\u043e\"} <\/code><\/pre>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/4ef\/b86\/e7d\/4efb86e7d6f4528e6604444c7b4fc614.png\" alt=\"UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439\" title=\"UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439\" width=\"1322\" height=\"1404\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/4ef\/b86\/e7d\/4efb86e7d6f4528e6604444c7b4fc614.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/4ef\/b86\/e7d\/4efb86e7d6f4528e6604444c7b4fc614.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439<\/figcaption><\/div>\n<\/figure>\n<h2>Token based auth<\/h2>\n<h4>\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b token-based \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 (\u0411\u0435\u0437 refresh token)<\/h4>\n<ol>\n<li>\n<p><strong>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c<\/strong><\/p>\n<p>\u2192 <code>POST \/login<\/code> \u0441 <code>{\"username\": \"...\", \"password\": \"...\"}<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0435\u0440\u0432\u0435\u0440 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0443\u0447\u0451\u0442\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/strong><\/p>\n<p>\u2192 \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u0441 \u043b\u043e\u0433\u0438\u043d-\u043f\u0430\u0440\u043e\u043b\u0435\u043c \u0438\u0437 \u0431\u0430\u0437\u044b\/\u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430.<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0435\u0440\u0432\u0435\u0440 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 JWT access token<\/strong><\/p>\n<p>\u2192 \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u0437 \u043f\u0443\u043d\u043a\u0442\u0430 2. \u041f\u043e \u0441\u0443\u0442\u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u0438 <strong>\u043d\u0435 \u0445\u0440\u0430\u043d\u0438\u0442 \u0435\u0435<\/strong>, \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u0430\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043c\u0435\u0442\u043e\u0434\u0430<\/p>\n<\/li>\n<li>\n<p><strong>\u0422\u043e\u043a\u0435\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e<\/strong><\/p>\n<p>\u2192 \u0412 \u0442\u0435\u043b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 \u0438\u043b\u0438 <code>HttpOnly<\/code> cookie<\/p>\n<\/li>\n<li>\n<p><strong>\u041a\u043b\u0438\u0435\u043d\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0442\u043e\u043a\u0435\u043d<\/strong><\/p>\n<p>\u2192 \u0412 localStorage (\u274c\u00a0\u043d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e) \u0438\u043b\u0438 \u0432 cookie (\u2705\u00a0\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e)<\/p>\n<blockquote>\n<p><strong><em>LocalStorage<\/em><\/strong><em> \u2014 \u044d\u0442\u043e \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 <\/em><code><em>\u043a\u043b\u044e\u0447:\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/em><\/code><\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p><strong>\u0414\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438\u0434\u0443\u0442 \u0441 \u0442\u043e\u043a\u0435\u043d\u043e\u043c<\/strong><\/p>\n<p>\u2192 \u0412 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435: <code>Authorization: Bearer &lt;token&gt;<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0435\u0440\u0432\u0435\u0440 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043f\u043e\u0434\u043f\u0438\u0441\u044c \u0438 <\/strong><code><strong>exp<\/strong><\/code><strong> \u0442\u043e\u043a\u0435\u043d\u0430<\/strong><\/p>\n<p>\u2192 \u0415\u0441\u043b\u0438 \u0432\u0441\u0451 \u043e\u043a \u2014 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d<\/p>\n<\/li>\n<li>\n<p><strong>\u0415\u0441\u043b\u0438 exp \u043f\u0440\u043e\u0448\u0435\u043b, \u0437\u043d\u0430\u0447\u0438\u0442 \u0442\u043e\u043a\u0435\u043d \u0431\u043e\u043b\u0435\u0435 \u043d\u0435\u0432\u0430\u043b\u0438\u0434\u0435\u043d.<\/strong><\/p>\n<p>\u2192 \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u043d\u0430 \u043f\u0443\u043d\u043a\u0442 1.<\/p>\n<\/li>\n<\/ol>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/6ad\/133\/5e0\/6ad1335e0df3d739d3bc8425874d0951.png\" alt=\"\u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u0441\u0445\u0435\u043c\u0430 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0441 \u0442\u043e\u043a\u0435\u043d\u043e\u043c\" title=\"\u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u0441\u0445\u0435\u043c\u0430 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0441 \u0442\u043e\u043a\u0435\u043d\u043e\u043c\" width=\"2087\" height=\"660\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/6ad\/133\/5e0\/6ad1335e0df3d739d3bc8425874d0951.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/6ad\/133\/5e0\/6ad1335e0df3d739d3bc8425874d0951.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u0441\u0445\u0435\u043c\u0430 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0441 \u0442\u043e\u043a\u0435\u043d\u043e\u043c<\/figcaption><\/div>\n<\/figure>\n<p><em>\u0422\u043e\u043a\u0435\u043d \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u044e\u0431\u043e\u0439, \u043b\u0438\u0448\u044c \u0431\u044b \u0442\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u043b\u043e\u0433\u0438\u043a\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0438\u043d\u0433\u0430. \u041d\u041e, \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f JWT \u0442\u043e\u043a\u0435\u043d, \u0442\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0442\u0430\u043b\u043e \u201c\u043f\u043e\u043d\u044f\u0442\u043d\u044b\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u043c\u201d \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438.<\/em><\/p>\n<h3>JWT<\/h3>\n<p><a href=\"https:\/\/jwt.io\/\" rel=\"noopener noreferrer nofollow\">https:\/\/jwt.io\/<\/a> &#8212; \u043e\u0444 \u0434\u043e\u043a\u0430, <em>\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u044e!<\/em><br \/><a href=\"https:\/\/github.com\/takentui\/authorization_types\/tree\/jwt-auth\" rel=\"noopener noreferrer nofollow\">\u0414\u0435\u043c\u043e \u043f\u0440\u0438\u043c\u0435\u0440<\/a><\/p>\n<p><strong>JWT (JSON Web Token)<\/strong> \u2014 \u044d\u0442\u043e \u0442\u043e\u043a\u0435\u043d, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u0442\u0440\u0451\u0445 \u0447\u0430\u0441\u0442\u0435\u0439, \u0440\u0430\u0437\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0445 \u0442\u043e\u0447\u043a\u0430\u043c\u0438 <code>header.payload.signature<\/code><\/p>\n<p>\u041a\u0430\u0436\u0434\u0430\u044f \u0447\u0430\u0441\u0442\u044c:<\/p>\n<ol>\n<li>\n<p><strong>Header<\/strong> \u2014 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435: \u0442\u0438\u043f \u0442\u043e\u043a\u0435\u043d\u0430 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u0438<\/p>\n<\/li>\n<li>\n<p><strong>Payload<\/strong> \u2014 \u0434\u0430\u043d\u043d\u044b\u0435 (claims): \u043a\u0442\u043e \u0432\u044b, \u0441\u0440\u043e\u043a \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f, \u0440\u043e\u043b\u0438 \u0438 \u0442.\u0434.<\/p>\n<\/li>\n<li>\n<p><strong>Signature<\/strong> \u2014 \u0446\u0438\u0444\u0440\u043e\u0432\u0430\u044f \u043f\u043e\u0434\u043f\u0438\u0441\u044c (\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043f\u043e\u0434\u043b\u0438\u043d\u043d\u043e\u0441\u0442\u0438) &lt;\/aside&gt;<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0442\u043e\u043a\u0435\u043d\u0430 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435 <code>HTTP<\/code> <code>Authentication: Bearer {your_token_here}<\/code><\/p>\n<blockquote>\n<p>Base64 \u0434\u0435\u043b\u0430\u0435\u0442 \u0438\u0445 <strong>\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u043c\u0438 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u043e HTTP<\/strong>:<\/p>\n<ul>\n<li>\n<p>\u0411\u0435\u0437 \u0441\u043f\u0435\u0446\u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 (<code>{}<\/code>, <code>\"<\/code>, <code>:<\/code>)<\/p>\n<\/li>\n<li>\n<p>\u0411\u0435\u0437 \u043f\u0440\u043e\u0431\u0435\u043b\u043e\u0432, \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u0422\u043e\u043b\u044c\u043a\u043e ASCII: \u0443\u0434\u043e\u0431\u043d\u043e \u0432 URL, \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430\u0445<\/p>\n<\/li>\n<\/ul>\n<\/blockquote>\n<h4>\u041a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f JWT<\/h4>\n<p>\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u043e\u043a\u0435\u043d\u0430<\/p>\n<pre><code>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.KMUFsIDTnFmyG3nMiGM6H9FNFUROf3wh7SmqJp-QV30<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439 \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u043f\u043e\u0442\u043e\u043c \u043f\u043e\u0439\u043c\u0435\u043c \u043a\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0442\u043e\u043a\u0435\u043d.<\/p>\n<p><code>Header:<\/code><\/p>\n<pre><code class=\"json\">{ \"alg\": \"HS256\", \"typ\": \"JWT\" }<\/code><\/pre>\n<p><code>Payload:<\/code><\/p>\n<pre><code class=\"json\">{ \"sub\": \"1234567890\", # user uid \"name\": \"John Doe\", # user name (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u0444\u0440\u043e\u043d\u0442\u0430) \"admin\": true, # \u0440\u043e\u043b\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \"iat\": 1516239022, # \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u0433\u0434\u0430 \u0442\u043e\u043a\u0435\u043d \u0431\u044b\u043b \u0432\u044b\u043f\u0443\u0449\u0435\u043d \u0441\u0435\u043a\u0443\u043d\u0434\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 01-01-1970 \"exp\": 1716666000 # \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u0433\u0434\u0430 \u0442\u043e\u043a\u0435\u043d \"\u043f\u0440\u043e\u0442\u0443\u0445\u043d\u0435\u0442\"  }<\/code><\/pre>\n<p><code>Signature:<\/code><\/p>\n<p>\u041f\u043e\u0434\u043f\u0438\u0441\u044c. \u042d\u0442\u043e \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u0448\u0442\u0443\u043a\u0430, \u043c\u043e\u0436\u043d\u043e \u0438 \u0431\u0435\u0437 \u043d\u0435\u0435, \u043d\u043e \u0442\u043e\u0433\u0434\u0430 \u043d\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043a\u0442\u043e \u0432\u044b\u043f\u0443\u0441\u0442\u0438\u043b \u0442\u043e\u043a\u0435\u043d (\u0441\u043c. \u043d\u0438\u0436\u0435)<\/p>\n<pre><code class=\"json\">a-string-secret-at-least-256-bits-long<\/code><\/pre>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u043e\u043a\u0435\u043d\u0430:<\/strong><\/p>\n<p><em>\u043c\u043e\u0436\u0435\u0448\u044c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0432 \u043f\u0430\u043a\u0435\u0442 <\/em><code><em>pyJWT<\/em><\/code><\/p>\n<pre><code class=\"python\">import jwt from datetime import datetime, timedelta  SECRET_KEY = \"your-secret-key\" ALGORITHM = \"HS256\"  def generate_token(data: dict):     payload = data.copy()     payload[\"exp\"] = datetime.utcnow() + timedelta(minutes=15)     return jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM)  # \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f token = generate_token({\"sub\": \"user_id_123\"}) print(token)<\/code><\/pre>\n<p>\ud83d\udd39 <strong>\u0427\u0442\u043e \u043a\u043b\u0430\u0434\u0443\u0442 \u0432 payload? (\u0422\u0430\u043a \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0435 claims)<\/strong><\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p align=\"left\"><strong>Claim<\/strong><\/p>\n<\/th>\n<th>\n<p align=\"left\"><strong>\u041d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/strong><\/p>\n<\/th>\n<th>\n<p align=\"left\"><strong>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439?<\/strong><\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">sub<\/p>\n<\/td>\n<td>\n<p align=\"left\">ID \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2705 \u0414\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">exp<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0412\u0440\u0435\u043c\u044f \u0438\u0441\u0442\u0435\u0447\u0435\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430 (timestamp)<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2705 \u0414\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">iat<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041a\u043e\u0433\u0434\u0430 \u0431\u044b\u043b \u0432\u044b\u043f\u0443\u0449\u0435\u043d<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u26a0\ufe0f \u0416\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u043e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">nbf<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u201c\u041d\u0435 \u0440\u0430\u043d\u044c\u0448\u0435 \u0447\u0435\u043c\u201d (not before)<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u274c \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">iss<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041a\u0442\u043e \u0432\u044b\u0434\u0430\u043b \u0442\u043e\u043a\u0435\u043d (issuer)<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u274c \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">aud<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0414\u043b\u044f \u043a\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d (audience)<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u274c \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, role, email, scopes<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u274c \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043f\u043e\u0434\u043f\u0438\u0441\u0438: HS256 vs RS256<\/strong><\/p>\n<ul>\n<li>\n<p><strong>HS256<\/strong> \u2014 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430\u044f \u043f\u043e\u0434\u043f\u0438\u0441\u044c. \u041e\u0434\u0438\u043d \u0441\u0435\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043a\u043b\u044e\u0447. \u041f\u0440\u043e\u0441\u0442\u043e, \u043d\u043e \u043d\u0435 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u0442\u0441\u044f.<\/p>\n<\/li>\n<li>\n<p><strong>RS256<\/strong> \u2014 \u0430\u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430\u044f. \u0427\u0430\u0441\u0442\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u0434\u043b\u044f \u043f\u043e\u0434\u043f\u0438\u0441\u0438, \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0439 \u2014 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438. \u041b\u0443\u0447\u0448\u0435 \u0434\u043b\u044f \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u0438 OAuth2.<\/p>\n<\/li>\n<\/ul>\n<blockquote>\n<p>\u0422\u044b \u043c\u043e\u0436\u0435\u0448\u044c \u043d\u0430\u0447\u0430\u0442\u044c \u0441 <strong>HS256<\/strong>, \u0430 \u0432 \u043f\u043e\u043b\u043d\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u2014 \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043d\u0430 <strong>RS256 \u0438\u043b\u0438 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044e \u0441 Keycloak<\/strong>, \u0433\u0434\u0435 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438.<\/p>\n<\/blockquote>\n<h4>\u26a0\ufe0f\u00a0\u041e\u0448\u0438\u0431\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0447\u0430\u0441\u0442\u043e \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u044e\u0442:<\/h4>\n<ul>\n<li>\n<p>\u274c \u041d\u0435 \u0441\u0442\u0430\u0432\u044f\u0442 exp (\u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442 \u201c\u0432\u0435\u0447\u043d\u044b\u0435\u201d \u0442\u043e\u043a\u0435\u043d\u044b)<\/p>\n<\/li>\n<li>\n<p>\u274c \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442 \u0432 \u0442\u043e\u043a\u0435\u043d <strong>\u043f\u0430\u0440\u043e\u043b\u044c<\/strong> \u0438\u043b\u0438 \u0447\u0443\u0432\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/p>\n<\/li>\n<li>\n<p>\u274c \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0439 \u0438\u043b\u0438 \u0441\u043b\u0430\u0431\u044b\u0439 <code>SECRET_KEY<\/code><\/p>\n<\/li>\n<li>\n<p>\u274c \u041e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442 \u0442\u043e\u043a\u0435\u043d \u0431\u0435\u0437 Bearer \u0441\u0445\u0435\u043c\u044b \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435<\/p>\n<\/li>\n<li>\n<p>\u274c \u041d\u0435 \u0432\u0435\u0440\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u044e\u0442 \u043f\u043e\u0434\u043f\u0438\u0441\u044c \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435<\/p>\n<\/li>\n<\/ul>\n<h3>Refresh token flow<\/h3>\n<p><a href=\"https:\/\/github.com\/takentui\/authorization_types\/tree\/jwt-refresh-auth\" rel=\"noopener noreferrer nofollow\">\u0414\u0435\u043c\u043e \u043f\u0440\u0438\u043c\u0435\u0440<\/a><\/p>\n<p>Refresh token \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e <code>access token<\/code> \u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0415\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0441\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b:<\/p>\n<ol>\n<li>\n<p><strong>\u041f\u0440\u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043b\u0438\u0435\u043d\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u0432\u0430 \u0442\u043e\u043a\u0435\u043d\u0430:<\/strong><\/p>\n<ul>\n<li>\n<p>Access token (\u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0436\u0438\u0432\u0443\u0449\u0438\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 15-30 \u043c\u0438\u043d\u0443\u0442)<\/p>\n<\/li>\n<li>\n<p>Refresh token (\u0434\u043e\u043b\u0433\u043e\u0436\u0438\u0432\u0443\u0449\u0438\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 7-30 \u0434\u043d\u0435\u0439)<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u041a\u043e\u0433\u0434\u0430 access token \u0438\u0441\u0442\u0435\u043a\u0430\u0435\u0442:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041a\u043b\u0438\u0435\u043d\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 <code>refresh<\/code> token \u043d\u0430 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 endpoint (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>POST \/refresh<\/code>)<\/p>\n<\/li>\n<li>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0441\u0442\u044c <code>refresh<\/code> \u0442\u043e\u043a\u0435\u043d\u0430<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u0441\u0435\u0440\u0432\u0435\u0440 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 <code>access<\/code> token \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0435\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0443<\/p>\n<\/li>\n<li>\n<p>\u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e: \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 <code>refresh<\/code> token (rotation strategy)<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u0421\u0445\u0435\u043c\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f:<\/strong><\/p>\n<pre><code>POST \/refresh Content-Type: application\/json  {   \"refresh_token\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ...\" }<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u041e\u0442\u0432\u0435\u0442:<\/strong><\/p>\n<pre><code class=\"json\">{   \"access_token\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVC...\",   \"refresh_token\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVC...\", \/\/ \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e   \"token_type\": \"Bearer\",   \"expires_in\": 900 }<\/code><\/pre>\n<\/li>\n<\/ol>\n<h4>\u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 refresh \u0442\u043e\u043a\u0435\u043d\u043e\u0432:<\/h4>\n<ul>\n<li>\n<p><strong>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430<\/strong>: \u041c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c JWT \u0438\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 (<code>UUID<\/code>)<\/p>\n<\/li>\n<li>\n<p><strong>\u0425\u0440\u0430\u043d\u0435\u043d\u0438\u0435<\/strong>: \u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 <code>Redis<\/code> \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/p>\n<\/li>\n<li>\n<p><strong>\u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c<\/strong>: \u0425\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0432 <code>HttpOnly<\/code>, <code>Secure<\/code>, <code>SameSite=strict<\/code> cookie \u0438\u043b\u0438 \u0432 \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u043e\u043c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0440\u043e\u043a \u0436\u0438\u0437\u043d\u0438<\/strong>: \u0417\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u044c\u0448\u0435 <code>access<\/code> \u0442\u043e\u043a\u0435\u043d\u0430, \u043d\u043e \u0438\u043c\u0435\u0435\u0442 \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0441\u0440\u043e\u043a<\/p>\n<\/li>\n<\/ul>\n<h4>\u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438 \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/h4>\n<ul>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 <code>refresh<\/code> token \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 fingerprint \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0434\u043b\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0437\u0430\u0449\u0438\u0442\u044b<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043e\u0434\u043d\u043e\u0440\u0430\u0437\u043e\u0432\u044b\u0435 <code>refresh<\/code> \u0442\u043e\u043a\u0435\u043d\u044b (rotation strategy)<\/p>\n<\/li>\n<\/ul>\n<h3>\u041e\u0442\u0437\u044b\u0432 JWT (revoke)<\/h3>\n<blockquote>\n<p>\u041e\u0434\u043d\u0438\u043c \u0438\u0437 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u043e\u0432 JWT \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u0437\u044b\u0432\u0430 \u0442\u043e\u043a\u0435\u043d\u043e\u0432, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d\u0438 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0432\u0430\u043b\u0438\u0434\u043d\u044b \u0434\u043e \u0438\u0441\u0442\u0435\u0447\u0435\u043d\u0438\u044f \u0441\u0440\u043e\u043a\u0430.<\/p>\n<\/blockquote>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u043a \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b:<\/p>\n<ol>\n<li>\n<p><strong>Blacklist \u043e\u0442\u043e\u0437\u0432\u0430\u043d\u043d\u044b\u0445 \u0442\u043e\u043a\u0435\u043d\u043e\u0432<\/strong><\/p>\n<pre><code class=\"python\"># \u041f\u0440\u0438\u043c\u0435\u0440 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043e\u0442\u043e\u0437\u0432\u0430\u043d\u043d\u044b\u0445 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u0432 Redis def revoke_token(token):     token_data = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])     jti = token_data.get(\"jti\", token_data.get(\"sub\"))  # \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 ID \u0442\u043e\u043a\u0435\u043d\u0430     expiration = token_data[\"exp\"] - int(datetime.utcnow().timestamp())     redis_client.setex(f\"revoked:{jti}\", expiration, \"1\")  def is_token_revoked(token):     token_data = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])     jti = token_data.get(\"jti\", token_data.get(\"sub\"))     return redis_client.exists(f\"revoked:{jti}\")<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u0447\u0435\u0440\u0435\u0437 Refresh \u0442\u043e\u043a\u0435\u043d\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u0425\u0440\u0430\u043d\u0438\u0442\u0435 \u0442\u043e\u043b\u044c\u043a\u043e refresh \u0442\u043e\u043a\u0435\u043d\u044b \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043b\u043e\u0433\u0430\u0443\u0442\u0435 &#8212; \u0443\u0434\u0430\u043b\u044f\u0439\u0442\u0435 refresh \u0442\u043e\u043a\u0435\u043d \u0438\u0437 \u0431\u0430\u0437\u044b<\/p>\n<\/li>\n<li>\n<p>\u0414\u0435\u0440\u0436\u0438\u0442\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0439 \u0441\u0440\u043e\u043a \u0436\u0438\u0437\u043d\u0438 access \u0442\u043e\u043a\u0435\u043d\u043e\u0432<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u0412\u044b\u043f\u0443\u0441\u043a \u0441 \u0432\u0435\u0440\u0441\u0438\u0435\u0439\/\u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u0441\u0435\u0441\u0441\u0438\u0438<\/strong><\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"python\">def generate_token(user_id, session_id):     return jwt.encode({         \"sub\": user_id,         \"sid\": session_id,  # \u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0441\u0435\u0441\u0441\u0438\u0438         \"exp\": datetime.utcnow() + timedelta(minutes=15)     }, SECRET_KEY, algorithm=ALGORITHM)  def verify_token(token, active_sessions):     payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])     # \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434\u043f\u0438\u0441\u044c, \u043d\u043e \u0438 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u0435\u0441\u0441\u0438\u0438     if payload[\"sid\"] not in active_sessions.get(payload[\"sub\"], []):         raise Exception(\"Token revoked\")     return payload <\/code><\/pre>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/24f\/34b\/ccc\/24f34bccc185e824d483ef74c86b4bbe.png\" alt=\"UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439\" title=\"UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439\" width=\"1084\" height=\"1520\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/24f\/34b\/ccc\/24f34bccc185e824d483ef74c86b4bbe.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/24f\/34b\/ccc\/24f34bccc185e824d483ef74c86b4bbe.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439<\/figcaption><\/div>\n<\/figure>\n<h4>\u2705 \u041f\u043b\u044e\u0441\u044b:<\/h4>\n<ul>\n<li>\n<p><strong>Stateless<\/strong> &#8212; \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0435 \u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u0435\u0441\u0441\u0438\u0438, \u0447\u0442\u043e \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0430\u043c\u043e\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c<\/strong> &#8212; \u0432\u0441\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0432 \u0442\u043e\u043a\u0435\u043d\u0435<\/p>\n<\/li>\n<li>\n<p><strong>\u041a\u0440\u043e\u0441\u0441-\u0434\u043e\u043c\u0435\u043d\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430<\/strong> &#8212; \u043b\u0435\u0433\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043d\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/strong> &#8212; \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a \u0411\u0414 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<li>\n<p><strong>\u0413\u0438\u0431\u043a\u043e\u0441\u0442\u044c<\/strong> &#8212; \u043c\u043e\u0436\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 claims (\u0440\u043e\u043b\u0438, \u043f\u0440\u0430\u0432\u0430 \u0438 \u0442.\u0434.)<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0438\u0437\u0430\u0446\u0438\u044f<\/strong> &#8212; \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a<\/p>\n<\/li>\n<li>\n<p><strong>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438<\/strong> &#8212; \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441<\/p>\n<\/li>\n<\/ul>\n<h4>\u274c \u041c\u0438\u043d\u0443\u0441\u044b:<\/h4>\n<ul>\n<li>\n<p><strong>\u0420\u0430\u0437\u043c\u0435\u0440<\/strong> &#8212; JWT \u043e\u0431\u044b\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c session ID (\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c claims)<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u0437\u044b\u0432\u0430<\/strong> &#8212; \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u043e\u0442\u0437\u044b\u0432\u0430 (\u043d\u0443\u0436\u043d\u0430 \u0434\u043e\u043f. \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f)<\/p>\n<\/li>\n<li>\n<p><strong>\u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f<\/strong> &#8212; \u0441\u043b\u043e\u0436\u043d\u043e \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0435<\/p>\n<\/li>\n<li>\n<p><strong>\u0423\u0442\u0435\u0447\u043a\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438<\/strong> &#8212; payload \u043d\u0435 \u0437\u0430\u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d, \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d \u0432 Base64<\/p>\n<\/li>\n<li>\n<p><strong>\u0423\u0441\u043b\u043e\u0436\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 refresh \u0442\u043e\u043a\u0435\u043d\u043e\u0432<\/strong> &#8212; \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p><strong>\u0420\u0438\u0441\u043a XSS-\u0430\u0442\u0430\u043a<\/strong> &#8212; \u043f\u0440\u0438 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u0432 localStorage<\/p>\n<\/li>\n<li>\n<p><strong>\u041d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0441\u0442\u044c<\/strong> &#8212; \u043d\u0435\u043b\u044c\u0437\u044f \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0442\u043e\u043a\u0435\u043d\u0435 \u0431\u0435\u0437 \u043f\u0435\u0440\u0435\u0432\u044b\u043f\u0443\u0441\u043a\u0430<\/p>\n<\/li>\n<li>\n<p><strong>\u0418\u0441\u0442\u0435\u043a\u0448\u0438\u0435 \u0442\u043e\u043a\u0435\u043d\u044b<\/strong> &#8212; \u0434\u043e \u0438\u0441\u0442\u0435\u0447\u0435\u043d\u0438\u044f \u0441\u0440\u043e\u043a\u0430 \u043a\u043b\u0438\u0435\u043d\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d<\/p>\n<\/li>\n<\/ul>\n<h4>\u0413\u0434\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u043e\u043a\u0435\u043d \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0435<\/h4>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p align=\"left\">\u0425\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435<\/p>\n<\/th>\n<th>\n<p align=\"left\">Access Token<\/p>\n<\/th>\n<th>\n<p align=\"left\">Refresh Token<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">HttpOnly Cookie<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2705 \u0425\u043e\u0440\u043e\u0448\u043e<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2705 \u041b\u0443\u0447\u0448\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0417\u0430\u0449\u0438\u0442\u0430 \u043e\u0442 XSS, \u0443\u044f\u0437\u0432\u0438\u043c \u043a CSRF (\u043d\u0443\u0436\u043d\u0430 \u0434\u043e\u043f. \u0437\u0430\u0449\u0438\u0442\u0430)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">localStorage<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u274c \u041d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u274c \u041e\u0447\u0435\u043d\u044c \u043d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0423\u044f\u0437\u0432\u0438\u043c \u043a XSS-\u0430\u0442\u0430\u043a\u0430\u043c<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">sessionStorage<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u26a0\ufe0f \u041e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u274c \u041d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0416\u0438\u0432\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0441\u0435\u0441\u0441\u0438\u0438 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">Memory (JS \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f)<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2705 \u0425\u043e\u0440\u043e\u0448\u043e<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u26a0\ufe0f \u041d\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0418\u0441\u0447\u0435\u0437\u0430\u0435\u0442 \u043f\u0440\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">IndexedDB<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u26a0\ufe0f \u0417\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u274c \u041d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0422\u0440\u0435\u0431\u0443\u0435\u0442 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><strong>\u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u043c\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434:<\/strong><\/p>\n<ul>\n<li>\n<p><strong>Access token<\/strong>: <code>HttpOnly<\/code> cookie \u0438\u043b\u0438 in-memory storage<\/p>\n<\/li>\n<li>\n<p><strong>Refresh token<\/strong>: \u0422\u043e\u043b\u044c\u043a\u043e <code>HttpOnly<\/code>, <code>Secure<\/code>, <code>SameSite=strict cookie<\/code><\/p>\n<\/li>\n<\/ul>\n<h4>\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0440\u044b \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438:<\/h4>\n<ol>\n<li>\n<p><strong>Anti-CSRF \u0442\u043e\u043a\u0435\u043d\u044b<\/strong> <\/p>\n<ul>\n<li>\n<p>\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0439\u0442\u0435 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0435\u0441\u0441\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0439\u0442\u0435 \u0435\u0433\u043e \u043f\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445, \u0432\u043b\u0438\u044f\u044e\u0449\u0438\u0445 \u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Fingerprinting \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430<\/strong> <\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0439\u0442\u0435 refresh \u0442\u043e\u043a\u0435\u043d \u043a \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0430\u043c \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0439\u0442\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u0440\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0438 \u0442\u043e\u043a\u0435\u043d\u0430<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>SSO (Single-sign on)<\/h3>\n<p><strong>SSO (Single Sign-On)<\/strong> \u2014 \u044d\u0442\u043e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043b\u043e\u0433\u0438\u043d\u0438\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a\u043e \u0432\u0441\u0435\u043c \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u043c \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u043c, \u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438.<\/p>\n<blockquote>\n<p>\ud83c\udfaf \u0421\u0430\u043c SSO \u2014 \u044d\u0442\u043e <strong>\u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/strong>, \u0430 \u043d\u0435 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f.<br \/><em>\u0410 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u043d \u0447\u0435\u0440\u0435\u0437 Keycloak, Okta, Azure AD \u0438 \u0442.\u043f.<\/em><\/p>\n<\/blockquote>\n<ol>\n<li>\n<p><strong>User (\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c)<\/strong> \u2014 \u0445\u043e\u0447\u0435\u0442 \u0432\u043e\u0439\u0442\u0438, \u0437\u043d\u0430\u0435\u0442 \u0441\u0432\u043e\u0439 \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c<\/p>\n<\/li>\n<li>\n<p><strong>Service Provider (SP)<\/strong> \u2014 \u0441\u0430\u0439\u0442, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043d \u0437\u0430\u0445\u043e\u0434\u0438\u0442 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, GitLab)<\/p>\n<\/li>\n<li>\n<p><strong>Identity Provider (IdP)<\/strong> \u2014 \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u0435\u0442 \u043b\u0438\u0447\u043d\u043e\u0441\u0442\u044c (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Keycloak, Google, Okta)<\/p>\n<\/li>\n<\/ol>\n<h4>\u041a\u0430\u043a \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/h4>\n<ol>\n<li>\n<p>\u0417\u0430\u0445\u043e\u0434\u0438\u0448\u044c \u043d\u0430 <code>app1.company.com<\/code> \u2192 \u043b\u043e\u0433\u0438\u043d\u0438\u0448\u044c\u0441\u044f<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0448\u044c \u043d\u0430 <code>app2.company.com<\/code> \u2192 \u0443\u0436\u0435 \u0437\u0430\u043b\u043e\u0433\u0438\u043d\u0435\u043d<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0448\u044c \u0432 <a href=\"http:\/\/dashboard.partner.com\" rel=\"noopener noreferrer nofollow\"><code>dashboard.partner.com<\/code><\/a> \u2192 \u0442\u043e\u0436\u0435 \u0437\u0430\u043b\u043e\u0433\u0438\u043d\u0435\u043d<\/p>\n<p>\u2192 \u0431\u0435\u0437 \u0432\u0432\u043e\u0434\u0430 \u043f\u0430\u0440\u043e\u043b\u044f<\/p>\n<\/li>\n<\/ol>\n<h4>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c<\/h4>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0437\u0430\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 <code>Service A<\/code><\/p>\n<\/li>\n<li>\n<p>\u0415\u0433\u043e \u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442\u044f\u0442 \u043d\u0430 <strong>IdP<\/strong><\/p>\n<\/li>\n<li>\n<p>\u041e\u043d \u043b\u043e\u0433\u0438\u043d\u0438\u0442\u0441\u044f <strong>\u043e\u0434\u0438\u043d \u0440\u0430\u0437<\/strong><\/p>\n<\/li>\n<li>\n<p>IdP \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u043e\u043a\u0435\u043d \u0438\u043b\u0438 \u0441\u0435\u0441\u0441\u0438\u044e<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a <code>Service A<\/code><\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0435 \u043d\u0430 <code>Service B<\/code>, \u0442\u043e\u0442 \u0441\u043d\u043e\u0432\u0430 \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a IdP \u2192 <strong>\u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u0435\u0441\u0441\u0438\u044f \u2192 \u0432\u0445\u043e\u0434 \u0431\u0435\u0437 \u043f\u0430\u0440\u043e\u043b\u044f<\/strong><\/p>\n<\/li>\n<\/ol>\n<h4>SSO \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0447\u0435\u0440\u0435\u0437 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u044b:<\/h4>\n<ul>\n<li>\n<p><strong>SAML<\/strong> \u2014 \u0441\u0442\u0430\u0440\u044b\u0439, XML, \u0447\u0430\u0441\u0442\u043e \u0432 enterprise<\/p>\n<\/li>\n<li>\n<p><strong>OpenID Connect (OIDC)<\/strong> \u2014 \u043d\u0430\u0434\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043d\u0430\u0434 OAuth2, \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442<\/p>\n<\/li>\n<li>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u2014 <strong>Kerberos<\/strong> (\u0432 Windows-\u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u044f\u0445)<\/p>\n<\/li>\n<\/ul>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440:<\/h4>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u0442\u0435\u0431\u044f \u0435\u0441\u0442\u044c \u0442\u0440\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f:<\/p>\n<ul>\n<li>\n<p><a href=\"http:\/\/billing.domain.com\" rel=\"noopener noreferrer nofollow\"><code>billing.domain.com<\/code><\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/docs.domain.com\" rel=\"noopener noreferrer nofollow\"><code>docs.domain.com<\/code><\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/admin.domain.com\" rel=\"noopener noreferrer nofollow\"><code>admin.domain.com<\/code><\/a><\/p>\n<\/li>\n<\/ul>\n<p>\u0421 SSO:<\/p>\n<ul>\n<li>\n<p>\u0412\u0441\u0435 \u043e\u043d\u0438 \u0434\u043e\u0432\u0435\u0440\u044f\u044e\u0442 \u043e\u0434\u043d\u043e\u043c\u0443 IdP (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Keycloak)<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043b\u043e\u0433\u0438\u043d\u0438\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u2014 \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a\u043e \u0432\u0441\u0435\u043c \u0442\u0440\u0451\u043c<\/p>\n<\/li>\n<\/ul>\n<h4>\u2705 \u041f\u043b\u044e\u0441\u044b SSO<\/h4>\n<ul>\n<li>\n<p>\u0423\u0434\u043e\u0431\u043d\u043e \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 (1 \u043b\u043e\u0433\u0438\u043d \u2192 \u043c\u043d\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u043e\u0432)<\/p>\n<\/li>\n<li>\n<p>\u0426\u0435\u043d\u0442\u0440\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438<\/p>\n<\/li>\n<li>\n<p>\u0418\u0434\u0435\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u043a\u043e\u0440\u043f\u043e\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u044b, \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 MFA, LDAP, OAuth2, Google Login \u0438 \u0434\u0440.<\/p>\n<\/li>\n<\/ul>\n<h4>\u274c \u041c\u0438\u043d\u0443\u0441\u044b<\/h4>\n<ul>\n<li>\n<p>\u041e\u0434\u043d\u0430 \u0442\u043e\u0447\u043a\u0430 \u043e\u0442\u043a\u0430\u0437\u0430 (\u0435\u0441\u043b\u0438 IdP \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u2014 \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u0432\u043e\u0439\u0434\u0451\u0442)<\/p>\n<\/li>\n<li>\n<p>\u041d\u0443\u0436\u043d\u043e \u0443\u043c\u0435\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u0422\u0440\u0435\u0431\u0443\u0435\u0442 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u043e\u0432 (OIDC\/SAML)<\/p>\n<\/li>\n<\/ul>\n<h3>OAuth 2.0<\/h3>\n<p><a href=\"https:\/\/github.com\/takentui\/authorization_types\/tree\/oauth-2.0\" rel=\"noopener noreferrer nofollow\">\u0414\u0435\u043c\u043e \u043f\u0440\u0438\u043c\u0435\u0440<\/a><\/p>\n<p><strong>OAuth 2.0<\/strong> \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u0435, \u0431\u0435\u0437 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043b\u043e\u0433\u0438\u043d\u0430 \u0438 \u043f\u0430\u0440\u043e\u043b\u044f.<\/p>\n<blockquote>\n<p>\ud83c\udfaf <strong>OAuth 2.0 \u2260<\/strong> <strong>\u043d\u0435 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f, \u0430 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c \u043f\u043e \u0434\u043e\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0438.<\/strong><\/p>\n<p><em>\u00abOAuth2 \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043e\u0442 \u0438\u043c\u0435\u043d\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043d\u0435 \u0437\u043d\u0430\u044f \u0435\u0433\u043e \u043f\u0430\u0440\u043e\u043b\u044f. \u0412\u0441\u0451 \u0447\u0435\u0440\u0435\u0437 \u0442\u043e\u043a\u0435\u043d\u044b, \u0432\u0441\u0451 \u043f\u043e\u0434 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0435\u043c.\u00bb<\/em><\/p>\n<\/blockquote>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440:<\/h4>\n<p>\u0422\u044b \u043b\u043e\u0433\u0438\u043d\u0438\u0448\u044c\u0441\u044f \u043d\u0430 \u0441\u0430\u0439\u0442\u0435 \u0447\u0435\u0440\u0435\u0437 Google:<\/p>\n<ul>\n<li>\n<p>\u042d\u0442\u043e\u0442 \u0441\u0430\u0439\u0442 <strong>\u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0442\u0432\u043e\u0439 \u043f\u0430\u0440\u043e\u043b\u044c<\/strong><\/p>\n<\/li>\n<li>\n<p>Google \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442: \u201c\u042d\u0442\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0445\u043e\u0447\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0430\u0448\u0435\u0439 \u043f\u043e\u0447\u0442\u0435\u201d<\/p>\n<\/li>\n<li>\n<p>\u0422\u044b \u043d\u0430\u0436\u0438\u043c\u0430\u0435\u0448\u044c \u201c\u0420\u0430\u0437\u0440\u0435\u0448\u0438\u0442\u044c\u201d<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 <strong>\u0442\u043e\u043a\u0435\u043d<\/strong> \u2014 \u0438 \u043c\u043e\u0436\u0435\u0442 \u0447\u0438\u0442\u0430\u0442\u044c \u0442\u0432\u043e\u0438 \u043f\u0438\u0441\u044c\u043c\u0430 (\u0438\u043b\u0438 \u0447\u0442\u043e \u0442\u0430\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u043b\u0438)<\/p>\n<\/li>\n<\/ul>\n<h4>\u041a\u0442\u043e \u0443\u0447\u0430\u0441\u0442\u0432\u0443\u0435\u0442 \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435<\/h4>\n<ol>\n<li>\n<p><strong>Resource Owner<\/strong> \u2014 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c (\u0442\u044b)<\/p>\n<\/li>\n<li>\n<p><strong>Client<\/strong> \u2014 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0445\u043e\u0447\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Zoom, Notion)<\/p>\n<\/li>\n<li>\n<p><strong>Authorization Server<\/strong> \u2014 \u043a\u0442\u043e \u0432\u044b\u0434\u0430\u0435\u0442 \u0442\u043e\u043a\u0435\u043d (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Google)<\/p>\n<\/li>\n<li>\n<p><strong>Resource Server<\/strong> \u2014 API, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043a\u043b\u0438\u0435\u043d\u0442 \u0445\u043e\u0447\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Gmail API)<\/p>\n<\/li>\n<\/ol>\n<h4>\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a: Authorization Code Flow<\/h4>\n<p><strong>\u042d\u0442\u0430\u043f\u044b:<\/strong><\/p>\n<ol>\n<li>\n<p>\u041a\u043b\u0438\u0435\u043d\u0442 (frontend) \u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0430 Google:<\/p>\n<pre><code>&lt;https:\/\/accounts.google.com\/o\/oauth2\/auth?client\\_id=...&amp;redirect\\_uri=&gt;...<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043b\u043e\u0433\u0438\u043d\u0438\u0442\u0441\u044f \u0438 <strong>\u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f<\/strong><\/p>\n<\/li>\n<li>\n<p>Google \u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0441 <code>code<\/code>:<\/p>\n<pre><code>&lt;https:\/\/your-app.com\/callback?code=abc123&gt;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 (backend) \u043e\u0431\u043c\u0435\u043d\u0438\u0432\u0430\u0435\u0442 <code>code<\/code> \u043d\u0430 <strong>access token<\/strong> (\u0438 \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e refresh token)<\/p>\n<\/li>\n<li>\n<p>\u0421 \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u043a\u043b\u0438\u0435\u043d\u0442 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c JWT-\u0442\u043e\u043a\u0435\u043d:<\/p>\n<pre><code>Authorization: Bearer &lt;token&gt;<\/code><\/pre>\n<\/li>\n<\/ol>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a38\/60c\/d89\/a3860cd89a174426318dd8b7fd21c0e1.png\" alt=\"UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439\" title=\"UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439\" width=\"1654\" height=\"1530\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/a38\/60c\/d89\/a3860cd89a174426318dd8b7fd21c0e1.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a38\/60c\/d89\/a3860cd89a174426318dd8b7fd21c0e1.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439<\/figcaption><\/div>\n<\/figure>\n<h4>Access \u0438 Refresh Token<\/h4>\n<p>\u041f\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0438 \u0441 JWT-\u0442\u043e\u043a\u0435\u043d\u043e\u043c refresh-\u0442\u043e\u043a\u0435\u043d \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 access-\u0442\u043e\u043a\u0435\u043d \u043f\u043e \u0438\u0441\u0442\u0435\u0447\u0435\u043d\u0438\u044e \u0435\u0433\u043e \u0441\u0440\u043e\u043a\u0430 \u0436\u0438\u0437\u043d\u0438<\/p>\n<ul>\n<li>\n<p><strong>Access Token<\/strong> \u2014 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 (\u0447\u0430\u0441\u0442\u043e 1 \u0447\u0430\u0441), \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a API<\/p>\n<\/li>\n<li>\n<p><strong>Refresh Token<\/strong> \u2014 \u0434\u043e\u043b\u0433\u043e\u0432\u0435\u0447\u043d\u044b\u0439, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e access token \u0431\u0435\u0437 \u043b\u043e\u0433\u0438\u043d\u0430<\/p>\n<\/li>\n<\/ul>\n<h4>OAuth \u2260 Login<\/h4>\n<p>OAuth \u043d\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u201c\u043a\u0442\u043e \u0442\u044b\u201d, \u043e\u043d \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u201c\u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0442\u0435\u0431\u0435 \u044d\u0442\u043e\u201d.<\/p>\n<blockquote>\n<p>\u041d\u043e \u0435\u0441\u043b\u0438 <strong>\u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c OIDC (OpenID Connect)<\/strong> \u2014 \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f ID Token, \u0438 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c OAuth2 \u043a\u0430\u043a <strong>\u043b\u043e\u0433\u0438\u043d<\/strong> (SSO).<\/p>\n<\/blockquote>\n<h4>\u2705 \u041f\u043b\u044e\u0441\u044b OAuth2<\/h4>\n<ul>\n<li>\n<p>\u041d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043b\u043e\u0433\u0438\u043d\u044b\/\u043f\u0430\u0440\u043e\u043b\u0438 \u0434\u0440\u0443\u0433\u0438\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c<\/p>\n<\/li>\n<li>\n<p>\u041c\u043e\u0436\u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043f\u043e <strong>scope<\/strong> (<code>read<\/code>, <code>write<\/code>, <code>profile<\/code>)<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e\u043c \u043a\u0440\u0443\u043f\u043d\u044b\u0445 API<\/p>\n<\/li>\n<li>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u0434\u043b\u044f <strong>\u201c\u0412\u043e\u0439\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 Google\/Facebook\/GitHub\u201d<\/strong><\/p>\n<\/li>\n<\/ul>\n<h4>\u274c \u041c\u0438\u043d\u0443\u0441\u044b<\/h4>\n<ul>\n<li>\n<p>\u0421\u043b\u043e\u0436\u043d\u0430\u044f \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f (client_id, redirect_uri, state)<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u0442\u044c \u043c\u043d\u043e\u0433\u043e <strong>\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 (flows)<\/strong>: authorization code, client credentials, etc.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u2014 \u043b\u0435\u0433\u043a\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u0443\u044f\u0437\u0432\u0438\u043c\u043e\u0441\u0442\u044c<\/p>\n<\/li>\n<\/ul>\n<h3>Keycloak (SSO \/  OpenID Connect)<\/h3>\n<p><a href=\"https:\/\/github.com\/takentui\/authorization_types\/tree\/keycloak\" rel=\"noopener noreferrer nofollow\">\u0414\u0435\u043c\u043e \u043f\u0440\u0438\u043c\u0435\u0440<\/a><\/p>\n<p><strong>Keycloak<\/strong> \u2014 \u044d\u0442\u043e \u0433\u043e\u0442\u043e\u0432\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u044b OIDC (OpenID Connect) \u0438 OAuth2.0. \u041e\u043d\u0430 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 <strong>SSO, MFA, \u0432\u0445\u043e\u0434 \u0447\u0435\u0440\u0435\u0437 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b<\/strong> \u0438 \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u0430.<\/p>\n<blockquote>\n<p>Keycloak \u2014 \u044d\u0442\u043e \u0432\u0441\u0451 \u0432 \u043e\u0434\u043d\u043e\u043c: \u043b\u043e\u0433\u0438\u043d, \u0442\u043e\u043a\u0435\u043d\u044b, \u0440\u043e\u043b\u0438, \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438, OAuth2, OIDC, MFA \u0438 SSO.<\/p>\n<\/blockquote>\n<h4>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/h4>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043a\u043b\u0438\u043a\u0430\u0435\u0442 \u201c\u0412\u043e\u0439\u0442\u0438\u201d \u0432 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439<\/p>\n<\/li>\n<li>\n<p>\u0411\u0440\u0430\u0443\u0437\u0435\u0440 \u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0442\u0441\u044f \u043d\u0430 <strong>Keycloak<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>Keycloak<\/strong> \u043b\u043e\u0433\u0438\u043d\u0438\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f (\u0447\u0435\u0440\u0435\u0437 \u0444\u043e\u0440\u043c\u0443, LDAP, Google \u0438 \u0442.\u0434.)<\/p>\n<\/li>\n<li>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441 <code>code<\/code> \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043e\u0431\u043c\u0435\u043d\u0438\u0432\u0430\u0435\u0442 <code>code<\/code> \u043d\u0430 <code>access token<\/code>, <code>ID token<\/code>, <code>refresh token<\/code><\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u0435\u0440\u0432\u0438\u0441\u0443<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0434\u0440\u0443\u0433\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c <strong>\u0443\u0436\u0435 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d (SSO)<\/strong><\/p>\n<\/li>\n<\/ol>\n<h4>\u041c\u0435\u0445\u0430\u043d\u0438\u043a\u0430 (\u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438):<\/h4>\n<ul>\n<li>\n<p>Keycloak \u2014 \u044d\u0442\u043e <strong>Identity Provider (IdP)<\/strong><\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0447\u0435\u0440\u0435\u0437 <strong>OIDC<\/strong> (\u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u043a OAuth2)<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u0434\u0430\u0451\u0442 \u0442\u043e\u043a\u0435\u043d\u044b: <\/p>\n<ul>\n<li>\n<p><strong>Access token<\/strong> (\u0434\u043e\u0441\u0442\u0443\u043f \u043a API)<\/p>\n<\/li>\n<li>\n<p><strong>ID token<\/strong> (\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435)<\/p>\n<\/li>\n<li>\n<p><strong>Refresh token<\/strong> (\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0442\u043e\u043a\u0435\u043d\u043e\u0432)<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442: <\/p>\n<ul>\n<li>\n<p>JWT (RS256 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e)<\/p>\n<\/li>\n<li>\n<p>Google\/GitHub\/LDAP \u043a\u0430\u043a \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0435 IdP<\/p>\n<\/li>\n<li>\n<p>MFA (2FA), \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430, \u0440\u043e\u043b\u0438, \u0433\u0440\u0443\u043f\u043f\u044b<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/2b4\/a6e\/48a\/2b4a6e48add47088b7df0157145e0f03.png\" alt=\"UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439\" title=\"UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439\" width=\"1404\" height=\"1602\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/2b4\/a6e\/48a\/2b4a6e48add47088b7df0157145e0f03.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/2b4\/a6e\/48a\/2b4a6e48add47088b7df0157145e0f03.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439<\/figcaption><\/div>\n<\/figure>\n<h4>\u2705 \u041f\u043b\u044e\u0441\u044b:<\/h4>\n<ul>\n<li>\n<p>\u0426\u0435\u043d\u0442\u0440\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0439 SSO. \u0412\u0445\u043e\u0434\u0438\u0442 \u0432 \u043e\u0434\u043d\u043e \u2014 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d \u0432\u043e \u0432\u0441\u0451\u043c<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u0432: OIDC, OAuth2, SAML<\/p>\n<\/li>\n<li>\n<p>\u0413\u0438\u0431\u043a\u043e\u0441\u0442\u044c: UI \u043b\u043e\u0433\u0438\u043d, MFA, \u0440\u043e\u043b\u0438, \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438, custom flows<\/p>\n<\/li>\n<li>\n<p>\u041c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u043e \u0438 \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e: \u041e\u0442\u043b\u0438\u0447\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f production \/ enterprise<\/p>\n<\/li>\n<\/ul>\n<h4>\u274c \u041c\u0438\u043d\u0443\u0441\u044b:<\/h4>\n<ul>\n<li>\n<p>\u041d\u0443\u0436\u043d\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 real\u043c\u0430\u043c\u0438, \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c\u0438, \u043c\u0430\u043f\u043f\u0438\u043d\u0433\u0430\u043c\u0438<\/p>\n<\/li>\n<li>\n<p>\u0422\u0440\u0435\u0431\u0443\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0441 Keycloak, \u043b\u0438\u0431\u043e Docker<\/p>\n<\/li>\n<li>\n<p>\u0415\u0434\u0438\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430 \u043e\u0442\u043a\u0430\u0437\u0430 (IdP): \u0415\u0441\u043b\u0438 Keycloak \u0443\u043f\u0430\u043b \u2014 \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u0432\u043e\u0439\u0434\u0451\u0442<\/p>\n<\/li>\n<li>\n<p>\u0421\u043b\u043e\u0436\u043d\u043e \u0434\u043b\u044f \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432<\/p>\n<\/li>\n<\/ul>\n<h4>\u0413\u0434\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u043e:<\/h4>\n<p>\u2705 <strong>\u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442<\/strong>:<\/p>\n<ul>\n<li>\n<p>\u041c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043d\u0430\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u0440\u043f\u043e\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u044b<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u0438 \u0440\u043e\u043b\u0435\u0439<\/p>\n<\/li>\n<li>\n<p>SSO \u0447\u0435\u0440\u0435\u0437 Google, GitHub, LDAP<\/p>\n<\/li>\n<\/ul>\n<p>\u274c <strong>\u041d\u0435<\/strong> <strong>\u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442<\/strong>:<\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u044b \u0431\u0435\u0437 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439<\/p>\n<\/li>\n<li>\n<p>MVP \u0441 \u043e\u0434\u043d\u0438\u043c \u0432\u0445\u043e\u0434\u043e\u043c<\/p>\n<\/li>\n<li>\n<p>\u0422\u0430\u043c, \u0433\u0434\u0435 \u043d\u0435\u0442 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b<\/p>\n<\/li>\n<\/ul>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440 \u043d\u0430 FastAPI:<\/h4>\n<ul>\n<li>\n<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a <strong>&#171;Client&#187;<\/strong> \u0432 Keycloak<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043b\u043e\u0433\u0438\u043d\u0438\u0442\u0441\u044f \u2192 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f <code>code<\/code><\/p>\n<\/li>\n<li>\n<p>FastAPI <strong>\u043e\u0431\u043c\u0435\u043d\u0438\u0432\u0430\u0435\u0442 <\/strong><code><strong>code<\/strong><\/code><strong> \u043d\u0430 \u0442\u043e\u043a\u0435\u043d\u044b<\/strong> \u0447\u0435\u0440\u0435\u0437 HTTP-\u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">import requests  def exchange_code_for_token(code: str):     res = requests.post(\"&lt;https:\/\/keycloak\/auth\/realms\/myrealm\/protocol\/openid-connect\/token&gt;\", data={         \"client_id\": \"myapp\",         \"client_secret\": \"secret\",         \"grant_type\": \"authorization_code\",         \"code\": code,         \"redirect_uri\": \"&lt;https:\/\/myapp\/callback&gt;\"     })     return res.json() <\/code><\/pre>\n<h2>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0432\u0438\u0434\u043e\u0432 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/h2>\n<p>\u041d\u0443 \u0438 \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u043a \u0448\u043f\u0430\u0440\u0433\u0430\u043b\u043a\u0430 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f\u043c \u0432\u0438\u0434\u043e\u0432 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/01c\/fa9\/b60\/01cfa9b603e2d2f2820d76ca708d12b0.png\" alt=\"\u0422\u0430\u0431\u043b\u0438\u0447\u043a\u0430 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0432\u0438\u0434\u043e\u0432 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438.\" title=\"\u0422\u0430\u0431\u043b\u0438\u0447\u043a\u0430 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0432\u0438\u0434\u043e\u0432 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438.\" width=\"1990\" height=\"1218\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/01c\/fa9\/b60\/01cfa9b603e2d2f2820d76ca708d12b0.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/01c\/fa9\/b60\/01cfa9b603e2d2f2820d76ca708d12b0.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0422\u0430\u0431\u043b\u0438\u0447\u043a\u0430 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0432\u0438\u0434\u043e\u0432 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438.<\/figcaption><\/div>\n<\/figure>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0442\u0435\u0431\u0435 \u0431\u044b\u043b\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043e\u043c\u0430\u0448\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0432\u0435\u0442\u043a\u0435! \u0410 \u0442\u0430\u043a \u0434\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0438 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435, \u043f\u043e\u043a\u0430!<\/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\/939662\/\"> https:\/\/habr.com\/ru\/articles\/939662\/<\/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>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430 \u0442\u0435\u043c, \u043a\u0442\u043e \u043e\u0441\u043e\u0431\u043e \u043d\u0435 \u0437\u0430\u043c\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u043b\u0441\u044f, \u043a\u0430\u043a \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0432 \u0431\u0435\u043a\u0435\u043d\u0434\u0435, \u043d\u043e \u0445\u043e\u0447\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u043e \u0432\u044a\u0435\u0445\u0430\u0442\u044c \u0432 \u0442\u0435\u043c\u0443. \u041c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u0433\u0440\u0443\u0436\u0430\u0442\u044c\u0441\u044f \u0432 \u0441\u0430\u043c\u044b\u0435 \u043d\u0435\u0434\u0440\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043d\u043e \u0442\u043e\u0447\u043d\u043e \u043f\u043e\u0439\u043c\u0435\u043c \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438 \u043a\u0430\u043a \u043f\u0440\u0438\u043a\u0440\u0443\u0442\u0438\u0442\u044c \u044d\u0442\u043e \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435. \u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c python \u0438 FastAPI, \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u0430\u043a \u043f\u0440\u043e\u0449\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c, \u043d\u043e \u044d\u0442\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0438 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u043e\u0432\/\u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u0432.<\/p>\n<blockquote>\n<p><em>\u042d\u0442\u043e \u0442\u043e\u043f \u0432\u0438\u0434\u043e\u0432 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e API \u0434\u043b\u044f \u0431\u0435\u043a\u0435\u043d\u0434 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432. \u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b, \u043d\u043e \u044d\u0442\u0438 &#8212; \u043e\u0441\u043d\u043e\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0432 99% \u0441\u043b\u0443\u0447\u0430\u0435\u0432.<\/em><\/p>\n<\/blockquote>\n<details class=\"spoiler\">\n<summary>\u0427\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0434\u0430\u043b\u044c\u0448\u0435<\/summary>\n<div class=\"spoiler__content\">\n<ul>\n<li>\n<p><strong>Basic Auth<\/strong> \u2014 \u0431\u044b\u0441\u0442\u0440\u043e, \u043f\u0440\u043e\u0441\u0442\u043e, \u043d\u043e \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043b\u043e\u0445\u0430\u044f \u0438\u0434\u0435\u044f<\/p>\n<\/li>\n<li>\n<p><strong>Session<\/strong> \u2014 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0445 HTML-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439<\/p>\n<\/li>\n<li>\n<p><strong>JWT<\/strong> \u2014 \u043b\u0443\u0447\u0448\u0438\u0439 \u0432\u044b\u0431\u043e\u0440 \u0434\u043b\u044f REST API \u0438 SPA, \u0435\u0441\u043b\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c<\/p>\n<\/li>\n<li>\n<p><strong>OAuth2<\/strong> \u2014 must-have \u0434\u043b\u044f \u0432\u0445\u043e\u0434\u0430 \u0447\u0435\u0440\u0435\u0437 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b, \u0434\u0435\u043b\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0430\u0432<\/p>\n<\/li>\n<li>\n<p><strong>Keycloak<\/strong> \u2014 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u0435\u043d, \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u0435\u043d <strong>SSO<\/strong>, <strong>MFA<\/strong>, <strong>LDAP<\/strong>, <strong>Google Login<\/strong> \u0438 <strong>\u0446\u0435\u043d\u0442\u0440\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438<\/strong><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/details>\n<blockquote>\n<p><em>\u0412\u0441\u0435 \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u043d\u0438\u0436\u0435, \u044f \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b \u0432 \u0432\u0438\u0434\u0435\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u0434\u043b\u044f \u0442\u0432\u043e\u0435\u0433\u043e \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430! \u0412\u044b\u0431\u0438\u0440\u0430\u0439 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0442\u0435\u0431\u0435 \u0432\u0438\u0434 \u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430 \u0438 \u0438\u0437\u0443\u0447\u0430\u0439!<\/em><\/p>\n<\/blockquote>\n<details class=\"spoiler\">\n<summary>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u043e\u043c \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/summary>\n<div class=\"spoiler__content\">\n<ol>\n<li>\n<p>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0437\u0443\u0447\u0438 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043f\u043e \u0442\u0435\u043c\u0435<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438 \u0434\u0435\u043c\u043e \u043f\u0440\u0438\u043c\u0435\u0440, \u044d\u0442\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u0434\u043b\u044f \u0442\u0435\u0431\u044f \u044d\u0442\u043e\u0442 \u0432\u0438\u0434 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>\u0421\u0434\u0435\u043b\u0430\u0439 \u0434\u043e\u043c\u0430\u0448\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u0435 \u0438\u0437 \u0444\u0430\u0439\u043b\u0438\u043a\u0430 tasks.md \u0432 \u044d\u0442\u043e\u0439 \u0432\u0435\u0442\u043a\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <a href=\"https:\/\/github.com\/takentui\/authorization_types\/blob\/fastapi-basic-auth\/tasks.md\" rel=\"noopener noreferrer nofollow\">tasks.md<\/a><\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u0431\u0435\u0440\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u0448\u0438\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u0441 \u0433\u0443\u0433\u043b\u043e\u043c, \u0438\u043b\u0438 \u043f\u0438\u0448\u0438 \u043c\u043d\u0435 \u0432 \u0442\u0435\u043b\u0435\u0433\u0443<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/details>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/68a5969f79b5b6ab9ace586c\" data-style=\"\" id=\"68a5969f79b5b6ab9ace586c\" width=\"\"><\/div>\n<p>\u0418\u0442\u0430\u043a \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043c \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043d\u044f\u0442\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0434\u0430\u043b\u044c\u0448\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u044f\u0437\u044b\u043a\u0435. <\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0422\u0440\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0440\u0435\u0441\u0443\u0440\u0441\u0443<\/figcaption><\/div>\n<\/figure>\n<h2>\u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f (Identification)<\/h2>\n<p><strong>\u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/strong> \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0435, \u0442.\u0435. \u0437\u0430\u044f\u0432\u043b\u044f\u0435\u0442, \u043a\u0442\u043e \u043e\u043d \u0435\u0441\u0442\u044c.<\/p>\n<p><strong>\u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0447\u0435\u0440\u0442\u044b:<\/strong><\/p>\n<ul>\n<li>\n<p>\u042d\u0442\u043e <strong>\u0437\u0430\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u043b\u0438\u0447\u043d\u043e\u0441\u0442\u0438<\/strong>, \u0430 \u043d\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430<\/p>\n<\/li>\n<li>\n<p>\u0421\u0430\u043c\u043e \u043f\u043e \u0441\u0435\u0431\u0435 <strong>\u043d\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u0432\u044b \u0442\u043e\u0442, \u0437\u0430 \u043a\u043e\u0433\u043e \u0441\u0435\u0431\u044f \u0432\u044b\u0434\u0430\u0451\u0442\u0435<\/strong><\/p>\n<\/li>\n<li>\n<p>\u0412\u0441\u0435\u0433\u0434\u0430 <strong>\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438<\/strong><\/p>\n<\/li>\n<\/ul>\n<p><strong>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0424\u043e\u0440\u043c\u044b \u043b\u043e\u0433\u0438\u043d\u0430 (<code>username<\/code> \u0438\u043b\u0438 <code>email<\/code>)<\/p>\n<\/li>\n<li>\n<p>JWT \u2014 \u043f\u043e\u043b\u0435 <code>sub<\/code> (subject) \u2014 \u044d\u0442\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u043a\u043e\u0439 \u0442\u043e \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0431\u0430\u043d\u043a\u043e\u0432\u0441\u043a\u0430\u044f \u043a\u0430\u0440\u0442\u0430, \u043f\u0440\u043e\u043f\u0443\u0441\u043a)<\/p>\n<\/li>\n<\/ul>\n<h2>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f (Authentication)<\/h2>\n<p><strong>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/strong> \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043b\u0438\u0447\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0432\u044b <strong>\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0442\u043e\u0442, \u0437\u0430 \u043a\u043e\u0433\u043e \u0441\u0435\u0431\u044f \u0432\u044b\u0434\u0430\u0451\u0442\u0435<\/strong>.<\/p>\n<p><strong>\u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u0430\u0440\u043e\u043b\u044c \/ Pin code<\/p>\n<\/li>\n<li>\n<p>\u0422\u043e\u043a\u0435\u043d \/ \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442<\/p>\n<\/li>\n<li>\n<p>\u041e\u0434\u043d\u043e\u0440\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043e\u0434 (OTP)<\/p>\n<\/li>\n<li>\n<p>\u0411\u0438\u043e\u043c\u0435\u0442\u0440\u0438\u044f (\u043b\u0438\u0446\u043e, \u043f\u0430\u043b\u0435\u0446)<\/p>\n<\/li>\n<\/ul>\n<h2>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f (Authorization)<\/h2>\n<p><strong>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f<\/strong> \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f, \u043a\u0430\u043a\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u043f\u043e\u0441\u043b\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438.<\/p>\n<p><strong>\u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0444\u043e\u0440\u043c\u044b:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0420\u043e\u043b\u0438 (<code>admin<\/code>, <code>user<\/code>, <code>moderator<\/code>)<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0430\u0432\u0430 (<code>read<\/code>, <code>write<\/code>, <code>delete<\/code>)<\/p>\n<\/li>\n<li>\n<p>Scopes (\u0432 OAuth2)<\/p>\n<\/li>\n<li>\n<p>ACL (access control lists)<\/p>\n<\/li>\n<\/ul>\n<p><strong>\ud83e\udde0 \u041a\u0430\u043a \u043b\u0435\u0433\u043a\u043e \u0437\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u044c:<\/strong><\/p>\n<blockquote>\n<p><strong><em>\u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f &#8212; <\/em><\/strong><em>\u044d\u0442\u043e &#171;\u043f\u043e\u043a\u0430\u0436\u0438 \u043a\u0442\u043e \u0442\u044b&#187;<\/em><strong><em>, <\/em><\/strong><br \/><strong><em>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/em><\/strong><em> \u2014 \u044d\u0442\u043e \u201c\u0434\u043e\u043a\u0430\u0436\u0438, \u0447\u0442\u043e \u0442\u044b \u2014 \u044d\u0442\u043e \u0442\u044b\u201d,<\/em><br \/><strong><em>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f<\/em><\/strong><em> \u2014 \u201c\u0447\u0442\u043e \u0442\u0435\u0431\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043e\u201d<\/em><\/p>\n<\/blockquote>\n<h3>Base64<\/h3>\n<p>\u0415\u0449\u0435 \u043e\u0434\u043d\u0430 \u0448\u0442\u0443\u043a\u0430 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430 \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c.<\/p>\n<p><strong>Base64<\/strong> \u2014 \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u0442\u0435\u043a\u0441\u0442 \u0438\u043b\u0438 \u0431\u0430\u0439\u0442\u044b) \u0432 ASCII-\u0441\u0438\u043c\u0432\u043e\u043b\u044b, \u0447\u0442\u043e\u0431\u044b \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0443 (\u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430\u0445, URL, JSON \u0438 \u0442.\u043f.).<\/p>\n<blockquote>\n<p><em>\u041d\u0435 \u0437\u0430\u0449\u0438\u0449\u0430\u0435\u0442, \u043d\u0435 \u0448\u0438\u0444\u0440\u0443\u0435\u0442, \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0431\u0430\u0439\u0442\u044b \u043f\u0440\u0438\u0433\u043e\u0434\u043d\u044b\u043c\u0438 \u0434\u043b\u044f HTTP.<\/em><\/p>\n<\/blockquote>\n<p><strong>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043d\u0430\u0431\u043e\u0440 <strong>\u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 A-Z, a-z, 0-9, + \u0438 \/<\/strong>.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u0445\u043e\u0434 \u0432\u0441\u0435\u0433\u0434\u0430 <strong>\u0441\u0442\u0440\u043e\u043a\u0430<\/strong>, \u043f\u0440\u0438\u0433\u043e\u0434\u043d\u0430\u044f \u0434\u043b\u044f HTTP \u0438 JSON.<\/p>\n<\/li>\n<li>\n<p>\u0427\u0430\u0441\u0442\u043e \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 <code>=<\/code> (\u043f\u0430\u0434\u0434\u0438\u043d\u0433 \u0434\u043b\u044f \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u044f).<\/p>\n<\/li>\n<\/ul>\n<h4>\ud83e\uddd1\u200d\ud83d\udcbb \u041f\u0440\u0438\u043c\u0435\u0440\u044b:<\/h4>\n<pre><code class=\"python\"># \u041a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438: import base64  text = \"hello:world\" encoded = base64.b64encode(text.encode()).decode() print(encoded)  # aGVsbG86d29ybGQ=  # \u0414\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435: decoded = base64.b64decode(encoded).decode() print(decoded)  # hello:world <\/code><\/pre>\n<p><strong>\u26a0\ufe0f \u0412\u0430\u0436\u043d\u043e:<\/strong><\/p>\n<ul>\n<li>\n<p><strong>Base64 \u2014 \u043d\u0435 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435!<\/strong><br \/><em>\u041b\u044e\u0431\u043e\u0439 \u043c\u043e\u0436\u0435\u0442 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e. \u042d\u0442\u043e \u043d\u0435 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c, \u0430 \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438.<\/em><\/p>\n<\/li>\n<li>\n<p><strong>\u0414\u043b\u044f \u0437\u0430\u0449\u0438\u0442\u044b<\/strong> (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 JWT) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <strong>\u043f\u043e\u0434\u043f\u0438\u0441\u044c<\/strong>, \u0430 \u043d\u0435 base64.<\/p>\n<\/li>\n<\/ul>\n<h2>HTTP<\/h2>\n<p>\u0412\u0441\u0435 \u0447\u0442\u043e \u043d\u0430\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u0443\u0442 \u0437\u043d\u0430\u0442\u044c &#8212; \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b HTTP \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 <em>\u043a\u043b\u0438\u0435\u043d\u0442-\u0441\u0435\u0440\u0432\u0435\u0440<\/em>. \u041a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0435\u0433\u043e \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441, \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 \u0435\u0433\u043e \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0443.<\/p>\n<p>\u0423 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0435\u0441\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 (headers) \u0438 \u043c\u044b \u0432 \u043d\u0438\u0445 \u043c\u043e\u0436\u0435\u043c \u0447\u0442\u043e \u0442\u043e \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0414\u0430\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u0438 \u0447\u0442\u043e \u043c\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c.<\/p>\n<h2>Basic auth<\/h2>\n<p><strong>Basic Auth<\/strong> \u2014 \u044d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0432 \u043a\u0430\u0436\u0434\u043e\u043c HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0432 \u0432\u0438\u0434\u0435 base64-\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438.<\/p>\n<p>\u0418\u0434\u0435\u0430\u043b\u044c\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 <strong>\u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0438 \u0431\u044b\u0441\u0442\u0440\u043e<\/strong>, \u043d\u043e <strong>\u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0435\u0433\u043e \u0432 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0431\u0435\u0437 HTTPS.<\/strong><\/p>\n<blockquote>\n<p>Basic Auth \u2014 \u044d\u0442\u043e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0437\u043c \u0432 \u0447\u0438\u0441\u0442\u043e\u043c \u0432\u0438\u0434\u0435.<br \/><em>\u042d\u0442\u043e \u043d\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u201c\u043d\u0430 \u0433\u043e\u0434\u044b\u201d \u2014 \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u201c\u0432\u043e\u0442\u043a\u043d\u0443\u0442\u044c \u043b\u043e\u0433\u0438\u043d \u0437\u0430 30 \u0441\u0435\u043a\u0443\u043d\u0434\u201d.<\/em><\/p>\n<\/blockquote>\n<figure class=\"full-width\">\n<div><figcaption>\u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/figcaption><\/div>\n<\/figure>\n<h4>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/h4>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u0432\u043e\u0434\u0438\u0442 \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c<\/p>\n<\/li>\n<li>\n<p>\u042d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0432 base64: <code>username:password<\/code><br \/>Base64 <strong>\u043d\u0435 \u0448\u0438\u0444\u0440\u0443\u0435\u0442<\/strong>, \u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442 \u2192 \u043d\u0443\u0436\u0435\u043d HTTPS<\/p>\n<\/li>\n<li>\n<p>\u0412 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a:<\/p>\n<pre><code>Authorization: Basic YWRtaW46c2VjcmV0=<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043b\u043e\u0433\u0438\u043d\/\u043f\u0430\u0440\u043e\u043b\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0438\u043b\u0438 \u0447\u0435\u0440\u0435\u0437 middleware<\/p>\n<blockquote>\n<p><strong><em>Middleware<\/em><\/strong><em> \u2014 \u044d\u0442\u043e <\/em><strong><em>\u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0439 \u0441\u043b\u043e\u0439<\/em><\/strong><em>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 <\/em><strong><em>\u0434\u043e<\/em><\/strong><em> (\u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435) \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/em><\/p>\n<\/blockquote>\n<\/li>\n<\/ol>\n<h4>\u2705 \u041f\u043b\u044e\u0441\u044b:<\/h4>\n<ul>\n<li>\n<p>\u041d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0437\u0430 1 \u043c\u0438\u043d\u0443\u0442\u0443 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 <code><strong>Nginx<\/strong><\/code> \u0438\u043b\u0438 <code><strong>FastAPI<\/strong><\/code>)<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043a\u0443\u043a\u0438, \u0431\u0430\u0437\u044b \u0441\u0435\u0441\u0441\u0438\u0439, \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u043e\u043a\u0435\u043d\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442 HTTP \u0441 1990-\u0445, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u0445<\/p>\n<\/li>\n<\/ul>\n<h4>\u274c \u041c\u0438\u043d\u0443\u0441\u044b:<\/h4>\n<ul>\n<li>\n<p>\u0414\u0430\u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u043b\u043e\u0433\u0438\u043d\u0430 \u2014 \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u0443\u0445\u043e\u0434\u044f\u0442 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435<\/p>\n<\/li>\n<li>\n<p>\u201c\u0412\u044b\u0439\u0442\u0438\u201d \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u2014 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0447\u0438\u0441\u0442\u0438\u0432 \u043a\u044d\u0448 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430 \u0438\u043b\u0438 \u0442\u043e\u043a\u0435\u043d<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0445\u0432\u0430\u0442 \u0442\u0440\u0430\u0444\u0438\u043a\u0430 = \u043a\u0440\u0430\u0436\u0430 \u043f\u0430\u0440\u043e\u043b\u044f<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u0442 \u0440\u043e\u043b\u0435\u0439, scopes, \u0441\u0435\u0441\u0441\u0438\u0439<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u0442 revoke, \u043b\u043e\u0433\u043e\u0432, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044f, \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0438<\/p>\n<\/li>\n<\/ul>\n<h4>\u0413\u0434\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u043e:<\/h4>\n<p>\u2705 <strong>\u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442<\/strong>:<\/p>\n<ul>\n<li>\n<p>\u0410\u0434\u043c\u0438\u043d-\u043f\u0430\u043d\u0435\u043b\u0438 \u201c\u0434\u043b\u044f \u0441\u0432\u043e\u0438\u0445\u201d<\/p>\n<\/li>\n<li>\n<p>\u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b (\u0437\u0430 VPN)<\/p>\n<\/li>\n<li>\n<p>\u0411\u044b\u0441\u0442\u0440\u044b\u0435 MVP, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 API<\/p>\n<\/li>\n<\/ul>\n<p>\u274c <strong>\u041d\u0435<\/strong> <strong>\u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442<\/strong>:<\/p>\n<ul>\n<li>\n<p>\u041f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0435 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>SPA \u0438 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u044b<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0434\u0430\u043a\u0448\u043d \u0431\u0435\u0437 HTTPS<\/p>\n<\/li>\n<\/ul>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432 FastAPI:<\/h4>\n<p><a href=\"https:\/\/github.com\/takentui\/authorization_types\/tree\/fastapi-basic-auth\" rel=\"noopener noreferrer nofollow\">\u0414\u0435\u043c\u043e \u043f\u0440\u0438\u043c\u0435\u0440<\/a> <br \/><a href=\"https:\/\/fastapi.tiangolo.com\/advanced\/security\/http-basic-auth\/?h=basic+auth\" rel=\"noopener noreferrer nofollow\">\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f<\/a><\/p>\n<pre><code class=\"python\">from fastapi import FastAPI, Depends from fastapi.security import HTTPBasic, HTTPBasicCredentials  app = FastAPI() security = HTTPBasic()  @app.get(\"\/secret-resource\") def read_secret(credentials: HTTPBasicCredentials = Depends(security)):     if credentials.username == \"admin\" and credentials.password == \"secret\":         return {\"message\": \"Welcome!\"}     return {\"error\": \"Unauthorized\"}<\/code><\/pre>\n<figure class=\"full-width\">\n<div><figcaption>UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439<\/figcaption><\/div>\n<\/figure>\n<h3>\u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0432 NGINX<\/h3>\n<p>\u26a0\ufe0f\u00a0<em>\u042d\u0442\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0447\u0430\u0441\u0442\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f basic auth<\/em><\/p>\n<p><a href=\"https:\/\/github.com\/takentui\/authorization_types\/tree\/nginx-basic-auth\" rel=\"noopener noreferrer nofollow\">\u0414\u0435\u043c\u043e \u043f\u0440\u0438\u043c\u0435\u0440<\/a><br \/><a href=\"https:\/\/docs.nginx.com\/nginx\/admin-guide\/security-controls\/configuring-http-basic-authentication\/\" rel=\"noopener noreferrer nofollow\">\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f<\/a><\/p>\n<blockquote>\n<p>Nginx \u043c\u043e\u0436\u0435\u0442 \u0441\u0430\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u0434\u043e \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u043f\u0430\u0434\u0451\u0442 \u0432 backend (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, FastAPI, Django \u0438 \u0442.\u0434.).<\/p>\n<\/blockquote>\n<figure class=\"full-width\">\n<div><figcaption>\u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 basic-auth \u0432\u043c\u0435\u0441\u0442\u0435 c nginx<\/figcaption><\/div>\n<\/figure>\n<p>\u042d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e:<\/p>\n<ul>\n<li>\n<p>\u0417\u0430\u0449\u0438\u0442\u0438\u0442\u044c dev-\u0441\u0435\u0440\u0432\u0435\u0440, staging \u0438\u043b\u0438 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 API<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u201c\u0437\u0430\u0433\u043b\u0443\u0448\u043a\u0443\u201d \u043d\u0430 admin-\u043f\u0430\u043d\u0435\u043b\u044c<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435 \u043b\u0435\u0437\u0442\u044c \u0432 \u043a\u043e\u0434 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432\u043e\u043e\u0431\u0449\u0435<\/p>\n<\/li>\n<\/ul>\n<h4>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/h4>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0437\u0430\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u0437\u0430\u0449\u0438\u0449\u0451\u043d\u043d\u044b\u0439 URL<\/p>\n<\/li>\n<li>\n<p>\u0411\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043e\u043a\u043d\u043e \u0432\u0432\u043e\u0434\u0430 \u043b\u043e\u0433\u0438\u043d\u0430\/\u043f\u0430\u0440\u043e\u043b\u044f<\/p>\n<\/li>\n<li>\n<p>Nginx \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0438\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <strong>\u0444\u0430\u0439\u043b\u0430 <\/strong><code><strong>.htpasswd<\/strong><\/code><\/p>\n<\/li>\n<li>\n<p>\u0422\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043e\u0442\u0434\u0430\u0451\u0442 \u0434\u043e\u0441\u0442\u0443\u043f<\/p>\n<\/li>\n<\/ol>\n<figure class=\"full-width\">\n<div><figcaption>UML sequence \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439<\/figcaption><\/div>\n<\/figure>\n<h4>\u0412\u0430\u0436\u043d\u043e:<\/h4>\n<ul>\n<li>\n<p>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <strong>HTTPS<\/strong> \u2014 \u0438\u043d\u0430\u0447\u0435 \u043b\u043e\u0433\u0438\u043d\/\u043f\u0430\u0440\u043e\u043b\u044c \u043b\u0435\u0442\u044f\u0442 \u0432 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u043c \u0432\u0438\u0434\u0435<\/p>\n<\/li>\n<li>\n<p>\u041e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043b\u043e\u0433\u0438\u043d\/\u043f\u0430\u0440\u043e\u043b\u044c \u0443 \u0432\u0441\u0435\u0445 (\u0431\u0435\u0437 \u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f)<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u0442 \u0441\u0435\u0441\u0441\u0438\u0439, \u0440\u043e\u043b\u0435\u0439, \u0440\u0430\u0437\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u2014 \u0442\u043e\u043b\u044c\u043a\u043e &#171;\u0434\u043e\u043f\u0443\u0449\u0435\u043d \u0438\u043b\u0438 \u043d\u0435\u0442&#187;<\/p>\n<\/li>\n<\/ul>\n<h2>Session auth<\/h2>\n<p><a href=\"https:\/\/github.com\/takentui\/authorization_types\/tree\/cookie-auth\" rel=\"noopener noreferrer nofollow\">\u0414\u0435\u043c\u043e \u043f\u0440\u0438\u043c\u0435\u0440<\/a><\/p>\n<p>\u042d\u0442\u043e \u0441\u0442\u0430\u0440\u044b\u0439, \u043f\u0440\u043e\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 <strong>\u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445<\/strong> (<strong>Django<\/strong>, <strong>Flask<\/strong>, <strong>Rails<\/strong> \u0438 \u0434\u0440.)<\/p>\n<p>\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b session based auth<\/p>\n<ol>\n<li>\n<p><strong>\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043b\u043e\u0433\u0438\u043d\u0438\u0442\u0441\u044f<\/strong> \u2014 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043b\u043e\u0433\u0438\u043d\/\u043f\u0430\u0440\u043e\u043b\u044c<\/p>\n<p>\u2192 <code>POST \/login<\/code> \u0441 <code>{\"username\": \"...\", \"password\": \"...\"}<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0435\u0440\u0432\u0435\u0440 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0438\u0445<\/strong> \u0438 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 <strong>\u0441\u0435\u0441\u0441\u0438\u044e<\/strong><\/p>\n<p>\u2192 \u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438\u043b\u0438 \u0431\u0430\u0437\u0435 \u0441\u0432\u044f\u0437\u044c \u0441\u0435\u0441\u0441\u0438\u0438 \u0441 \u0443\u0447\u0435\u0442\u043a\u043e\u0439<\/p>\n<\/li>\n<li>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 <strong>\u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u0442 \u0441\u0435\u0441\u0441\u0438\u0438 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 ID<\/strong> (\u043e\u0431\u044b\u0447\u043d\u043e \u2014 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430)<\/p>\n<p>\u2192 \u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c session_id<\/p>\n<\/li>\n<li>\n<p>\u042d\u0442\u043e\u0442 ID \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0432 <strong>cookie<\/strong><\/p>\n<p>\u2192 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440,<\/p>\n<pre><code class=\"python\">HTTP\/1.1 200 OK Set-Cookie: sessionid=abc123; HttpOnly; Path=\/; Secure<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 <strong>\u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438<\/strong> \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 cookie<\/p>\n<p>\u2192 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440<\/p>\n<pre><code class=\"python\">GET \/profile HTTP\/1.1 Host: example.com Cookie: sessionid=abc123<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u043f\u043e ID \u043d\u0430\u0445\u043e\u0434\u0438\u0442 \u0441\u0435\u0441\u0441\u0438\u044e \u0438 \u201c\u0443\u0437\u043d\u0430\u0451\u0442\u201d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/p>\n<p>\u2192 \u0441\u0432\u0435\u0440\u044f\u0435\u043c \u0441\u0435\u0441\u0441\u0438\u044e \u0432 \u0431\u0430\u0437\u0435 \u0438 \u0432\u044b\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c \u0443\u0447\u0435\u0442\u043a\u0443 \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u0430 \u0441\u0435\u0441\u0441\u0438\u044f<\/p>\n<\/li>\n<\/ol>\n<h4>\u2705 \u041f\u043b\u044e\u0441\u044b<\/h4>\n<ul>\n<li>\n<p><strong>\u041f\u0440\u043e\u0441\u0442\u043e\u0442\u0430 \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f:<\/strong> \u043b\u0435\u0433\u043a\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0430 \u043c\u043d\u043e\u0433\u0438\u0445 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430\u0445<\/p>\n<\/li>\n<li>\n<p><strong>\u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c:<\/strong> \u0441\u0435\u0441\u0441\u0438\u043e\u043d\u043d\u044b\u0439 ID \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435, \u0442\u043e\u043a\u0435\u043d \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0447\u0443\u0432\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0440\u043e\u0441\u0442\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435:<\/strong> \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0430\u043d\u043d\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0435\u0441\u0441\u0438\u0438, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438<\/p>\n<\/li>\n<li>\n<p><strong>\u041c\u0435\u043d\u044c\u0448\u0430\u044f \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442:<\/strong> \u0432\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435<\/p>\n<\/li>\n<li>\n<p><strong>\u0423\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u043e\u0441\u0442\u044c \u043a XSS-\u0430\u0442\u0430\u043a\u0430\u043c:<\/strong> \u043f\u0440\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441 HttpOnly cookies<\/p>\n<\/li>\n<\/ul>\n<h4>\u274c \u041c\u0438\u043d\u0443\u0441\u044b<\/h4>\n<ul>\n<li>\n<p><strong>\u0425\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f:<\/strong> \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0435\u0441\u0441\u0438\u0439 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 (\u043f\u0430\u043c\u044f\u0442\u044c\/\u0411\u0414\/Redis)<\/p>\n<\/li>\n<li>\n<p><strong>\u041c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u044c:<\/strong> \u0441\u043b\u043e\u0436\u043d\u0435\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043c\u0435\u0436\u0434\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u043c\u0438<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441 CORS:<\/strong> \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u043f\u0440\u0438 \u043a\u0440\u043e\u0441\u0441-\u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445<\/p>\n<\/li>\n<li>\n<p><strong>\u0423\u044f\u0437\u0432\u0438\u043c\u043e\u0441\u0442\u044c \u043a CSRF-\u0430\u0442\u0430\u043a\u0430\u043c:<\/strong> \u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0440\u044b \u0437\u0430\u0449\u0438\u0442\u044b<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c:<\/strong> \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0443 \u0441\u0435\u0441\u0441\u0438\u0439<\/p>\n<\/li>\n<\/ul>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432 FastAPI:<\/h4>\n<p>\u0412\u043e\u0442 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 FastAPI:<\/p>\n<blockquote>\n<p>\u042d\u0442\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u0435\u0441\u0441\u0438\u043e\u043d\u043d\u043e\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c FastAPI \u0438 Redis \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0435\u0441\u0441\u0438\u0439.<\/p>\n<\/blockquote>\n<p><strong>\u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b:<\/strong><\/p>\n<ol>\n<li>\n<p>\u0421\u0435\u0441\u0441\u0438\u043e\u043d\u043d\u044b\u0439 ID \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a <code>UUID<\/code> \u0438 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 <code>Redis<\/code> \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0436\u0438\u0437\u043d\u0438<\/p>\n<\/li>\n<li>\n<p>\u041a\u0443\u043a\u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0441 \u0444\u043b\u0430\u0433\u0430\u043c\u0438 <code>HttpOnly<\/code> \u0438 <code>SameSite<\/code> \u0434\u043b\u044f \u0437\u0430\u0449\u0438\u0442\u044b<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u044b: \u0432\u0445\u043e\u0434, \u0432\u044b\u0445\u043e\u0434 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0441\u0435\u0441\u0441\u0438\u044f \u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0437 <code>Redis<\/code> \u0438 \u043a\u0443\u043a\u0438 \u043e\u0447\u0438\u0449\u0430\u044e\u0442\u0441\u044f<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"python\">from fastapi import FastAPI, Depends, HTTPException, status, Response, Request from fastapi.security import HTTPBasic, HTTPBasicCredentials from pydantic import BaseModel import uuid import redis from typing import Optional  app = FastAPI() security = HTTPBasic()  # \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a Redis r = redis.Redis(host='localhost', port=6379, db=0) SESSION_EXPIRE = 3600  # 1 \u0447\u0430\u0441  # \u041c\u043e\u0434\u0435\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 class User(BaseModel):     username: str     password: str  class UserInDB(User):     hashed_password: str  # \u0417\u0430\u0433\u043b\u0443\u0448\u043a\u0430 \u0434\u043b\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 fake_users_db = {     \"user1\": {         \"username\": \"user1\",         \"hashed_password\": \"password1\"  # \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u0440\u043e\u043b\u0435\u0439     } }  # \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0443\u0447\u0435\u0442\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 def get_current_user(credentials: HTTPBasicCredentials = Depends(security)):     user = fake_users_db.get(credentials.username)     if not user or user[\"hashed_password\"] != credentials.password:         raise HTTPException(             status_code=status.HTTP_401_UNAUTHORIZED,             detail=\"\u041d\u0435\u0432\u0435\u0440\u043d\u044b\u0435 \u0443\u0447\u0435\u0442\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\",             headers={\"WWW-Authenticate\": \"Basic\"},         )     return user  # \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u0441\u0435\u0441\u0441\u0438\u0438 def get_user_by_session(request: Request):  <\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-471593","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/471593","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=471593"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/471593\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=471593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=471593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=471593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}