|
1 |
| -# Аутентификация и авторизация |
| 1 | +# Authentication and authorization |
2 | 2 |
|
3 |
| -::: details Что такое аутентификация и авторизация |
4 |
| -Грубо говоря: |
| 3 | +::: details What is authentication and authorization |
| 4 | +Roughly speaking: |
5 | 5 |
|
6 |
| -Аутентификация - когда система удостоверяется, что в неё залогинился именно Вася Пупкин, например, проверив его логин и пароль |
| 6 | +Authentication is when the system verifies that it is Vasya Pupkin who logged in, for example, by checking his username and password |
7 | 7 |
|
8 |
| -Авторизация - когда система удостоверяется, что запрашивающий ресурс/действие пользователь имеет право доступа к нему. Например, старший менеджер имеет право удаления товара в админке, а простой менеджер - нет. |
| 8 | +Authorization - when the system verifies that the user requesting the resource/action has the right to access it. For example, a senior manager has the right to delete an item in the admin, but a simple manager does not. |
9 | 9 | :::
|
10 | 10 |
|
11 |
| -::: details Как сделать систему аутентификации на сайта |
| 11 | +:::: details How to make an authentication system on the site |
12 | 12 |
|
13 |
| -Для SPA самый распространенный способ - на JWT токенах. Вариантов это сделать много, в зависимости от требований уровня безопасности (личный блог Васи Пупкина с комментариями гостей и онлайн магазин с депозитами и бонусами - две большие разницы). |
| 13 | +For SPA the most common way - on JWT tokens. There are many variants of this, depending on the requirements of the security level (Vasya Pupkin's personal blog with guest comments and online store with deposits and bonuses - two big differences). |
14 | 14 |
|
15 |
| -JWT - это стандарт записи небольшого количества информации в строку (токен) и подписывания её (криптография). Делает это бэкенд. Таким образом только бэкенд может удостовериться, что токен выписан им, и в нем действительная информация. |
| 15 | +JWT is a standard for writing a small amount of information into a string (token) and signing it (cryptography). It is done by the backend. So only the backend can make sure that the token is written by it and has valid information in it. |
16 | 16 |
|
17 |
| -Стандарта аутентификация на токенах как такового нет, есть лучшие практики. |
| 17 | +There is no standard for token authentication as such, there are best practices. |
18 | 18 |
|
19 |
| -Варианты: |
| 19 | +Options: |
20 | 20 |
|
21 |
| -1. Бэкенд генерит токен (access token - AT) и кладет в httpOnly cookie. Фронтэнд доступа к токену не имеет, браузер просто возвращает куку. Метод незаслужено редко используемый, но вполне надежный. С фронта снимаются все заморочки по манипуляции с АТ. |
| 21 | +1. backend generates token (access token - AT) and puts it in httpOnly cookie. The frontend does not have access to the token, the browser just returns the cookie. This method is undeservedly rarely used, but it is quite reliable. The frontend removes all the hassles of manipulating AT. |
22 | 22 |
|
23 |
| -Сценарий: |
| 23 | +Scenario: |
24 | 24 |
|
25 |
| -- Фронт логинится, получает данные пользователя с бэка и работает с ними. При получении при любом запросе 401 - направляет пользователя на форму логина, обнуляет данные пользователя. |
26 |
| -- Бэк при логине создает httpOnly cookie с токеном с определенным сроком жизни. На каждом запросе проверяет токен, определяет того, кому выписан этот токен и его права, и дальше решает разрешать ли доступ. Если срок жизни токена кончился - возвращает 401. |
| 25 | +- Front logs in, gets user data from the backend and works with it. When receiving any 401 request, it directs the user to the login form and resets the user data to zero. |
| 26 | +- Back at login creates httpOnly cookie with token with certain lifetime. At each request it checks the token, determines the person to whom this token is issued and his rights, and then decides whether to allow access. If the lifetime of the token has expired, it returns 401. |
27 | 27 |
|
28 |
| -2. Бэкенд генерит токен (access token - AT) и передаёт фронту. Фронт сохраняет его и каждый раз отправляет его обратно. По сути то же самое, что и в первом случае, плюс лишние телодвижения и возможность потерять токен через XSS. |
| 28 | +2 The backend generates an access token (AT) and passes it to the front. The front saves it and sends it back each time. In essence the same as in the first case, plus unnecessary steps and the possibility of losing the token through XSS. |
29 | 29 |
|
30 |
| -3. Используются два токена - короткоживущий access token (AT) и дольше живущий refresh token (RT) пересылаемый в httpOnly cookie. AT работает как во втором случае. Когда записанный в нем срок его жизни кончается, бэкенд проверяет RT, и если он валиден, обновляет AT. Кончается RT - пользователь направляется на перелогин. |
| 30 | +3. two tokens are used - short-lived access token (AT) and longer-lived refresh token (RT) sent in httpOnly cookie. AT works as in the second case. When its lifetime expires, the backend checks the RT and if it is valid, refreshes the AT. RT expires - the user is sent to re-login. |
31 | 31 |
|
32 |
| -Подробнее по данной теме можно ознакомиться в, например, [этой статье](https://habr.com/ru/articles/710552/) |
| 32 | +More details on this topic can be found in, for example, [this article](https://habr.com/ru/articles/710552/). |
33 | 33 |
|
34 | 34 | :::
|
35 | 35 |
|
36 |
| -::: details Где хранить access token на фронте? |
| 36 | +:::: details Where to store access token on the frontend? |
37 | 37 |
|
38 |
| -В 95% случаев в LocalStorage |
| 38 | +In 95% of cases in LocalStorage |
39 | 39 |
|
40 | 40 | :::
|
41 | 41 |
|
42 |
| -::: details Что такое oAuth и SSO? |
| 42 | +:::: details What is oAuth and SSO? |
43 | 43 |
|
44 |
| -oAuth - Логин "через Google". Также когда, например, у пользователь есть возможность на твоем сайте сохранить что-то в Google Drive. Чтобы получить разрешение для твоего сайта работы с Google Drive аккаунтом пользователя, ты просишь его дать через Google разрешения твоему сайту лазить в его аккаунт. Итого у нас есть четыре части - Google (авторизационный центр), сторонний ресурс (Google Drive), твой сайт и пользователь |
| 44 | +oAuth - Login "via Google". Also when, for example, a user has the option on your site to save something to Google Drive. In order to get permission for your site to work with the user's Google Drive account, you ask the user to allow your site to access their account via Google. So we have four parts - Google (the authorization center), the third-party resource (Google Drive), your site, and the user |
45 | 45 |
|
46 |
| -SSO - single sign-on - логин пользователя на портал некой компании один раз, и затем прозрачный доступ на разные его сервисы. Например, логин в GMail и доступ в сразу в Google Диск, Google Photo и так далее. |
| 46 | +SSO - single sign-on - a user logs in to a certain company's portal once, and then has transparent access to its different services. For example, login to GMail and access to Google Drive, Google Photo and so on. |
47 | 47 |
|
48 | 48 | :::
|
49 | 49 |
|
50 |
| -::: details Дает ли аутентификация через JWT безопасность? |
| 50 | +:::: details Does authentication via JWT give security? |
51 | 51 |
|
52 |
| -Безопасность - очень комплексное понятие, аутентификация через JWT - всего лишь один из её элементов. Для критичных приложений можно снимать отпечаток системы пользователя (browser fingerprint) и заставлять пользователя перелогиниваться как только он изменился. То же самое с IP. Таким образом можно бороться с кражей АТ. Также ставить очень маленький срок жизни AT, если используется RT. Но основные меры безопасности связаны не с аутентификацией, а с постоянным мониторингом системы на подозрительные действия. |
| 52 | +Security is a very complex concept, and JWT authentication is just one of its elements. For critical applications, it is possible to capture the user's browser fingerprint and force the user to re-login as soon as they change. Same with IP. In this way you can fight AT theft. Also set very small AT lifetime if RT is used. But the main security measures are not related to authentication, but to constant monitoring of the system for suspicious actions. |
53 | 53 |
|
54 | 54 | :::
|
55 | 55 |
|
56 |
| -::: details Авторизация |
| 56 | +:::: details Authorization |
57 | 57 |
|
58 |
| -Если нужно давать пользователям разный уровень доступа к ресурсам сайта, то обычно применяется или RBAC (Role-based access control) или PBA - Policy-Based Authorization (Permission-Based Authorization). |
| 58 | +If it is necessary to give users different levels of access to site resources, it is common to use either RBAC (Role-based access control) or PBA (Policy-Based Authorization). |
59 | 59 |
|
60 |
| -В первом случае пользователи распределяются по ролям (админ, менеджер, юзер) и уровень доступа определяется ролью. |
| 60 | +In the first case, users are assigned to roles (admin, manager, user) and the level of access is determined by the role. |
61 | 61 |
|
62 |
| -Во-втором можно более гранулировано задать разрешение каждому пользователю индивидуально на любое действие. |
| 62 | +In the second case, it is possible to set permissions for each user individually for any action in a more granular way. |
63 | 63 |
|
64 |
| -Нужно понимать, что на фронте авторизация делается для удобства (пользователь не видит разделы сайта, которые ему запрещены), но за безопасность отвечает бэк - он должен проверять каждый раз, имеет ли пользователь право доступа к запрашиваемому ресурсу, даже если на фронте его как бы видно не должно быть. Сломать фронт - очень несложно. |
| 64 | +It should be understood that on the front authorization is done for convenience (the user does not see the sections of the site that are forbidden to him), but the back is responsible for security - he must check each time whether the user has the right to access the requested resource, even if on the front it should not be visible. It is not very difficult to break the front end. |
65 | 65 |
|
66 | 66 | :::
|
0 commit comments