eqeqo-api-auth 0.0.1

Centralized authentication and authorization API for multiple independent services. Manages users, roles, permissions, and service associations.
Documentation
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
use httpageboy::{Request, Response, StatusCode};
use serde::Deserialize;
use serde_json::json;
use sqlx::types::Json;

use super::roles::Role;
use super::users::User;
use super::{
  FlexibleId, error_response, require_token_with_renew, resolve_permission_id, resolve_person_id,
  resolve_service_id,
};

#[derive(Deserialize)]
pub struct ServiceRolePayload {
  service_id: FlexibleId,
  role_id: i32,
}

pub async fn assign_role_to_service(req: &Request) -> Response {
  let (db, _, _) = match require_token_with_renew(req).await {
    Ok(tuple) => tuple,
    Err(response) => return response,
  };
  let payload: ServiceRolePayload = match serde_json::from_slice(req.body.as_bytes()) {
    Ok(p) => p,
    Err(_) => return error_response(StatusCode::BadRequest, "invalid_request_body"),
  };
  let service_id = match resolve_service_id(&db, &payload.service_id, true).await {
    Ok(id) => id,
    Err(response) => return response,
  };
  match sqlx::query("CALL auth.assign_role_to_service($1, $2)")
    .bind(service_id)
    .bind(payload.role_id)
    .execute(db.pool())
    .await
  {
    Ok(_) => Response {
      status: StatusCode::Ok.to_string(),
      content_type: "application/json".to_string(),
      content: json!({ "status": "success" }).to_string().into_bytes(),
    },
    Err(_) => error_response(
      StatusCode::InternalServerError,
      "assign_role_service_failed",
    ),
  }
}

pub async fn remove_role_from_service(req: &Request) -> Response {
  let (db, _, _) = match require_token_with_renew(req).await {
    Ok(tuple) => tuple,
    Err(response) => return response,
  };
  let payload: ServiceRolePayload = match serde_json::from_slice(req.body.as_bytes()) {
    Ok(p) => p,
    Err(_) => return error_response(StatusCode::BadRequest, "invalid_request_body"),
  };
  let service_id = match resolve_service_id(&db, &payload.service_id, false).await {
    Ok(id) => id,
    Err(response) => return response,
  };
  match sqlx::query("CALL auth.remove_role_from_service($1, $2)")
    .bind(service_id)
    .bind(payload.role_id)
    .execute(db.pool())
    .await
  {
    Ok(_) => Response {
      status: StatusCode::Ok.to_string(),
      content_type: "application/json".to_string(),
      content: json!({ "status": "role_removed_from_service" })
        .to_string()
        .into_bytes(),
    },
    Err(_) => error_response(
      StatusCode::InternalServerError,
      "remove_role_service_failed",
    ),
  }
}

pub async fn list_service_roles(req: &Request) -> Response {
  let (db, _, _) = match require_token_with_renew(req).await {
    Ok(tuple) => tuple,
    Err(response) => return response,
  };
  let identifier = match req.params.get("id") {
    Some(id) => FlexibleId::from(id.clone()),
    None => return error_response(StatusCode::BadRequest, "invalid_service_id"),
  };
  let id = match resolve_service_id(&db, &identifier, false).await {
    Ok(id) => id,
    Err(response) => return response,
  };
  match sqlx::query_as::<_, Role>("SELECT * FROM auth.list_service_roles($1)")
    .bind(id)
    .fetch_all(db.pool())
    .await
  {
    Ok(roles) => Response {
      status: StatusCode::Ok.to_string(),
      content_type: "application/json".to_string(),
      content: serde_json::to_vec(&roles).unwrap(),
    },
    Err(_) => error_response(StatusCode::InternalServerError, "list_service_roles_failed"),
  }
}

#[derive(Deserialize)]
pub struct PersonServiceRolePayload {
  person_id: FlexibleId,
  service_id: FlexibleId,
  role_id: i32,
}

pub async fn assign_role_to_person_in_service(req: &Request) -> Response {
  let (db, _, _) = match require_token_with_renew(req).await {
    Ok(tuple) => tuple,
    Err(response) => return response,
  };
  let payload: PersonServiceRolePayload = match serde_json::from_slice(req.body.as_bytes()) {
    Ok(p) => p,
    Err(_) => return error_response(StatusCode::BadRequest, "invalid_request_body"),
  };
  let person_id = match resolve_person_id(&db, &payload.person_id).await {
    Ok(id) => id,
    Err(response) => return response,
  };
  let service_id = match resolve_service_id(&db, &payload.service_id, false).await {
    Ok(id) => id,
    Err(response) => return response,
  };
  match sqlx::query("CALL auth.assign_role_to_person_in_service($1, $2, $3)")
    .bind(person_id)
    .bind(service_id)
    .bind(payload.role_id)
    .execute(db.pool())
    .await
  {
    Ok(_) => Response {
      status: StatusCode::Ok.to_string(),
      content_type: "application/json".to_string(),
      content: json!({ "status": "success" }).to_string().into_bytes(),
    },
    Err(_) => error_response(StatusCode::InternalServerError, "assign_role_person_failed"),
  }
}

