fix: remove duplicated magic strings across multiple files #82

Closed
BobdaFett wants to merge 1 commits from fix/centralize-cognito-pool-ids into main
pull from: fix/centralize-cognito-pool-ids
13 changed files with 46 additions and 30 deletions

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
pub const STUDENT_PROVIDER_POOL_ID: &str = "us-east-1_Lfjuy5zaM";
pub const PROVIDER_GROUP: &str = "ScholarshipProviders";
pub const STUDENT_GROUP: &str = "ScholarshipStudents";
+4 -3
View File
@@ -4,6 +4,7 @@ use aws_config::BehaviorVersion;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use std::collections::HashMap; use std::collections::HashMap;
use aws_sdk_cognitoidentityprovider::error::ProvideErrorMetadata; use aws_sdk_cognitoidentityprovider::error::ProvideErrorMetadata;
use crate::constants::{PROVIDER_GROUP, STUDENT_GROUP, STUDENT_PROVIDER_POOL_ID};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@@ -43,7 +44,7 @@ async fn add_user_to_group(provider: Client, user_id: String, group_name: impl I
println!("Adding user {} to group {}", user_id, group_name); println!("Adding user {} to group {}", user_id, group_name);
match provider match provider
.admin_add_user_to_group() .admin_add_user_to_group()
.user_pool_id("us-east-1_Lfjuy5zaM") .user_pool_id(STUDENT_PROVIDER_POOL_ID)
.username(user_id) .username(user_id)
.group_name(group_name) .group_name(group_name)
.send() .send()
@@ -63,8 +64,8 @@ pub(crate) async fn handler(event: LambdaEvent<PostConfirmationEvent>) -> Result
// Check the client ID of the request to assign the user to the correct location. // Check the client ID of the request to assign the user to the correct location.
let res = match event.payload.caller_context.client_id.as_str() { let res = match event.payload.caller_context.client_id.as_str() {
"10jr2h3vtpu9n7gj46pvg5qo2q" => add_user_to_group(client, user_id, "ScholarshipStudents").await, "10jr2h3vtpu9n7gj46pvg5qo2q" => add_user_to_group(client, user_id, STUDENT_GROUP).await,
"56c2bqvl021rv8d5mq36blt7jv" => add_user_to_group(client, user_id, "ScholarshipProviders").await, "56c2bqvl021rv8d5mq36blt7jv" => add_user_to_group(client, user_id, PROVIDER_GROUP).await,
_ => Err("Invalid client ID, denying all user access.".to_string()), _ => Err("Invalid client ID, denying all user access.".to_string()),
}; };
+1
View File
@@ -1,4 +1,5 @@
use lambda_runtime::{run, service_fn, tracing, Error}; use lambda_runtime::{run, service_fn, tracing, Error};
mod constants;
mod handler; mod handler;
use handler::handler as function_handler; use handler::handler as function_handler;
+5
View File
@@ -0,0 +1,5 @@
pub const STUDENT_PROVIDER_POOL_ID: &str = "us-east-1_Lfjuy5zaM";
pub const ADMIN_POOL_ID: &str = "us-east-1_rvCU4Xy4j";
pub const COGNITO_REGION: &str = "us-east-1";
pub const PROVIDER_GROUP: &str = "ScholarshipProviders";
pub const STUDENT_GROUP: &str = "ScholarshipStudents";
+2 -1
View File
@@ -9,8 +9,9 @@ mod value_type;
pub mod animations; pub mod animations;
mod tab_info; mod tab_info;
mod schema_node; mod schema_node;
mod constants;
pub use self::{ pub use self::{
comparison::*, date_info::*, expandable_info::*, form_params::*, form_schema::*, comparison::*, date_info::*, expandable_info::*, form_params::*, form_schema::*,
submit_status::*, user::*, value_type::*, tab_info::*, schema_node::*, submit_status::*, user::*, value_type::*, tab_info::*, schema_node::*, constants::*,
}; };
+3 -2
View File
@@ -1,11 +1,12 @@
use crate::components::login::setup::provide_auth_context; use crate::common::{ADMIN_POOL_ID, COGNITO_REGION};
use crate::components::login::setup::provide_auth_context;
use leptos::prelude::*; use leptos::prelude::*;
#[component] #[component]
pub fn AdminLoginContext(children: Children) -> impl IntoView { pub fn AdminLoginContext(children: Children) -> impl IntoView {
provide_auth_context( provide_auth_context(
crate::utils::use_origin(), crate::utils::use_origin(),
"https://cognito-idp.us-east-1.amazonaws.com/us-east-1_rvCU4Xy4j", format!("https://cognito-idp.{COGNITO_REGION}.amazonaws.com/{ADMIN_POOL_ID}"),
"1vh1q994rid6cgi6tjf58r9jp4", "1vh1q994rid6cgi6tjf58r9jp4",
"/admin/callback", "/admin/callback",
); );
+2 -1
View File
@@ -1,6 +1,7 @@
use leptos::prelude::*; use leptos::prelude::*;
use leptos_router::components::Outlet; use leptos_router::components::Outlet;
use super::setup::provide_auth_context; use super::setup::provide_auth_context;
use crate::common::{COGNITO_REGION, STUDENT_PROVIDER_POOL_ID};
///# Provider Login Context Component ///# Provider Login Context Component
/// ///
@@ -19,7 +20,7 @@ use super::setup::provide_auth_context;
pub fn ProviderLoginContext() -> impl IntoView { pub fn ProviderLoginContext() -> impl IntoView {
provide_auth_context( provide_auth_context(
crate::utils::use_origin(), crate::utils::use_origin(),
"https://cognito-idp.us-east-1.amazonaws.com/us-east-1_Lfjuy5zaM", format!("https://cognito-idp.{COGNITO_REGION}.amazonaws.com/{STUDENT_PROVIDER_POOL_ID}"),
"56c2bqvl021rv8d5mq36blt7jv", "56c2bqvl021rv8d5mq36blt7jv",
"/providers/callback" "/providers/callback"
); );
+2 -1
View File
@@ -1,5 +1,6 @@
use leptos::prelude::*; use leptos::prelude::*;
use super::setup::provide_auth_context; use super::setup::provide_auth_context;
use crate::common::{COGNITO_REGION, STUDENT_PROVIDER_POOL_ID};
///# Student Login Context Component ///# Student Login Context Component
/// ///
@@ -20,7 +21,7 @@ pub fn StudentLoginContext(
) -> impl IntoView { ) -> impl IntoView {
provide_auth_context( provide_auth_context(
crate::utils::use_origin(), crate::utils::use_origin(),
"https://cognito-idp.us-east-1.amazonaws.com/us-east-1_Lfjuy5zaM", format!("https://cognito-idp.{COGNITO_REGION}.amazonaws.com/{STUDENT_PROVIDER_POOL_ID}"),
"10jr2h3vtpu9n7gj46pvg5qo2q", "10jr2h3vtpu9n7gj46pvg5qo2q",
"/students/callback" "/students/callback"
); );
+2 -1
View File
@@ -63,10 +63,11 @@ fn AdminApplicantsScholarshipList() -> impl IntoView {
async fn admin_get_provider_scholarships( async fn admin_get_provider_scholarships(
access_token: String, access_token: String,
) -> Result<Vec<ExpandableInfo>, ServerFnError> { ) -> Result<Vec<ExpandableInfo>, ServerFnError> {
use crate::common::{ADMIN_POOL_ID, COGNITO_REGION};
use crate::pages::api::SCHOLARSHIPS_TABLE; use crate::pages::api::SCHOLARSHIPS_TABLE;
use crate::pages::api::tokens::validate_and_get_token_info; use crate::pages::api::tokens::validate_and_get_token_info;
let claims = validate_and_get_token_info(access_token, "us-east-1_rvCU4Xy4j", "us-east-1").await?; let claims = validate_and_get_token_info(access_token, ADMIN_POOL_ID, COGNITO_REGION).await?;
// Get the information from the database. // Get the information from the database.
let client = crate::utils::server::create_dynamo_client().await; let client = crate::utils::server::create_dynamo_client().await;
+4 -3
View File
@@ -6,6 +6,7 @@ use server_fn::codec::{MultipartData, MultipartFormData};
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
mod imports { mod imports {
pub use super::super::{MAIN_TABLE_NAME, S3_BUCKET_NAME}; pub use super::super::{MAIN_TABLE_NAME, S3_BUCKET_NAME};
pub use crate::common::{COGNITO_REGION, STUDENT_PROVIDER_POOL_ID};
pub use crate::pages::api::tokens::validate_and_get_token_info; pub use crate::pages::api::tokens::validate_and_get_token_info;
pub use crate::utils::server::create_aws_config; pub use crate::utils::server::create_aws_config;
pub use aws_sdk_dynamodb::{ pub use aws_sdk_dynamodb::{
@@ -66,7 +67,7 @@ pub async fn upload_file(data: MultipartData) -> Result<String, ServerFnError> {
return Err(ServerFnError::new("Missing file contents")); return Err(ServerFnError::new("Missing file contents"));
} }
let user_claims = validate_and_get_token_info(access_token, "us-east-1_Lfjuy5zaM", "us-east-1").await?; let user_claims = validate_and_get_token_info(access_token, STUDENT_PROVIDER_POOL_ID, COGNITO_REGION).await?;
let subject = user_claims.subject; let subject = user_claims.subject;
let key = format!("{form_id}/{subject}/{input_name}/{file_name}"); let key = format!("{form_id}/{subject}/{input_name}/{file_name}");
@@ -157,7 +158,7 @@ pub async fn delete_file(
) -> Result<String, ServerFnError> { ) -> Result<String, ServerFnError> {
use imports::*; use imports::*;
let user_claims = validate_and_get_token_info(access_token, "us-east-1_Lfjuy5zaM", "us-east-1").await?; let user_claims = validate_and_get_token_info(access_token, STUDENT_PROVIDER_POOL_ID, COGNITO_REGION).await?;
let subject = user_claims.subject; let subject = user_claims.subject;
let entry_hk = format!("STUDENT#{subject}"); let entry_hk = format!("STUDENT#{subject}");
@@ -226,7 +227,7 @@ pub async fn list_files(
) -> Result<Vec<String>, ServerFnError> { ) -> Result<Vec<String>, ServerFnError> {
use imports::*; use imports::*;
let user_claims = validate_and_get_token_info(access_token, "us-east-1_Lfjuy5zaM", "us-east-1").await?; let user_claims = validate_and_get_token_info(access_token, STUDENT_PROVIDER_POOL_ID, COGNITO_REGION).await?;
let subject = user_claims.subject; let subject = user_claims.subject;
let entry_hk = format!("STUDENT#{subject}"); let entry_hk = format!("STUDENT#{subject}");
+15 -15
View File
@@ -7,7 +7,7 @@ use std::process::Stdio;
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
mod imports { mod imports {
pub use super::super::MAIN_TABLE_NAME; pub use super::super::MAIN_TABLE_NAME;
pub use crate::common::ValueType; pub use crate::common::{ADMIN_POOL_ID, COGNITO_REGION, PROVIDER_GROUP, STUDENT_PROVIDER_POOL_ID, ValueType};
pub use crate::pages::api::tokens::validate_and_get_token_info; pub use crate::pages::api::tokens::validate_and_get_token_info;
pub use crate::utils::server::*; pub use crate::utils::server::*;
pub use aws_sdk_dynamodb::error::ProvideErrorMetadata; pub use aws_sdk_dynamodb::error::ProvideErrorMetadata;
@@ -148,11 +148,11 @@ pub async fn provider_get_completed_students(
use imports::*; use imports::*;
let claims = let claims =
validate_and_get_token_info(access_token, "us-east-1_Lfjuy5zaM", "us-east-1").await?; validate_and_get_token_info(access_token, STUDENT_PROVIDER_POOL_ID, COGNITO_REGION).await?;
if !claims.groups.contains(&"ScholarshipProviders".to_string()) { if !claims.groups.contains(&PROVIDER_GROUP.to_string()) {
return Err(ServerFnError::new( return Err(ServerFnError::new(format!(
"User is not in the ScholarshipProviders group", "User is not in the {PROVIDER_GROUP} group",
)); )));
} }
get_completed_students().await get_completed_students().await
@@ -164,7 +164,7 @@ pub async fn admin_get_completed_students(
) -> Result<HashMap<String, HashMap<String, crate::common::ValueType>>, ServerFnError> { ) -> Result<HashMap<String, HashMap<String, crate::common::ValueType>>, ServerFnError> {
use imports::*; use imports::*;
let _ = validate_and_get_token_info(access_token, "us-east-1_rvCU4Xy4j", "us-east-1").await?; let _ = validate_and_get_token_info(access_token, ADMIN_POOL_ID, COGNITO_REGION).await?;
get_completed_students().await get_completed_students().await
} }
@@ -254,7 +254,7 @@ pub async fn get_file_by_key(
use imports::*; use imports::*;
let user_claims = let user_claims =
validate_and_get_token_info(access_token, "us-east-1_Lfjuy5zaM", "us-east-1").await?; validate_and_get_token_info(access_token, STUDENT_PROVIDER_POOL_ID, COGNITO_REGION).await?;
// Check if the user's subject is contained in the file_key (since all files are keyed by the // Check if the user's subject is contained in the file_key (since all files are keyed by the
// user's subject) // user's subject)
@@ -262,7 +262,7 @@ pub async fn get_file_by_key(
// Check that the user is a provider. // Check that the user is a provider.
if !user_claims if !user_claims
.groups .groups
.contains(&"ScholarshipProviders".to_string()) .contains(&PROVIDER_GROUP.to_string())
{ {
return Err(ServerFnError::new( return Err(ServerFnError::new(
"Access denied: user is not the student or a provider", "Access denied: user is not the student or a provider",
@@ -304,16 +304,16 @@ pub async fn get_student_files(
) -> Result<(String, Vec<u8>), ServerFnError> { ) -> Result<(String, Vec<u8>), ServerFnError> {
use imports::*; use imports::*;
match validate_and_get_token_info(access_token.clone(), "us-east-1_Lfjuy5zaM", "us-east-1") match validate_and_get_token_info(access_token.clone(), STUDENT_PROVIDER_POOL_ID, COGNITO_REGION)
.await .await
{ {
Ok(claims) => { Ok(claims) => {
if !claims.groups.contains(&"ScholarshipProviders".to_string()) { if !claims.groups.contains(&PROVIDER_GROUP.to_string()) {
return Err(ServerFnError::new("Access denied: user is not a provider")); return Err(ServerFnError::new("Access denied: user is not a provider"));
} }
} }
Err(_) => { Err(_) => {
let _ = validate_and_get_token_info(access_token, "us-east-1_rvCU4Xy4j", "us-east-1") let _ = validate_and_get_token_info(access_token, ADMIN_POOL_ID, COGNITO_REGION)
.await?; .await?;
} }
}; };
@@ -507,7 +507,7 @@ pub async fn admin_get_all_input_files(
) -> Result<HashMap<String, Vec<String>>, ServerFnError> { ) -> Result<HashMap<String, Vec<String>>, ServerFnError> {
use imports::*; use imports::*;
let _ = validate_and_get_token_info(access_token, "us-east-1_rvCU4Xy4j", "us-east-1").await?; let _ = validate_and_get_token_info(access_token, ADMIN_POOL_ID, COGNITO_REGION).await?;
get_all_input_files(form_name, input_name).await get_all_input_files(form_name, input_name).await
} }
@@ -521,8 +521,8 @@ pub async fn provider_get_all_input_files(
use imports::*; use imports::*;
let claims = let claims =
validate_and_get_token_info(access_token, "us-east-1_Lfjuy5zaM", "us-east-1").await?; validate_and_get_token_info(access_token, STUDENT_PROVIDER_POOL_ID, COGNITO_REGION).await?;
if !claims.groups.contains(&"ScholarshipProviders".to_string()) { if !claims.groups.contains(&PROVIDER_GROUP.to_string()) {
return Err(ServerFnError::new("Access denied: user is not a provider")); return Err(ServerFnError::new("Access denied: user is not a provider"));
} }
-1
View File
@@ -28,7 +28,6 @@ pub async fn validate_and_get_token_info(
leptos::logging::debug_log!("Validating token using group {}", pool_id); leptos::logging::debug_log!("Validating token using group {}", pool_id);
// let jwks = fetch_jwks("us-east-1_Lfjuy5zaM".into(), "us-east-1".into()).await?;
let jwks = fetch_jwks(pool_id, pool_region.into()).await?; let jwks = fetch_jwks(pool_id, pool_region.into()).await?;
let validation = Validation::new(Algorithm::RS256); let validation = Validation::new(Algorithm::RS256);
+3 -2
View File
@@ -75,13 +75,14 @@ fn ApplicantsScholarshipList() -> impl IntoView {
async fn get_provider_scholarships( async fn get_provider_scholarships(
access_token: String, access_token: String,
) -> Result<Vec<ExpandableInfo>, ServerFnError> { ) -> Result<Vec<ExpandableInfo>, ServerFnError> {
use crate::common::{COGNITO_REGION, STUDENT_PROVIDER_POOL_ID};
use crate::pages::api::SCHOLARSHIPS_TABLE; use crate::pages::api::SCHOLARSHIPS_TABLE;
use crate::pages::api::tokens::validate_and_get_token_info; use crate::pages::api::tokens::validate_and_get_token_info;
let claims = validate_and_get_token_info( let claims = validate_and_get_token_info(
access_token, access_token,
"us-east-1_Lfjuy5zaM".to_string(), STUDENT_PROVIDER_POOL_ID.to_string(),
"us-east-1".to_string(), COGNITO_REGION.to_string(),
) )
.await?; .await?;