-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathauthz.rs
188 lines (167 loc) · 4.94 KB
/
authz.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::collections::HashSet;
use crate::error::Result;
use crate::models::auth::{AuthDriverError, PgAuthDriver};
use axum::extract::Path;
use axum::response::Json;
use axum::Extension;
use editoast_authz::authorizer::Authorizer;
use editoast_authz::BuiltinRole;
use editoast_derive::EditoastError;
use super::{AuthenticationExt, AuthorizationError};
crate::routes! {
"/authz/roles" => {
"/me" => list_current_roles,
"/{user_id}" => {
list_user_roles,
grant_roles,
strip_roles,
},
},
}
editoast_common::schemas! {
BuiltinRole,
}
#[derive(serde::Deserialize, utoipa::IntoParams)]
struct UserIdPathParam {
/// A user ID (not to be mistaken for its identity, cf. editoast user model documentation)
user_id: i64,
}
#[derive(Debug, thiserror::Error, EditoastError)]
#[editoast_error(base_id = "authz")]
enum AuthzError {
#[error("Internal error")]
#[editoast_error(status = 500, no_context)]
Driver(#[from] AuthDriverError),
#[error("Authorization error")]
Authz(#[from] AuthorizationError),
}
#[derive(Debug, thiserror::Error, EditoastError)]
#[editoast_error(base_id = "authz")]
enum NoSuchUserError {
#[error("No user with ID {user_id} found")]
#[editoast_error(status = 404)]
NoSuchUser { user_id: i64 },
}
#[derive(serde::Serialize, utoipa::ToSchema)]
struct Roles {
builtin: HashSet<BuiltinRole>,
}
#[utoipa::path(
get, path = "",
tag = "authz",
responses(
(status = 200, description = "List the roles of the issuer of the request", body = inline(Roles)),
),
)]
async fn list_current_roles(Extension(auth): AuthenticationExt) -> Result<Json<Roles>> {
let authorizer = auth.authorizer()?;
Ok(Json(Roles {
builtin: authorizer
.user_builtin_roles(authorizer.user_id())
.await
.map_err(AuthzError::from)?,
}))
}
async fn check_user_exists(
user_id: i64,
authorizer: &Authorizer<PgAuthDriver<BuiltinRole>>,
) -> Result<()> {
if !authorizer
.user_exists(user_id)
.await
.map_err(AuthzError::from)?
{
return Err(NoSuchUserError::NoSuchUser { user_id }.into());
}
Ok(())
}
#[utoipa::path(
get, path = "",
tag = "authz",
params(UserIdPathParam),
responses(
(status = 200, description = "List the roles of a user", body = inline(Roles)),
),
)]
async fn list_user_roles(
Path(UserIdPathParam { user_id }): Path<UserIdPathParam>,
Extension(auth): AuthenticationExt,
) -> Result<Json<Roles>> {
if !auth
.check_roles([BuiltinRole::SubjectRead, BuiltinRole::RoleRead].into())
.await
.map_err(AuthorizationError::from)?
{
return Err(AuthorizationError::Forbidden.into());
}
let authorizer = auth.authorizer()?;
check_user_exists(user_id, &authorizer).await?;
Ok(Json(Roles {
builtin: authorizer
.user_builtin_roles(user_id)
.await
.map_err(AuthzError::from)?,
}))
}
#[derive(serde::Deserialize, utoipa::ToSchema)]
struct RoleListBody {
roles: Vec<BuiltinRole>,
}
#[utoipa::path(
post, path = "",
tag = "authz",
params(UserIdPathParam),
request_body = inline(RoleListBody),
responses(
(status = 204, description = "The roles have been granted sucessfully"),
),
)]
async fn grant_roles(
Path(UserIdPathParam { user_id }): Path<UserIdPathParam>,
Extension(auth): AuthenticationExt,
Json(RoleListBody { roles }): Json<RoleListBody>,
) -> Result<impl axum::response::IntoResponse> {
if !auth
.check_roles([BuiltinRole::SubjectRead, BuiltinRole::RoleWrite].into())
.await
.map_err(AuthorizationError::from)?
{
return Err(AuthorizationError::Forbidden.into());
}
let mut authorizer = auth.authorizer()?;
check_user_exists(user_id, &authorizer).await?;
authorizer
.grant_roles(user_id, HashSet::from_iter(roles))
.await
.map_err(AuthzError::from)?;
Ok(axum::http::StatusCode::NO_CONTENT)
}
#[utoipa::path(
delete, path = "",
tag = "authz",
params(UserIdPathParam),
request_body = inline(RoleListBody),
responses(
(status = 204, description = "The roles have been removed sucessfully"),
),
)]
async fn strip_roles(
Path(UserIdPathParam { user_id }): Path<UserIdPathParam>,
Extension(auth): AuthenticationExt,
Json(RoleListBody { roles }): Json<RoleListBody>,
) -> Result<impl axum::response::IntoResponse> {
if !auth
.check_roles([BuiltinRole::SubjectRead, BuiltinRole::RoleWrite].into())
.await
.map_err(AuthorizationError::from)?
{
return Err(AuthorizationError::Forbidden.into());
}
let mut authorizer = auth.authorizer()?;
check_user_exists(user_id, &authorizer).await?;
authorizer
.strip_roles(user_id, HashSet::from_iter(roles))
.await
.map_err(AuthzError::from)?;
Ok(axum::http::StatusCode::NO_CONTENT)
}