浏览代码

feat(auth): refresh token route

Bryan Lee 1 年之前
父节点
当前提交
3067e52245

+ 1 - 0
authentication/src/auth/mod.rs

@@ -10,6 +10,7 @@ use serde::Serialize;
 
 pub fn config_service(cfg: &mut web::ServiceConfig) {
     cfg.service(sign_out)
+        .service(refresh::refresh)
         .service(web::scope("/oauth2").configure(oauth2::config_service));
 }
 

+ 1 - 1
authentication/src/auth/oauth2/sign_in.rs

@@ -1,7 +1,7 @@
 use crate::auth::identity::{Identity, IdentityConfig};
 use crate::auth::oauth2::google_provider::{GoogleUserInfo, GoogleUserInfoService};
 use crate::auth::provider::{AuthProvider, AuthProviderChangeset, AuthProviderType};
-use crate::auth::refresh::RefreshSession;
+use crate::auth::refresh::session::RefreshSession;
 use crate::auth::token::BearerToken;
 use crate::auth::SignInSuccess;
 use crate::db::{DbConnection, DbPool};

+ 30 - 0
authentication/src/auth/refresh/mod.rs

@@ -0,0 +1,30 @@
+use actix_web::{error, post, web, HttpResponse};
+use serde::Deserialize;
+
+use crate::auth::identity::IdentityConfig;
+use crate::auth::refresh::session::{RefreshResult, RefreshSession};
+use crate::db::DbPool;
+
+pub mod session;
+
+#[derive(Deserialize)]
+struct RefreshRequestBody {
+    refresh_token: String,
+}
+
+#[post("/refresh/")]
+async fn refresh(
+    pool: web::Data<DbPool>,
+    identity_config: web::Data<IdentityConfig>,
+    body: web::Json<RefreshRequestBody>,
+) -> actix_web::Result<HttpResponse> {
+    let mut conn = pool.get().await.map_err(error::ErrorInternalServerError)?;
+    let result = RefreshSession::refresh(&mut conn, &identity_config, &body.refresh_token)
+        .await
+        .map_err(error::ErrorInternalServerError)?;
+
+    match result {
+        RefreshResult::Success(success) => Ok(HttpResponse::Ok().json(success)),
+        _ => Ok(HttpResponse::BadRequest().finish()),
+    }
+}

+ 1 - 1
authentication/src/auth/refresh.rs → authentication/src/auth/refresh/session.rs

@@ -73,7 +73,7 @@ pub enum RefreshResult {
     SessionInvalidated,
 }
 
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Serialize)]
 #[cfg_attr(test, derive(PartialEq))]
 pub struct RefreshSuccess {
     pub access_token: String,