Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added missing workspace member check to oauth #321

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions kraken/src/api/handler/oauth/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use log::debug;
use rand::distributions::Alphanumeric;
use rand::distributions::DistString;
use rand::thread_rng;
use rorm::and;
use rorm::query;
use rorm::FieldAccess;
use rorm::Model;
Expand All @@ -36,6 +37,8 @@ use crate::chan::global::GLOBAL;
use crate::models::OAuthDecision;
use crate::models::OAuthDecisionAction;
use crate::models::OauthClient;
use crate::models::User;
use crate::models::UserPermission;
use crate::models::Workspace;
use crate::models::WorkspaceAccessToken;
use crate::modules::oauth::schemas::AuthError;
Expand Down Expand Up @@ -73,8 +76,9 @@ pub async fn auth(
SessionUser(user_uuid): SessionUser,
) -> Result<Redirect, ApiError> {
let request = request.into_inner();
let mut tx = GLOBAL.db.start_transaction().await?;

let Some(client) = query!(&GLOBAL.db, OauthClient)
let Some(client) = query!(&mut tx, OauthClient)
.condition(OauthClient::F.uuid.equals(request.client_id))
.optional()
.await?
Expand Down Expand Up @@ -138,6 +142,26 @@ pub async fn auth(
);
};

if !Workspace::is_user_member_or_owner(&mut tx, workspace, user_uuid).await?
&& !query!(&GLOBAL.db, (User::F.uuid,))
.condition(and![
User::F.uuid.equals(user_uuid),
User::F.permission.equals(UserPermission::Admin)
])
.optional()
.await?
.is_some()
{
return build_redirect(
&client.redirect_uri,
AuthError {
error: AuthErrorType::AccessDenied,
state: None,
error_description: Some("The user is not member of this workspace"),
},
);
}

let Some(state) = request.state else {
return build_redirect(
&client.redirect_uri,
Expand Down Expand Up @@ -178,8 +202,8 @@ pub async fn auth(
code_challenge,
};

if let Some(action) =
OAuthDecision::get(&GLOBAL.db, user_uuid, request.client_id, open_request.scope).await?
let redirect = if let Some(action) =
OAuthDecision::get(&mut tx, user_uuid, request.client_id, open_request.scope).await?
{
match action {
OAuthDecisionAction::Accept => {
Expand All @@ -204,7 +228,9 @@ pub async fn auth(
} else {
let request_uuid = manager.insert_open(open_request);
Ok(Redirect::to(format!("/#/oauth-request/{request_uuid}")))
}
}?;
tx.commit().await?;
Ok(redirect)
}

/// Queried by the frontend to display information about the oauth request to the user
Expand Down
Loading