From 19db9ef4e85f6cab8b8a4d4e03fd1e1c23646da8 Mon Sep 17 00:00:00 2001 From: augustuswm Date: Thu, 12 Oct 2023 12:34:11 -0500 Subject: [PATCH] Fmt --- rfd-api/src/context.rs | 35 +++++++++------ rfd-api/src/endpoints/api_user.rs | 5 ++- rfd-api/src/endpoints/api_user_provider.rs | 14 +++--- rfd-api/src/endpoints/mod.rs | 2 +- rfd-api/src/endpoints/well_known/mod.rs | 50 ++++++++++++---------- rfd-api/src/initial_data.rs | 6 ++- rfd-api/src/permissions.rs | 4 +- rfd-api/src/server.rs | 3 +- rfd-cli/src/auth/link.rs | 27 ++++++------ rfd-cli/src/auth/login.rs | 2 +- rfd-cli/src/main.rs | 5 +-- rfd-cli/src/printer/tab.rs | 10 ++++- rfd-model/src/storage/mod.rs | 8 +++- rfd-model/src/storage/postgres.rs | 25 ++++++----- 14 files changed, 118 insertions(+), 78 deletions(-) diff --git a/rfd-api/src/context.rs b/rfd-api/src/context.rs index d169830f..2b5796b2 100644 --- a/rfd-api/src/context.rs +++ b/rfd-api/src/context.rs @@ -551,7 +551,10 @@ impl ApiContext { // Login Operations #[instrument(skip(self, info), fields(info.external_id))] - pub async fn register_api_user(&self, info: UserInfo) -> Result<(User, ApiUserProvider), ApiError> { + pub async fn register_api_user( + &self, + info: UserInfo, + ) -> Result<(User, ApiUserProvider), ApiError> { // Check if we have seen this identity before let mut filter = ApiUserProviderFilter::default(); filter.provider = Some(vec![info.external_id.provider().to_string()]); @@ -576,14 +579,15 @@ impl ApiContext { let user = self .ensure_api_user(Uuid::new_v4(), mapped_permissions, mapped_groups) .await?; - let user_provider = self.update_api_user_provider(NewApiUserProvider { - id: Uuid::new_v4(), - api_user_id: user.id, - emails: info.verified_emails, - provider: info.external_id.provider().to_string(), - provider_id: info.external_id.id().to_string(), - }) - .await?; + let user_provider = self + .update_api_user_provider(NewApiUserProvider { + id: Uuid::new_v4(), + api_user_id: user.id, + emails: info.verified_emails, + provider: info.external_id.provider().to_string(), + provider_id: info.external_id.id().to_string(), + }) + .await?; Ok((user, user_provider)) } @@ -592,9 +596,11 @@ impl ApiContext { // This branch ensures that there is a 0th indexed item let provider = api_user_providers.into_iter().nth(0).unwrap(); - Ok((self - .ensure_api_user(provider.api_user_id, mapped_permissions, mapped_groups) - .await?, provider)) + Ok(( + self.ensure_api_user(provider.api_user_id, mapped_permissions, mapped_groups) + .await?, + provider, + )) } _ => { // If we found more than one provider, then we have encountered an inconsistency in @@ -1166,7 +1172,10 @@ impl ApiContext { update_request.completed_at = Some(Utc::now()); LinkRequestStore::upsert(&*self.storage, &update_request).await?; - Ok(Some(ApiUserProviderStore::transfer(&*self.storage, provider.into(), source_api_user_id).await?)) + Ok(Some( + ApiUserProviderStore::transfer(&*self.storage, provider.into(), source_api_user_id) + .await?, + )) } else { tracing::warn!(?link_request, "Expected to find a provider that was assigned to a link request, but it looks to have gone missing"); Ok(None) diff --git a/rfd-api/src/endpoints/api_user.rs b/rfd-api/src/endpoints/api_user.rs index d84f2890..bdb51de4 100644 --- a/rfd-api/src/endpoints/api_user.rs +++ b/rfd-api/src/endpoints/api_user.rs @@ -6,7 +6,10 @@ use dropshot::{ RequestContext, TypedBody, }; use partial_struct::partial; -use rfd_model::{storage::{ListPagination, ApiUserProviderFilter}, ApiUser, NewApiKey, NewApiUser, ApiUserProvider}; +use rfd_model::{ + storage::{ApiUserProviderFilter, ListPagination}, + ApiUser, ApiUserProvider, NewApiKey, NewApiUser, +}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use tap::TapFallible; diff --git a/rfd-api/src/endpoints/api_user_provider.rs b/rfd-api/src/endpoints/api_user_provider.rs index 506ef444..9c6b55da 100644 --- a/rfd-api/src/endpoints/api_user_provider.rs +++ b/rfd-api/src/endpoints/api_user_provider.rs @@ -42,15 +42,17 @@ pub async fn create_link_token( let path = path.into_inner(); let body = body.into_inner(); - let provider = ctx.get_api_user_provider(&path.identifier).await.map_err(ApiError::Storage)?; + let provider = ctx + .get_api_user_provider(&path.identifier) + .await + .map_err(ApiError::Storage)?; if let Some(provider) = provider { - if provider.api_user_id == caller.id && caller.can(&ApiPermission::CreateUserApiProviderLinkToken) { + if provider.api_user_id == caller.id + && caller.can(&ApiPermission::CreateUserApiProviderLinkToken) + { let token = ctx - .create_link_request_token( - &path.identifier, - &caller.id, - &body.user_identifier) + .create_link_request_token(&path.identifier, &caller.id, &body.user_identifier) .await .map_err(ApiError::Storage)?; diff --git a/rfd-api/src/endpoints/mod.rs b/rfd-api/src/endpoints/mod.rs index 83d93bee..80051d03 100644 --- a/rfd-api/src/endpoints/mod.rs +++ b/rfd-api/src/endpoints/mod.rs @@ -1,7 +1,7 @@ -pub mod well_known; pub mod api_user; pub mod api_user_provider; pub mod group; pub mod login; pub mod rfd; pub mod webhook; +pub mod well_known; diff --git a/rfd-api/src/endpoints/well_known/mod.rs b/rfd-api/src/endpoints/well_known/mod.rs index b4c867f5..de734b22 100644 --- a/rfd-api/src/endpoints/well_known/mod.rs +++ b/rfd-api/src/endpoints/well_known/mod.rs @@ -1,5 +1,5 @@ -use dropshot::{RequestContext, HttpResponseOk, HttpError, endpoint}; -use jsonwebtoken::jwk::{JwkSet, AlgorithmParameters, PublicKeyUse}; +use dropshot::{endpoint, HttpError, HttpResponseOk, RequestContext}; +use jsonwebtoken::jwk::{AlgorithmParameters, JwkSet, PublicKeyUse}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use trace_request::trace_request; @@ -19,10 +19,10 @@ pub struct OpenIdConfiguration { }] #[instrument(skip(rqctx), err(Debug))] pub async fn openid_configuration( - rqctx: RequestContext + rqctx: RequestContext, ) -> Result, HttpError> { Ok(HttpResponseOk(OpenIdConfiguration { - jwks_uri: format!("{}/.well-known/jwks.json", &rqctx.context().public_url) + jwks_uri: format!("{}/.well-known/jwks.json", &rqctx.context().public_url), })) } @@ -57,25 +57,29 @@ pub async fn jwks_json( impl From<&JwkSet> for Jwks { fn from(value: &JwkSet) -> Self { Self { - keys: value.keys.iter().map(|jwk| { - let (algo, n, e) = match &jwk.algorithm { - AlgorithmParameters::RSA(params) => { - ("RSA".to_string(), params.n.clone(), params.e.clone()) - } - _ => panic!("Unexpected key type"), - }; + keys: value + .keys + .iter() + .map(|jwk| { + let (algo, n, e) = match &jwk.algorithm { + AlgorithmParameters::RSA(params) => { + ("RSA".to_string(), params.n.clone(), params.e.clone()) + } + _ => panic!("Unexpected key type"), + }; - Jwk { - kty: algo, - kid: jwk.common.key_id.as_ref().unwrap().clone(), - use_: match jwk.common.public_key_use { - Some(PublicKeyUse::Signature) => "sig".to_string(), - _ => panic!("Unexpected key use"), - }, - n, - e, - } - }).collect() + Jwk { + kty: algo, + kid: jwk.common.key_id.as_ref().unwrap().clone(), + use_: match jwk.common.public_key_use { + Some(PublicKeyUse::Signature) => "sig".to_string(), + _ => panic!("Unexpected key use"), + }, + n, + e, + } + }) + .collect(), } } -} \ No newline at end of file +} diff --git a/rfd-api/src/initial_data.rs b/rfd-api/src/initial_data.rs index 24b84a50..e06eed2d 100644 --- a/rfd-api/src/initial_data.rs +++ b/rfd-api/src/initial_data.rs @@ -59,7 +59,11 @@ impl InitialData { let span = tracing::info_span!("Initializing group", group = ?group); async { - let id = existing_groups.iter().find(|g| g.name == group.name).map(|g| g.id).unwrap_or(Uuid::new_v4()); + let id = existing_groups + .iter() + .find(|g| g.name == group.name) + .map(|g| g.id) + .unwrap_or(Uuid::new_v4()); ctx.create_group(NewAccessGroup { id, diff --git a/rfd-api/src/permissions.rs b/rfd-api/src/permissions.rs index d1317c75..00dce7b9 100644 --- a/rfd-api/src/permissions.rs +++ b/rfd-api/src/permissions.rs @@ -307,7 +307,9 @@ impl PermissionStorage for Permissions { } } - contracted.push(ApiPermission::ManageGroupMemberships(manage_group_memberships)); + contracted.push(ApiPermission::ManageGroupMemberships( + manage_group_memberships, + )); contracted.push(ApiPermission::ManageGroups(manage_groups)); contracted.push(ApiPermission::GetRfds(rfds)); contracted.push(ApiPermission::GetDiscussions(discussions)); diff --git a/rfd-api/src/server.rs b/rfd-api/src/server.rs index 839b7126..a30e79a0 100644 --- a/rfd-api/src/server.rs +++ b/rfd-api/src/server.rs @@ -26,7 +26,8 @@ use crate::{ device_token::{exchange_device_token, get_device_provider}, }, rfd::{get_rfd, get_rfds, search_rfds}, - webhook::github_webhook, well_known::{openid_configuration, jwks_json}, + webhook::github_webhook, + well_known::{jwks_json, openid_configuration}, }, }; diff --git a/rfd-cli/src/auth/link.rs b/rfd-cli/src/auth/link.rs index bcc99beb..f20d374a 100644 --- a/rfd-cli/src/auth/link.rs +++ b/rfd-cli/src/auth/link.rs @@ -2,13 +2,13 @@ use core::panic; use anyhow::Result; use clap::{Parser, Subcommand}; -use jsonwebtoken::{DecodingKey, Validation, Algorithm}; +use jsonwebtoken::{Algorithm, DecodingKey, Validation}; use oauth2::TokenResponse; use rfd_sdk::types::OAuthProviderName; use serde::Deserialize; use uuid::Uuid; -use crate::{Context, auth::oauth}; +use crate::{auth::oauth, Context}; use super::login::LoginProvider; @@ -37,27 +37,30 @@ impl Link { let jwk = &jwks.keys[0]; // Decode the access token to extract the provider token - let jwt = jsonwebtoken::decode::(&access_token, &DecodingKey::from_rsa_components(&jwk.n, &jwk.e)?, &Validation::new(Algorithm::RS256))?; + let jwt = jsonwebtoken::decode::( + &access_token, + &DecodingKey::from_rsa_components(&jwk.n, &jwk.e)?, + &Validation::new(Algorithm::RS256), + )?; // An account linking request can only be generated by the owning account. Therefore we // need to use the sdk to generate a new client let client = Context::new_client(Ok(&access_token), ctx.config.host()?)?; - + // This needs to be the id of the provider the client just logged in with - let link_token = client.create_link_token().identifier(jwt.claims.prv) - .body_map(|body| { - body.user_identifier(self_id) - }) + let link_token = client + .create_link_token() + .identifier(jwt.claims.prv) + .body_map(|body| body.user_identifier(self_id)) .send() .await? .into_inner() .token; - ctx.client()?.link_provider() + ctx.client()? + .link_provider() .identifier(self_id) - .body_map(|body| { - body.token(link_token) - }) + .body_map(|body| body.token(link_token)) .send() .await?; diff --git a/rfd-cli/src/auth/login.rs b/rfd-cli/src/auth/login.rs index fcdd1fb6..5321d96d 100644 --- a/rfd-cli/src/auth/login.rs +++ b/rfd-cli/src/auth/login.rs @@ -3,7 +3,7 @@ use clap::{Parser, Subcommand}; use oauth2::TokenResponse; use rfd_sdk::types::OAuthProviderName; -use crate::{Context, auth::oauth}; +use crate::{auth::oauth, Context}; // Authenticates and generates an access token for interacting with the api #[derive(Parser, Debug)] diff --git a/rfd-cli/src/main.rs b/rfd-cli/src/main.rs index 4a76f6af..0bc1dd12 100644 --- a/rfd-cli/src/main.rs +++ b/rfd-cli/src/main.rs @@ -45,8 +45,7 @@ impl Context { let mut default_headers = HeaderMap::new(); if let Ok(token) = token { - let mut auth_header = - HeaderValue::from_str(&format!("Bearer {}", token))?; + let mut auth_header = HeaderValue::from_str(&format!("Bearer {}", token))?; auth_header.set_sensitive(true); default_headers.insert(AUTHORIZATION, auth_header); } @@ -67,7 +66,7 @@ impl Context { self.client .as_ref() - .ok_or_else(|| anyhow!("Failed to construct client")) + .ok_or_else(|| anyhow!("Failed to construct client")) } } diff --git a/rfd-cli/src/printer/tab.rs b/rfd-cli/src/printer/tab.rs index 88de5635..8e89ac95 100644 --- a/rfd-cli/src/printer/tab.rs +++ b/rfd-cli/src/printer/tab.rs @@ -1,5 +1,7 @@ use itertools::{EitherOrBoth, Itertools}; -use rfd_sdk::types::{AccessGroupForApiPermission, ApiUserForApiPermission, Error, ListRfd, GetApiUserResponse}; +use rfd_sdk::types::{ + AccessGroupForApiPermission, ApiUserForApiPermission, Error, GetApiUserResponse, ListRfd, +}; use std::{fs::File, io::Write, process::Command}; use tabwriter::TabWriter; @@ -233,7 +235,11 @@ fn print_user(user: &GetApiUserResponse) { HEADER_COLOR ); - let lines = user.info.permissions.iter().zip_longest(user.info.groups.iter()); + let lines = user + .info + .permissions + .iter() + .zip_longest(user.info.groups.iter()); for (i, line) in lines.enumerate() { let inner = match line { diff --git a/rfd-model/src/storage/mod.rs b/rfd-model/src/storage/mod.rs index 69d0d553..330793ba 100644 --- a/rfd-model/src/storage/mod.rs +++ b/rfd-model/src/storage/mod.rs @@ -303,8 +303,12 @@ pub trait ApiUserProviderStore { filter: ApiUserProviderFilter, pagination: &ListPagination, ) -> Result, StoreError>; - async fn upsert(&self, api_user: NewApiUserProvider,) -> Result; - async fn transfer(&self, api_user: NewApiUserProvider, current_api_user_id: Uuid) -> Result; + async fn upsert(&self, api_user: NewApiUserProvider) -> Result; + async fn transfer( + &self, + api_user: NewApiUserProvider, + current_api_user_id: Uuid, + ) -> Result; async fn delete(&self, id: &Uuid) -> Result, StoreError>; } diff --git a/rfd-model/src/storage/postgres.rs b/rfd-model/src/storage/postgres.rs index 96b1dcf6..22e784e3 100644 --- a/rfd-model/src/storage/postgres.rs +++ b/rfd-model/src/storage/postgres.rs @@ -846,19 +846,22 @@ impl ApiUserProviderStore for PostgresStore { }) } - async fn transfer(&self, provider: NewApiUserProvider, current_api_user_id: Uuid) -> Result { + async fn transfer( + &self, + provider: NewApiUserProvider, + current_api_user_id: Uuid, + ) -> Result { tracing::trace!(id = ?provider.id, api_user_id = ?provider.api_user_id, provider = ?provider, "Updating user provider"); - let provider_m: ApiUserProviderModel = - update(api_user_provider::dsl::api_user_provider) - .set(( - api_user_provider::api_user_id.eq(provider.api_user_id), - api_user_provider::updated_at.eq(Utc::now()), - )) - .filter(api_user_provider::id.eq(provider.id)) - .filter(api_user_provider::api_user_id.eq(current_api_user_id)) - .get_result_async(&self.conn) - .await?; + let provider_m: ApiUserProviderModel = update(api_user_provider::dsl::api_user_provider) + .set(( + api_user_provider::api_user_id.eq(provider.api_user_id), + api_user_provider::updated_at.eq(Utc::now()), + )) + .filter(api_user_provider::id.eq(provider.id)) + .filter(api_user_provider::api_user_id.eq(current_api_user_id)) + .get_result_async(&self.conn) + .await?; Ok(ApiUserProvider { id: provider_m.id,