diff --git a/iam-common/src/lib.rs b/iam-common/src/lib.rs index 951ce35..a5eb334 100644 --- a/iam-common/src/lib.rs +++ b/iam-common/src/lib.rs @@ -4,5 +4,6 @@ pub mod error; mod id; pub mod password; pub mod token; +pub mod user; pub use id::*; diff --git a/iam-common/src/user.rs b/iam-common/src/user.rs new file mode 100644 index 0000000..70af5e3 --- /dev/null +++ b/iam-common/src/user.rs @@ -0,0 +1,22 @@ +use crate::error::{self, Result}; +use iam_entity::users; +use sea_orm::{ConnectionTrait, EntityTrait, FromQueryResult}; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, FromQueryResult, Serialize, Deserialize, PartialEq, Eq)] +pub struct UserInfo { + pub id: String, + pub name: String, + pub email: String, +} + +pub async fn get_user(db: &D, id: &str) -> Result +where + D: ConnectionTrait, +{ + users::Entity::find_by_id(id.to_owned()) + .into_model() + .one(db) + .await? + .ok_or(error::USER_NOT_FOUND) +} diff --git a/iam/src/handlers/v1/users/id/get.rs b/iam/src/handlers/v1/users/id/get.rs index f090e3d..22935cd 100644 --- a/iam/src/handlers/v1/users/id/get.rs +++ b/iam/src/handlers/v1/users/id/get.rs @@ -1,8 +1,6 @@ use crate::{json::Json, shared::SharedTrait}; use axum::{extract::Path, Extension}; -use iam_common::error::{self, Result}; -use iam_entity::users; -use sea_orm::entity::EntityTrait; +use iam_common::{error::Result, user::UserInfo}; use serde::Serialize; #[derive(Serialize, Debug)] @@ -15,15 +13,6 @@ pub struct GetUserResponse { pub async fn get_user( Extension(shared): Extension, Path(id): Path, -) -> Result> { - let res = users::Entity::find_by_id(id) - .one(shared.db()) - .await? - .ok_or(error::USER_NOT_FOUND)?; - - Ok(Json(GetUserResponse { - id: res.id, - name: res.name, - email: res.email, - })) +) -> Result> { + Ok(Json(iam_common::user::get_user(shared.db(), &id).await?)) } diff --git a/libiam/src/app.rs b/libiam/src/app.rs index 1ff21ad..c071fb3 100644 --- a/libiam/src/app.rs +++ b/libiam/src/app.rs @@ -1,12 +1,13 @@ use std::sync::Arc; +use iam_common::user::UserInfo; use reqwest::Client; use serde::Deserialize; use serde_json::json; use crate::{ error::{unwrap_res, ErrorMessage, Result}, - utils::Either, + utils::{create_client, Either}, Iam, }; @@ -14,7 +15,8 @@ use crate::{ pub struct AppInner { secret: String, token: String, - _iam: Iam, + iam: Iam, + client: Client, } #[derive(Debug, Clone)] @@ -46,12 +48,18 @@ impl App { Ok(Self { inner: Arc::new(AppInner { secret: secret.to_owned(), + client: create_client(&res.token), token: res.token, - _iam: iam.clone(), + iam: iam.clone(), }), }) } + #[inline] + fn client(&self) -> &Client { + &self.inner.client + } + pub fn token(&self) -> &str { &self.inner.token } @@ -60,4 +68,18 @@ impl App { let (id, _) = iam_common::app::parse_token(&self.inner.secret).unwrap(); id } + + pub async fn get_user_info(&self, id: &str) -> Result { + let res = self + .client() + .get(self.inner.iam.get_url(&format!("/v1/users/{}/", id))) + .send() + .await? + .json::>() + .await?; + + let res = unwrap_res(res)?; + + Ok(res) + } } diff --git a/libiam/src/error.rs b/libiam/src/error.rs index 94a5c08..7bbe6f0 100644 --- a/libiam/src/error.rs +++ b/libiam/src/error.rs @@ -5,7 +5,7 @@ use crate::utils::Either; #[derive(Debug, Deserialize)] pub struct ErrorMessage { pub code: String, - pub message: String, + pub error: String, } #[derive(Debug, thiserror::Error)] @@ -22,6 +22,9 @@ pub type Result = std::result::Result; pub fn unwrap_res(either: Either) -> Result { match either { Either::Left(t) => Ok(t), - Either::Right(err) => Err(Error::Iam(err)), + Either::Right(err) => { + tracing::error!("iam returned error: {err:?}"); + Err(Error::Iam(err)) + } } } diff --git a/libiam/src/testing/mod.rs b/libiam/src/testing/mod.rs index 62cb9f4..348972d 100644 --- a/libiam/src/testing/mod.rs +++ b/libiam/src/testing/mod.rs @@ -1,5 +1,6 @@ pub mod actions; pub mod apps; mod db; +pub mod users; pub use db::Database; diff --git a/libiam/src/testing/users.rs b/libiam/src/testing/users.rs new file mode 100644 index 0000000..8954d8e --- /dev/null +++ b/libiam/src/testing/users.rs @@ -0,0 +1,11 @@ +pub use iam_common::user::UserInfo; +use sea_orm::ConnectionTrait; + +pub async fn get_user(db: &D, id: &str) -> UserInfo +where + D: ConnectionTrait, +{ + iam_common::user::get_user(db, id) + .await + .expect("failed to get user") +} diff --git a/libiam/src/utils.rs b/libiam/src/utils.rs index 9528564..574110b 100644 --- a/libiam/src/utils.rs +++ b/libiam/src/utils.rs @@ -1,3 +1,7 @@ +use reqwest::{ + header::{HeaderMap, HeaderValue, AUTHORIZATION}, + Client, +}; use serde::Deserialize; #[derive(Debug, Deserialize)] @@ -6,3 +10,16 @@ pub enum Either { Left(L), Right(R), } + +pub fn create_client(token: &str) -> Client { + let mut header_map = HeaderMap::with_capacity(1); + header_map.insert( + AUTHORIZATION, + HeaderValue::from_str(&format!("Bearer {}", token)).unwrap(), + ); + + Client::builder() + .default_headers(header_map) + .build() + .expect("failed to create reqwest client") +}