Skip to content
Merged
Show file tree
Hide file tree
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
31 changes: 31 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ utoipa = { version = "5.4", features = ["axum_extras", "chrono", "yaml"] }
utoipa-axum = { version = "0.2" }
utoipa-swagger-ui = { version = "9.0", features = ["axum", "vendored"], default-features = false }
uuid = { version = "1.18", features = ["v4"] }
validator = { version = "0.20", features = ["derive"] }
webauthn-rs = { version = "0.5", features = ["danger-allow-state-serialisation"] }
webauthn-rs-proto = { version = "0.5" }

Expand Down
8 changes: 8 additions & 0 deletions src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ pub enum KeystoneApiError {
#[error("changing current authentication scope is forbidden")]
AuthenticationRescopeForbidden,

/// Request validation error.
#[error("request validation failed: {source}")]
Validator {
/// The source of the error.
#[from]
source: validator::ValidationErrors,
},

/// Others.
#[error(transparent)]
Other(#[from] eyre::Report),
Expand Down
63 changes: 51 additions & 12 deletions src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ use chrono::{DateTime, Utc};
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use validator::{Validate, ValidationErrors};

use crate::catalog::types::{Endpoint as ProviderEndpoint, Service};
use crate::resource::types as resource_provider_types;

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema)]
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema, Validate)]
pub struct Versions {
#[validate(nested)]
pub versions: Values,
}

Expand All @@ -36,13 +38,15 @@ impl IntoResponse for Versions {
}
}

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema)]
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema, Validate)]
pub struct Values {
#[validate(nested)]
pub values: Vec<Version>,
}

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema)]
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema, Validate)]
pub struct SingleVersion {
#[validate(nested)]
pub version: Version,
}

Expand All @@ -52,15 +56,18 @@ impl IntoResponse for SingleVersion {
}
}

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema)]
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema, Validate)]
pub struct Version {
#[validate(length(max = 5))]
pub id: String,
pub status: VersionStatus,
#[serde(skip_serializing_if = "Option::is_none")]
pub updated: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[validate(nested)]
pub links: Option<Vec<Link>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[validate(nested)]
pub media_types: Option<Vec<MediaType>>,
}

Expand All @@ -73,9 +80,11 @@ pub enum VersionStatus {
Experimental,
}

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema)]
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema, Validate)]
pub struct Link {
#[validate(length(max = 10))]
pub rel: String,
#[validate(url)]
pub href: String,
}

Expand All @@ -88,7 +97,7 @@ impl Link {
}
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, ToSchema)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, ToSchema, Validate)]
pub struct MediaType {
pub base: String,
pub r#type: String,
Expand All @@ -107,19 +116,28 @@ impl Default for MediaType {
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema)]
pub struct Catalog(Vec<CatalogService>);

impl Validate for Catalog {
fn validate(&self) -> Result<(), ValidationErrors> {
self.0.validate()
}
}

impl IntoResponse for Catalog {
fn into_response(self) -> Response {
(StatusCode::OK, Json(self)).into_response()
}
}

/// A catalog object.
#[derive(Builder, Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema)]
#[derive(Builder, Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema, Validate)]
#[builder(setter(strip_option, into))]
pub struct CatalogService {
pub r#type: Option<String>,
#[validate(length(max = 255))]
pub name: Option<String>,
#[validate(length(max = 64))]
pub id: String,
#[validate(nested)]
pub endpoints: Vec<Endpoint>,
}

Expand All @@ -135,15 +153,20 @@ impl From<(Service, Vec<ProviderEndpoint>)> for CatalogService {
}

/// A Catalog Endpoint.
#[derive(Builder, Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema)]
#[derive(Builder, Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema, Validate)]
#[builder(setter(strip_option, into))]
pub struct Endpoint {
#[validate(length(max = 64))]
pub id: String,
#[validate(url)]
pub url: String,
#[validate(length(max = 64))]
pub interface: String,
#[builder(default)]
#[validate(length(max = 64))]
pub region: Option<String>,
#[builder(default)]
#[validate(length(max = 64))]
pub region_id: Option<String>,
}

Expand Down Expand Up @@ -190,46 +213,62 @@ pub enum Scope {
System(System),
}

impl Validate for Scope {
fn validate(&self) -> Result<(), ValidationErrors> {
match self {
Self::Project(project) => project.validate(),
Self::Domain(domain) => domain.validate(),
Self::System(system) => system.validate(),
}
}
}

/// Project scope information.
#[derive(Builder, Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema)]
#[derive(Builder, Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema, Validate)]
#[builder(setter(into, strip_option))]
pub struct ProjectScope {
/// Project ID.
#[builder(default)]
#[validate(length(max = 64))]
pub id: Option<String>,
/// Project Name.
#[builder(default)]
#[validate(length(max = 64))]
pub name: Option<String>,
/// Project domain.
#[builder(default)]
pub domain: Option<Domain>,
}

/// Domain information.
#[derive(Builder, Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema)]
#[derive(Builder, Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema, Validate)]
#[builder(setter(into, strip_option))]
pub struct Domain {
/// Domain ID.
#[builder(default)]
#[validate(length(max = 64))]
pub id: Option<String>,
/// Domain Name.
#[builder(default)]
#[validate(length(max = 64))]
pub name: Option<String>,
}

/// Project information.
#[derive(Builder, Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema)]
#[derive(Builder, Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema, Validate)]
pub struct Project {
/// Project ID.
#[validate(length(max = 64))]
pub id: String,
/// Project Name.
#[validate(length(max = 64))]
pub name: String,
/// project domain.
pub domain: Domain,
}

/// System scope.
#[derive(Builder, Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema)]
#[derive(Builder, Clone, Debug, Default, Deserialize, PartialEq, Serialize, ToSchema, Validate)]
#[builder(setter(into, strip_option))]
pub struct System {
/// All systems access.
Expand Down
2 changes: 2 additions & 0 deletions src/api/v3/auth/token/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use axum::{
http::StatusCode,
response::IntoResponse,
};
use validator::Validate;

use crate::api::v3::auth::token::common::{authenticate_request, get_authz_info};
use crate::api::v3::auth::token::types::{
Expand Down Expand Up @@ -46,6 +47,7 @@ pub(super) async fn create(
State(state): State<ServiceState>,
Json(req): Json<AuthRequest>,
) -> Result<impl IntoResponse, KeystoneApiError> {
req.validate()?;
let authed_info = authenticate_request(&state, &req).await?;
let authz_info = get_authz_info(&state, &req).await?;
if let Some(restriction_id) = &authed_info.token_restriction_id {
Expand Down
Loading
Loading