-
Notifications
You must be signed in to change notification settings - Fork 9
feat: Implement authc by app credential #1187
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
Open
konac-hamza
wants to merge
1
commit into
openstack-experimental:main
Choose a base branch
from
konac-hamza:feature/authc-by-appcred
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
138 changes: 138 additions & 0 deletions
138
crates/appcred-driver-sql/src/application_credential/verify.rs
This file contains hidden or 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,138 @@ | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| //! # Verify application credential | ||
| use sea_orm::DatabaseConnection; | ||
| use sea_orm::entity::*; | ||
| use sea_orm::query::*; | ||
| use secrecy::SecretString; | ||
|
|
||
| use openstack_keystone_config::Config; | ||
| use openstack_keystone_core::application_credential::ApplicationCredentialProviderError; | ||
| use openstack_keystone_core::error::DbContextExt; | ||
| use openstack_keystone_password_hashing as password_hashing; | ||
|
|
||
| use crate::entity::{ | ||
| application_credential as db_application_credential, | ||
| prelude::ApplicationCredential as DbApplicationCredential, | ||
| }; | ||
|
|
||
| pub async fn verify_secret( | ||
| config: &Config, | ||
| db: &DatabaseConnection, | ||
| credential_id: &str, | ||
| secret: &SecretString, | ||
| ) -> Result<(), ApplicationCredentialProviderError> { | ||
| let record = DbApplicationCredential::find() | ||
| .filter(db_application_credential::Column::Id.eq(credential_id)) | ||
| .one(db) | ||
| .await | ||
| .context("looking up application credential for authentication")? | ||
| .ok_or(ApplicationCredentialProviderError::AuthenticationFailed)?; | ||
|
|
||
| // record is db_application_credential::Model, which has secret_hash | ||
| let matched = password_hashing::verify_password(config, secret, &record.secret_hash) | ||
| .await | ||
| .map_err(ApplicationCredentialProviderError::password_hash)?; | ||
|
|
||
| if !matched { | ||
| return Err(ApplicationCredentialProviderError::AuthenticationFailed); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use chrono::{DateTime, Utc}; | ||
| use sea_orm::{DatabaseBackend, MockDatabase, Transaction}; | ||
|
|
||
| use openstack_keystone_config::PasswordHashingAlgo; | ||
|
|
||
| use super::*; | ||
| use crate::entity::application_credential as db_application_credential; | ||
|
|
||
| fn make_app_cred_model(id: &str, secret_hash: &str) -> db_application_credential::Model { | ||
| db_application_credential::Model { | ||
| internal_id: 1, | ||
| id: id.to_string(), | ||
| name: "fake appcred".to_string(), | ||
| secret_hash: secret_hash.to_string(), | ||
| description: Some("description".to_string()), | ||
| user_id: "user_id".to_string(), | ||
| project_id: Some("project_id".to_string()), | ||
| expires_at: Some(DateTime::<Utc>::MIN_UTC.timestamp_micros()), | ||
| system: None, | ||
| unrestricted: Some(true), | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_verify_secret_success() { | ||
| let mut config = Config::default(); | ||
| config.identity.password_hashing_algorithm = PasswordHashingAlgo::None; | ||
|
|
||
| let db = MockDatabase::new(DatabaseBackend::Postgres) | ||
| .append_query_results([vec![make_app_cred_model("app_cred_id", "test_secret")]]) | ||
| .into_connection(); | ||
|
|
||
| let result = verify_secret(&config, &db, "app_cred_id", &"test_secret".into()).await; | ||
|
|
||
| assert!(result.is_ok()); | ||
|
|
||
| assert_eq!( | ||
| db.into_transaction_log(), | ||
| [Transaction::from_sql_and_values( | ||
| DatabaseBackend::Postgres, | ||
| r#"SELECT "application_credential"."internal_id", "application_credential"."id", "application_credential"."name", "application_credential"."secret_hash", "application_credential"."description", "application_credential"."user_id", "application_credential"."project_id", "application_credential"."expires_at", "application_credential"."system", "application_credential"."unrestricted" FROM "application_credential" WHERE "application_credential"."id" = $1 LIMIT $2"#, | ||
| ["app_cred_id".into(), 1u64.into()] | ||
| )] | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_verify_secret_wrong_secret() { | ||
| let mut config = Config::default(); | ||
| config.identity.password_hashing_algorithm = PasswordHashingAlgo::None; | ||
|
|
||
| let db = MockDatabase::new(DatabaseBackend::Postgres) | ||
| .append_query_results([vec![make_app_cred_model("app_cred_id", "test_secret")]]) | ||
| .into_connection(); | ||
|
|
||
| let result = verify_secret(&config, &db, "app_cred_id", &"wrong_secret".into()).await; | ||
|
|
||
| assert!(result.is_err()); | ||
| assert!(matches!( | ||
| result.unwrap_err(), | ||
| ApplicationCredentialProviderError::AuthenticationFailed | ||
| )); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_verify_secret_not_found() { | ||
| let db = MockDatabase::new(DatabaseBackend::Postgres) | ||
| .append_query_results([Vec::<db_application_credential::Model>::new()]) | ||
| .into_connection(); | ||
|
|
||
| let mut config = Config::default(); | ||
| config.identity.password_hashing_algorithm = PasswordHashingAlgo::None; | ||
|
|
||
| let result = verify_secret(&config, &db, "nonexistent_id", &"test_secret".into()).await; | ||
|
|
||
| assert!(result.is_err()); | ||
| assert!(matches!( | ||
| result.unwrap_err(), | ||
| ApplicationCredentialProviderError::AuthenticationFailed | ||
| )); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also as mentioned below I would add here a Builder validator (https://docs.rs/derive_builder/latest/derive_builder/#pre-build-validation) to ensure id or name are present