pub async fn remove_role_from_person_in_service(req: &Request) -> Response {
  let (db, _, _) = match require_token_with_renew(req).await {
    Ok(tuple) => tuple,
    Err(response) => return response,
  };
  let payload: PersonServiceRolePayload = match serde_json::from_slice(req.body.as_bytes()) {
    Ok(p) => p,
    Err(_) => return error_response(StatusCode::BadRequest, "invalid_request_body"),
  };
  let person_id = match resolve_person_id(&db, &payload.person_id).await {
    Ok(id) => id,
    Err(response) => return response,
  };
  let service_id = match resolve_service_id(&db, &payload.service_id, false).await {
    Ok(id) => id,
    Err(response) => return response,
  };
  match sqlx::query("CALL auth.remove_role_from_person_in_service($1, $2, $3)")
    .bind(person_id)
    .bind(service_id)
    .bind(payload.role_id)
    .execute(db.pool())
    .await
  {
    Ok(_) => Response {
      status: StatusCode::Ok.to_string(),
      content_type: "application/json".to_string(),
      content: json!({ "status": "role_removed_from_person" })
        .to_string()
        .into_bytes(),
    },
    Err(_) => error_response(StatusCode::InternalServerError, "remove_role_person_failed"),
  }
}

pub async fn list_person_roles_in_service(req: &Request) -> Response {
  let (db, _, _) = match require_token_with_renew(req).await {
    Ok(tuple) => tuple,
    Err(response) => return response,
  };
  let person_identifier = match req.params.get("person_id") {
    Some(value) => FlexibleId::from(value.clone()),
    None => return error_response(StatusCode::BadRequest, "invalid_person_id"),
  };
  let service_identifier = match req.params.get("service_id") {
    Some(value) => FlexibleId::from(value.clone()),
    None => return error_response(StatusCode::BadRequest, "invalid_service_id"),
  };
  let person_id = match resolve_person_id(&db, &person_identifier).await {
    Ok(id) => id,
    Err(response) => return response,
  };
  let service_id = match resolve_service_id(&db, &service_identifier, false).await {
    Ok(id) => id,
    Err(response) => return response,
  };
  match sqlx::query_as::<_, Role>("SELECT * FROM auth.list_person_roles_in_service($1, $2)")
    .bind(person_id)
    .bind(service_id)
    .fetch_all(db.pool())
    .await
  {
    Ok(roles) => Response {
      status: StatusCode::Ok.to_string(),
      content_type: "application/json".to_string(),
      content: serde_json::to_vec(&roles).unwrap(),
    },
    Err(_) => error_response(StatusCode::InternalServerError, "list_person_roles_failed"),
  }
}

pub async fn list_persons_with_role_in_service(req: &Request) -> Response {
  let (db, _, _) = match require_token_with_renew(req).await {
    Ok(tuple) => tuple,
    Err(response) => return response,
  };
  let service_identifier = match req.params.get("service_id") {
    Some(value) => FlexibleId::from(value.clone()),
    None => return error_response(StatusCode::BadRequest, "invalid_service_id"),
  };
  let role_id: i32 = match req.params.get("role_id").and_then(|s| s.parse().ok()) {
    Some(id) => id,
    None => return error_response(StatusCode::BadRequest, "invalid_role_id"),
  };
  let service_id = match resolve_service_id(&db, &service_identifier, false).await {
    Ok(id) => id,
    Err(response) => return response,
  };
  match sqlx::query_as::<_, User>(
    "SELECT id, username, name FROM auth.list_persons_with_role_in_service($1, $2)",
  )
  .bind(service_id)
  .bind(role_id)
  .fetch_all(db.pool())
  .await
  {
    Ok(users) => Response {
      status: StatusCode::Ok.to_string(),
      content_type: "application/json".to_string(),
      content: serde_json::to_vec(&users).unwrap(),
    },
    Err(_) => error_response(
      StatusCode::InternalServerError,
      "list_persons_with_role_failed",
    ),
  }
}

