torrust_index_backend/web/api/v1/contexts/user/mod.rs
1//! API context: `user`.
2//!
3//! This API context is responsible for handling:
4//!
5//! - User registration
6//! - User authentication
7//! - User ban
8//!
9//! For more information about the API authentication, refer to the [`auth`](crate::web::api::v1::auth)
10//! module.
11//!
12//! # Endpoints
13//!
14//! Registration:
15//!
16//! - [Registration](#registration)
17//! - [Email verification](#email-verification)
18//!
19//! Authentication:
20//!
21//! - [Login](#login)
22//! - [Token verification](#token-verification)
23//! - [Token renewal](#token-renewal)
24//!
25//! User ban:
26//!
27//! - [Ban a user](#ban-a-user)
28//!
29//! # Registration
30//!
31//! `POST /v1/user/register`
32//!
33//! It registers a new user.
34//!
35//! **Post parameters**
36//!
37//! Name | Type | Description | Required | Example
38//! ---|---|---|---|---
39//! `username` | `String` | The username | Yes | `indexadmin`
40//! `email` | `Option<String>` | The user's email | No | `indexadmin@torrust.com`
41//! `password` | `String` | The password | Yes | `BenoitMandelbrot1924`
42//! `confirm_password` | `String` | Same password again | Yes | `BenoitMandelbrot1924`
43//!
44//! **NOTICE**: Email could be optional, depending on the configuration.
45//!
46//! ```toml
47//! [auth]
48//! email_on_signup = "Optional"
49//! min_password_length = 6
50//! max_password_length = 64
51//! ```
52//!
53//! Refer to the [`RegistrationForm`](crate::web::api::v1::contexts::user::forms::RegistrationForm)
54//! struct for more information about the registration form.
55//!
56//! **Example request**
57//!
58//! ```bash
59//! curl \
60//! --header "Content-Type: application/json" \
61//! --request POST \
62//! --data '{"username":"indexadmin","email":"indexadmin@torrust.com","password":"BenoitMandelbrot1924","confirm_password":"BenoitMandelbrot1924"}' \
63//! http://127.0.0.1:3001/v1/user/register
64//! ```
65//!
66//! For more information about the registration process, refer to the [`auth`](crate::web::api::v1::auth)
67//! module.
68//!
69//! # Email verification
70//!
71//! `GET /v1/user/email/verify/{token}`
72//!
73//! If email on signup is enabled, the user will receive an email with a
74//! verification link. The link will contain a token that can be used to verify
75//! the email address.
76//!
77//! This endpoint will verify the email address and update the user's email
78//! verification status. It also shows an text page with the result of the
79//! verification.
80//!
81//! **Example response** `200`
82//!
83//! ```text
84//! Email verified, you can close this page.
85//! ```
86//!
87//! # Login
88//!
89//! `POST /v1/user/login`
90//!
91//! It logs in a user.
92//!
93//! **Post parameters**
94//!
95//! Name | Type | Description | Required | Example
96//! ---|---|---|---|---
97//! `login` | `String` | The password | Yes | `indexadmin`
98//! `password` | `String` | The password | Yes | `BenoitMandelbrot1924`
99//!
100//! Refer to the [`LoginForm`](crate::web::api::v1::contexts::user::forms::LoginForm)
101//! struct for more information about the login form.
102//!
103//! **Example request**
104//!
105//! ```bash
106//! curl \
107//! --header "Content-Type: application/json" \
108//! --request POST \
109//! --data '{"login":"indexadmin","password":"BenoitMandelbrot1924"}' \
110//! http://127.0.0.1:3001/v1/user/login
111//! ```
112//!
113//! For more information about the login process, refer to the [`auth`](crate::web::api::v1::auth)
114//! module.
115//!
116//! # Token verification
117//!
118//! `POST /v1/user/token/verify`
119//!
120//! It logs in a user.
121//!
122//! **Post parameters**
123//!
124//! Name | Type | Description | Required | Example
125//! ---|---|---|---|---
126//! `token` | `String` | The token you want to verify | Yes | `eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7InVzZXJfaWQiOjEsInVzZXJuYW1lIjoiaW5kZXhhZG1pbiIsImFkbWluaXN0cmF0b3IiOnRydWV9LCJleHAiOjE2ODYyMTU3ODh9.4k8ty27DiWwOk4WVcYEhIrAndhpXMRWnLZ3i_HlJnvI`
127//!
128//! Refer to the [`JsonWebToken`](crate::web::api::v1::contexts::user::forms::JsonWebToken)
129//! struct for more information about the token.
130//!
131//! **Example request**
132//!
133//! ```bash
134//! curl \
135//! --header "Content-Type: application/json" \
136//! --request POST \
137//! --data '{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7InVzZXJfaWQiOjEsInVzZXJuYW1lIjoiaW5kZXhhZG1pbiIsImFkbWluaXN0cmF0b3IiOnRydWV9LCJleHAiOjE2ODYyMTU3ODh9.4k8ty27DiWwOk4WVcYEhIrAndhpXMRWnLZ3i_HlJnvI"}' \
138//! http://127.0.0.1:3001/v1/user/token/verify
139//! ```
140//!
141//! **Example response** `200`
142//!
143//! For a valid token:
144//!
145//! ```json
146//! {
147//! "data":"Token is valid."
148//! }
149//! ```
150//!
151//! And for an invalid token:
152//!
153//! ```json
154//! {
155//! "data":"Token invalid."
156//! }
157//! ```
158//!
159//! # Token renewal
160//!
161//! `POST /v1/user/token/verify`
162//!
163//! It renew a user's token.
164//!
165//! The token must be valid and not expired. And it's only renewed if it is
166//! valid for less than one week.
167//!
168//! **Post parameters**
169//!
170//! Name | Type | Description | Required | Example
171//! ---|---|---|---|---
172//! `token` | `String` | The current valid token | Yes | `eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7InVzZXJfaWQiOjEsInVzZXJuYW1lIjoiaW5kZXhhZG1pbiIsImFkbWluaXN0cmF0b3IiOnRydWV9LCJleHAiOjE2ODYyMTU3ODh9.4k8ty27DiWwOk4WVcYEhIrAndhpXMRWnLZ3i_HlJnvI`
173//!
174//! Refer to the [`JsonWebToken`](crate::web::api::v1::contexts::user::forms::JsonWebToken)
175//! struct for more information about the token.
176//!
177//! **Example request**
178//!
179//! ```bash
180//! curl \
181//! --header "Content-Type: application/json" \
182//! --request POST \
183//! --data '{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7InVzZXJfaWQiOjEsInVzZXJuYW1lIjoiaW5kZXhhZG1pbiIsImFkbWluaXN0cmF0b3IiOnRydWV9LCJleHAiOjE2ODYyMTU3ODh9.4k8ty27DiWwOk4WVcYEhIrAndhpXMRWnLZ3i_HlJnvI"}' \
184//! http://127.0.0.1:3001/v1/user/token/renew
185//! ```
186//!
187//! **Example response** `200`
188//!
189//! If you try to renew a token that is still valid for more than one week:
190//!
191//! ```json
192//! {
193//! "data": {
194//! "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7InVzZXJfaWQiOjEsInVzZXJuYW1lIjoiaW5kZXhhZG1pbiIsImFkbWluaXN0cmF0b3IiOnRydWV9LCJleHAiOjE2ODYyMTU3ODh9.4k8ty27DiWwOk4WVcYEhIrAndhpXMRWnLZ3i_HlJnvI",
195//! "username": "indexadmin",
196//! "admin": true
197//! }
198//! }
199//! ```
200//!
201//! You will get the same token. If a new token is generated, the response will
202//! be the same but with the new token.
203//!
204//! **WARNING**: The token is associated to the user's role. The application does not support
205//! changing the role of a user. If you change the user's role manually in the
206//! database, the token will still be valid but with the same role. That should
207//! only be done for testing purposes.
208//!
209//! # Ban a user
210//!
211//! `DELETE /v1/user/ban/{user}`
212//!
213//! It add a user to the banned user list.
214//!
215//! Only admin can ban other users.
216//!
217//! **Path parameters**
218//!
219//! Name | Type | Description | Required | Example
220//! ---|---|---|---|---
221//! `user` | `String` | username | Yes | `indexadmin`
222//!
223//! **Example request**
224//!
225//! ```bash
226//! curl \
227//! --header "Content-Type: application/json" \
228//! --header "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7InVzZXJfaWQiOjEsInVzZXJuYW1lIjoiaW5kZXhhZG1pbiIsImFkbWluaXN0cmF0b3IiOnRydWV9LCJleHAiOjE2ODYyMTU3ODh9.4k8ty27DiWwOk4WVcYEhIrAndhpXMRWnLZ3i_HlJnvI" \
229//! --request DELETE \
230//! http://127.0.0.1:3001/v1/user/ban/indexadmin
231//! ```
232//!
233//! **Example response** `200`
234//!
235//! If you try to renew a token that is still valid for more than one week:
236//!
237//! ```json
238//! {
239//! "data": "Banned user: indexadmin"
240//! }
241//! ```
242//!
243//! **WARNING**: The admin can ban themselves. If they do, they will not be able
244//! to unban themselves. The only way to unban themselves is to manually remove
245//! the user from the banned user list in the database.
246pub mod forms;
247pub mod handlers;
248pub mod responses;
249pub mod routes;