-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
feat(users): Added blacklist for users #3469
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5d66397
feat(users): Added blacklist for users
racnan c2b3346
blacklist in auth
racnan 43c6e69
Merge branch 'main' into impl-user-blacklist
racnan ed0e994
analytics fix
racnan 508698e
Merge branch 'main' into impl-user-blacklist
racnan 4eb7675
chore: run formatter
hyperswitch-bot[bot] c2c3b42
Merge branch 'main' into impl-user-blacklist
racnan d43547a
cargo.lock
racnan 651582a
olap
racnan 201ac4b
Merge branch 'main' into impl-user-blacklist
racnan d014065
chore: run formatter
hyperswitch-bot[bot] 34e000d
minor
racnan 8529ceb
chore: run formatter
hyperswitch-bot[bot] 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 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 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 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 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 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 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 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 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,52 @@ | ||
use std::sync::Arc; | ||
|
||
use common_utils::date_time; | ||
use error_stack::{IntoReport, ResultExt}; | ||
use redis_interface::RedisConnectionPool; | ||
|
||
use crate::{ | ||
consts::{JWT_TOKEN_TIME_IN_SECS, USER_BLACKLIST_PREFIX}, | ||
core::errors::{ApiErrorResponse, RouterResult, UserErrors, UserResult}, | ||
routes::{app::AppStateInfo, AppState}, | ||
}; | ||
|
||
pub async fn insert_user_in_blacklist(state: &AppState, user_id: &str) -> UserResult<()> { | ||
let token = format!("{}{}", USER_BLACKLIST_PREFIX, user_id); | ||
let expiry = | ||
expiry_to_i64(JWT_TOKEN_TIME_IN_SECS).change_context(UserErrors::InternalServerError)?; | ||
let redis_conn = get_redis_connection(state).change_context(UserErrors::InternalServerError)?; | ||
redis_conn | ||
.set_key_with_expiry(token.as_str(), date_time::now_unix_timestamp(), expiry) | ||
.await | ||
.change_context(UserErrors::InternalServerError) | ||
} | ||
|
||
pub async fn check_user_in_blacklist<A: AppStateInfo>( | ||
state: &A, | ||
user_id: &str, | ||
token_expiry: u64, | ||
) -> RouterResult<bool> { | ||
let token = format!("{}{}", USER_BLACKLIST_PREFIX, user_id); | ||
let token_issued_at = expiry_to_i64(token_expiry - JWT_TOKEN_TIME_IN_SECS)?; | ||
let redis_conn = get_redis_connection(state)?; | ||
redis_conn | ||
.get_key::<Option<i64>>(token.as_str()) | ||
.await | ||
.change_context(ApiErrorResponse::InternalServerError) | ||
.map(|timestamp| timestamp.is_some_and(|timestamp| timestamp > token_issued_at)) | ||
} | ||
|
||
fn get_redis_connection<A: AppStateInfo>(state: &A) -> RouterResult<Arc<RedisConnectionPool>> { | ||
state | ||
.store() | ||
.get_redis_conn() | ||
.change_context(ApiErrorResponse::InternalServerError) | ||
.attach_printable("Failed to get redis connection") | ||
} | ||
|
||
fn expiry_to_i64(expiry: u64) -> RouterResult<i64> { | ||
expiry | ||
.try_into() | ||
.into_report() | ||
.change_context(ApiErrorResponse::InternalServerError) | ||
} | ||
This file contains 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.
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.
You can inline this