#[derive(Deserialize)]
pub struct PersonServicePermissionPayload {
  person_id: FlexibleId,
  service_id: FlexibleId,
  permission_id: Option<FlexibleId>,
  permission_name: Option<String>,
}

async fn ensure_direct_role(
  db: &crate::database::DB,
  person_id: i32,
  service_id: i32,
) -> Result<i32, Response> {
  let role_name = format!("direct:{}:{}", person_id, service_id);

  let existing = sqlx::query_scalar::<_, i32>("SELECT id FROM auth.role WHERE name = $1")
    .bind(&role_name)
    .fetch_optional(db.pool())
    .await
    .map_err(|_| {
      error_response(
        StatusCode::InternalServerError,
        "resolve_direct_role_failed",
      )
    })?;
  if let Some(id) = existing {
    return Ok(id);
  }

  let inserted = sqlx::query_scalar::<_, i32>(
    "INSERT INTO auth.role (name) VALUES ($1)
      ON CONFLICT (name) DO NOTHING
      RETURNING id",
  )
  .bind(&role_name)
  .fetch_optional(db.pool())
  .await
  .map_err(|_| error_response(StatusCode::InternalServerError, "create_direct_role_failed"))?;

  if let Some(id) = inserted {
    return Ok(id);
  }

  sqlx::query_scalar::<_, i32>("SELECT id FROM auth.role WHERE name = $1")
    .bind(&role_name)
    .fetch_one(db.pool())
    .await
    .map_err(|_| {
      error_response(
        StatusCode::InternalServerError,
        "resolve_direct_role_failed",
      )
    })
}

pub async fn grant_permission_to_person_in_service(req: &Request) -> Response {
  let (db, _, _) = match require_token_with_renew(req).await {
    Ok(tuple) => tuple,
    Err(response) => return response,
  };

  let payload: PersonServicePermissionPayload = match serde_json::from_slice(req.body.as_bytes()) {
    Ok(p) => p,
    Err(_) => return error_response(StatusCode::BadRequest, "invalid_request_body"),
  };

  let person_id = match resolve_person_id(&db, &payload.person_id).await {
    Ok(id) => id,
    Err(response) => return response,
  };

  let service_id = match resolve_service_id(&db, &payload.service_id, false).await {
    Ok(id) => id,
    Err(response) => return response,
  };

  let permission_identifier = match (payload.permission_id, payload.permission_name) {
    (Some(id), _) => id,
    (None, Some(name)) => FlexibleId::from(name),
    _ => return error_response(StatusCode::BadRequest, "invalid_request_body"),
  };

  let permission_id = match resolve_permission_id(&db, &permission_identifier).await {
    Ok(id) => id,
    Err(response) => return response,
  };

  let role_id = match ensure_direct_role(&db, person_id, service_id).await {
    Ok(id) => id,
    Err(response) => return response,
  };

  if let Err(_) = sqlx::query(
    "INSERT INTO auth.service_roles (service_id, role_id) VALUES ($1, $2)
      ON CONFLICT (service_id, role_id) DO NOTHING",
  )
  .bind(service_id)
  .bind(role_id)
  .execute(db.pool())
  .await
  {
    return error_response(StatusCode::InternalServerError, "link_role_service_failed");
  }

  if let Err(_) = sqlx::query(
    "INSERT INTO auth.role_permission (role_id, permission_id) VALUES ($1, $2)
      ON CONFLICT (role_id, permission_id) DO NOTHING",
  )
  .bind(role_id)
  .bind(permission_id)
  .execute(db.pool())
  .await
  {
    return error_response(StatusCode::InternalServerError, "assign_permission_failed");
  }

  if let Err(_) = sqlx::query(
    "INSERT INTO auth.person_service_role (person_id, service_id, role_id) VALUES ($1, $2, $3)
      ON CONFLICT (person_id, service_id, role_id) DO NOTHING",
  )
  .bind(person_id)
  .bind(service_id)
  .bind(role_id)
  .execute(db.pool())
  .await
  {
    return error_response(StatusCode::InternalServerError, "assign_role_person_failed");
  }

  Response {
    status: StatusCode::Ok.to_string(),
    content_type: "application/json".to_string(),
    content: json!({
      "status": "permission_granted",
      "person_id": person_id,
      "service_id": service_id,
      "permission_id": permission_id,
      "role_id": role_id
    })
    .to_string()
    .into_bytes(),
  }
}

#[derive(sqlx::FromRow, serde::Serialize)]
struct PersonData {
  id: i32,
  username: String,
  name: String,
}

