-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2920: feat(sdf-server, auth-portal, web): adding a page that we can send a post request to that forces auth cleanup r=stack72 a=stack72 This page is basic for now and we redirect both ways but we can make this better. It will compare the users from the auth-api against what’s in our DB - the auth-api is the source of truth!! There are a number of improvements needed for this but we can start to use it immediately for internal use In the future, this will be replaced by the use of websockets to do the same job Co-authored-by: stack72 <[email protected]>
- Loading branch information
Showing
9 changed files
with
175 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<template><div>Refresh auth</div></template> | ||
|
||
<script setup lang="ts"> | ||
import { useRoute } from "vue-router"; | ||
import { onMounted } from "vue"; | ||
import { useAuthStore } from "@/store/auth.store"; | ||
const route = useRoute(); | ||
const authStore = useAuthStore(); | ||
const AUTH_PORTAL_URL = import.meta.env.VITE_AUTH_PORTAL_URL; | ||
const workspaceId = route.query.workspaceId as string; | ||
onMounted(async () => { | ||
if (workspaceId) { | ||
await authStore.FORCE_REFRESH_MEMBERS(workspaceId); | ||
} | ||
window.location.href = `${AUTH_PORTAL_URL}/workspace/${workspaceId}`; | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
SELECT row_to_json(u.*) AS object | ||
FROM users AS u | ||
INNER JOIN user_belongs_to_workspaces bt ON bt.user_pk = u.pk | ||
WHERE bt.workspace_pk = $1 | ||
ORDER BY u.created_at ASC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
lib/sdf-server/src/server/service/session/refresh_workspace_members.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use super::{SessionError, SessionResult}; | ||
use crate::server::extract::{AccessBuilder, HandlerContext, RawAccessToken}; | ||
use crate::service::session::AuthApiErrBody; | ||
use axum::Json; | ||
use dal::User; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct RefreshWorkspaceMembersRequest { | ||
pub workspace_id: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct WorkspaceMember { | ||
pub user_id: String, | ||
pub email: String, | ||
pub nickname: String, | ||
pub role: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct RefreshWorkspaceMembersResponse { | ||
pub success: bool, | ||
} | ||
|
||
pub async fn refresh_workspace_members( | ||
HandlerContext(builder): HandlerContext, | ||
AccessBuilder(access_builder): AccessBuilder, | ||
RawAccessToken(raw_access_token): RawAccessToken, | ||
Json(request): Json<RefreshWorkspaceMembersRequest>, | ||
) -> SessionResult<Json<RefreshWorkspaceMembersResponse>> { | ||
let client = reqwest::Client::new(); | ||
let auth_api_url = match option_env!("LOCAL_AUTH_STACK") { | ||
Some(_) => "http://localhost:9001", | ||
None => "https://auth-api.systeminit.com", | ||
}; | ||
|
||
let res = client | ||
.get(format!( | ||
"{}/workspace/{}/members", | ||
auth_api_url, | ||
request.workspace_id.clone() | ||
)) | ||
.bearer_auth(&raw_access_token) | ||
.send() | ||
.await?; | ||
|
||
if res.status() != reqwest::StatusCode::OK { | ||
let res_err_body = res | ||
.json::<AuthApiErrBody>() | ||
.await | ||
.map_err(|err| SessionError::AuthApiError(err.to_string()))?; | ||
println!("code exchange failed = {:?}", res_err_body.message); | ||
return Err(SessionError::AuthApiError(res_err_body.message)); | ||
} | ||
|
||
let workspace_members = res.json::<Vec<WorkspaceMember>>().await?; | ||
|
||
let ctx = builder.build_head(access_builder).await?; | ||
let members = User::list_members_for_workspace(&ctx, request.workspace_id.clone()).await?; | ||
let member_ids: Vec<_> = workspace_members.into_iter().map(|w| w.user_id).collect(); | ||
let users_to_remove: Vec<_> = members | ||
.into_iter() | ||
.filter(|u| !member_ids.contains(&u.pk().to_string())) | ||
.collect(); | ||
|
||
for remove in users_to_remove { | ||
println!("Removing User: {}", remove.pk().clone()); | ||
User::delete_user_from_workspace(&ctx, remove.pk(), request.workspace_id.clone()).await?; | ||
} | ||
|
||
Ok(Json(RefreshWorkspaceMembersResponse { success: true })) | ||
} |