#[derive(sqlx::FromRow)]
struct PermissionsCacheRow {
  permissions: Json<Vec<String>>,
  modified_at: i64,
}

pub async fn get_person_service_info(req: &Request) -> Response {
  let (db, validation, _) = match require_token_with_renew(req).await {
    Ok(result) => result,
    Err(response) => return response,
  };

  let person_identifier = match req.params.get("person_id") {
    Some(value) => FlexibleId::from(value.clone()),
    None => return error_response(StatusCode::BadRequest, "invalid_person_id"),
  };

  let service_identifier = match req.params.get("service_id") {
    Some(value) => FlexibleId::from(value.clone()),
    None => return error_response(StatusCode::BadRequest, "invalid_service_id"),
  };

  let person_id = match resolve_person_id(&db, &person_identifier).await {
    Ok(id) => id,
    Err(response) => return response,
  };

  if let Some(token_user_id) = validation
    .record
    .payload
    .get("user_id")
    .and_then(|v| v.as_i64())
  {
    if token_user_id as i32 != person_id {
      return error_response(StatusCode::Forbidden, "forbidden_person_lookup");
    }
  }

  let service_id = match resolve_service_id(&db, &service_identifier, false).await {
    Ok(id) => id,
    Err(response) => return response,
  };

  let person = match sqlx::query_as::<_, PersonData>(
    "SELECT id, username, name FROM auth.person WHERE id = $1 AND removed_at IS NULL",
  )
  .bind(person_id)
  .fetch_optional(db.pool())
  .await
  {
    Ok(Some(person)) => person,
    Ok(None) => return error_response(StatusCode::NotFound, "person_not_found"),
    Err(_) => return error_response(StatusCode::InternalServerError, "load_person_failed"),
  };

  let cached_permissions = match sqlx::query_as::<_, PermissionsCacheRow>(
    "SELECT permissions, modified_at FROM auth.permissions_cache WHERE token = $1 AND service_id = $2",
  )
  .bind(&validation.record.token)
  .bind(service_id)
  .fetch_optional(db.pool())
  .await
  {
    Ok(Some(cache)) if cache.modified_at == validation.record.modified_at => {
      Some(cache.permissions.0)
    }
    Ok(Some(_)) => None,
    Ok(None) => None,
    Err(_) => {
      return error_response(
        StatusCode::InternalServerError,
        "load_permissions_cache_failed",
      );
    }
  };

  let permissions = if let Some(perms) = cached_permissions {
    perms
  } else {
    let perms = match sqlx::query_scalar::<_, String>(
      "SELECT name FROM (
        SELECT DISTINCT p.id, p.name
        FROM auth.person_service_role psr
        JOIN auth.role_permission rp ON rp.role_id = psr.role_id
        JOIN auth.permission p ON p.id = rp.permission_id
        WHERE psr.person_id = $1 AND psr.service_id = $2
      ) perms
      ORDER BY id",
    )
    .bind(person_id)
    .bind(service_id)
    .fetch_all(db.pool())
    .await
    {
      Ok(perms) => perms,
      Err(_) => return error_response(StatusCode::InternalServerError, "load_permissions_failed"),
    };

    if let Err(_) = sqlx::query(
      "INSERT INTO auth.permissions_cache (token, service_id, permissions, modified_at)
        VALUES ($1, $2, $3, $4)
        ON CONFLICT (token, service_id)
        DO UPDATE SET permissions = EXCLUDED.permissions, modified_at = EXCLUDED.modified_at",
    )
    .bind(&validation.record.token)
    .bind(service_id)
    .bind(Json(&perms))
    .bind(validation.record.modified_at)
    .execute(db.pool())
    .await
    {
      return error_response(
        StatusCode::InternalServerError,
        "store_permissions_cache_failed",
      );
    }
    perms
  };

  let roles = match sqlx::query_scalar::<_, String>(
    "SELECT r.name FROM auth.person_service_role psr
      JOIN auth.role r ON r.id = psr.role_id
      WHERE psr.person_id = $1 AND psr.service_id = $2",
  )
  .bind(person_id)
  .bind(service_id)
  .fetch_all(db.pool())
  .await
  {
    Ok(list) => list,
    Err(_) => {
      return error_response(StatusCode::InternalServerError, "load_roles_failed");
    }
  };

  Response {
    status: StatusCode::Ok.to_string(),
    content_type: "application/json".to_string(),
    content: json!({
      "user": person,
      "service_id": service_id,
      "roles": roles,
      "permissions": permissions,
    })
    .to_string()
    .into_bytes(),
  }
}