Skip to content
Clone
1480255
194 lines 8.7 KB Rust
use crate::errors::AuthError;
use crate::protos::auth::common::v1::Code;
use export::KenAuthError;
pub fn code_of(error: &AuthError) -> Code {
match error {
AuthError::Expired => Code::ExpiredChallenge,
AuthError::InvalidPayload => Code::InvalidPayload,
AuthError::InvalidProof => Code::InvalidProof,
AuthError::MalformedRequest => Code::MalformedRequest,
AuthError::InvalidUsername => Code::InvalidUsername,
AuthError::InvalidDisplayname => Code::InvalidDisplayname,
AuthError::InvalidEmail => Code::InvalidEmail,
AuthError::EmailUnchanged => Code::EmailUnchanged,
AuthError::InvalidBirthDate => Code::InvalidBirthDate,
AuthError::UnderMinimumAge { .. } => Code::UnderMinimumAge,
AuthError::InvalidPassword => Code::InvalidPassword,
AuthError::PasswordHash(_) => Code::Internal,
AuthError::UsernameTaken => Code::UsernameTaken,
AuthError::EmailTaken => Code::EmailTaken,
AuthError::InvalidEmailCode => Code::InvalidEmailCode,
AuthError::EmailCodeAttemptsExceeded => Code::EmailCodeAttemptsExceeded,
AuthError::InvalidResetToken => Code::InvalidResetToken,
AuthError::Unauthenticated => Code::Unauthenticated,
AuthError::Forbidden => Code::Forbidden,
AuthError::BadRequest(_) => Code::MalformedRequest,
AuthError::Conflict(_) => Code::VersionMismatch,
AuthError::InvalidCredentials => Code::InvalidCredentials,
AuthError::LoginForbidden { .. } => Code::LoginForbidden,
AuthError::AccountUnavailable { .. } => Code::UserDisabled,
AuthError::MfaRequired => Code::MfaRequired,
AuthError::TotpNotConfigured => Code::MfaNotConfigured,
AuthError::TotpAlreadyEnabled => Code::TotpAlreadyEnabled,
AuthError::TotpCodeInvalid => Code::InvalidCredentials,
AuthError::TotpStepReused => Code::TotpStepReused,
AuthError::RecoveryCodeInvalid => Code::InvalidCredentials,
AuthError::RecoveryCodesExhausted => Code::RecoveryCodesExhausted,
AuthError::InvalidAccessToken => Code::InvalidAccessToken,
AuthError::AccessTokenNotFound => Code::AccessKeyNotFound,
AuthError::InvalidAccessKeyName => Code::InvalidName,
AuthError::InvalidScope => Code::InvalidScope,
AuthError::InvalidExpiry => Code::InvalidExpiry,
AuthError::TooManyAccessKeys => Code::TooManyAccessKeys,
AuthError::SshKeyInvalid => Code::InvalidPublicKey,
AuthError::InvalidPermission { .. } => Code::InvalidPermission,
AuthError::OrganizationNotFound => Code::OrganizationNotFound,
AuthError::OrganizationMemberNotFound => Code::OrganizationMemberNotFound,
AuthError::OrganizationRoleNotFound => Code::OrganizationRoleNotFound,
AuthError::OrganizationInviteNotFound => Code::OrganizationInviteNotFound,
AuthError::OrganizationSlugTaken => Code::OrganizationSlugTaken,
AuthError::OrganizationMemberExists => Code::OrganizationMemberExists,
AuthError::OrganizationRoleKeyTaken => Code::OrganizationRoleKeyTaken,
AuthError::OrganizationRoleInUse => Code::OrganizationRoleInUse,
AuthError::OrganizationRoleSystem => Code::OrganizationRoleSystem,
AuthError::OrganizationInviteExhausted => Code::OrganizationInviteExhausted,
AuthError::OrganizationInviteExpired => Code::OrganizationInviteExpired,
AuthError::OrganizationOwnerRequired => Code::OrganizationOwnerRequired,
AuthError::OrganizationHasMembers => Code::OrganizationHasMembers,
AuthError::OrganizationInactive => Code::OrganizationInactive,
AuthError::OrganizationMemberBanned => Code::OrganizationMemberBanned,
AuthError::VersionMismatch { .. } => Code::VersionMismatch,
AuthError::SshKeyTaken => Code::SshKeyTaken,
AuthError::SshKeyNameInvalid => Code::InvalidName,
AuthError::SshKeyNotFound => Code::SshKeyNotFound,
AuthError::AgreementNotFound { .. } => Code::AgreementNotFound,
AuthError::AgreementNotAccepted { .. } => Code::AgreementNotAccepted,
AuthError::DeletionAlreadyRequested => Code::DeletionAlreadyRequested,
AuthError::DeletionNotRequested => Code::DeletionNotRequested,
AuthError::RateLimited { .. } => Code::RateLimited,
AuthError::UserNotFound => Code::UserNotFound,
AuthError::AppNotFound => Code::NotFound,
AuthError::SessionNotFound => Code::SessionNotFound,
AuthError::TokenNotFound => Code::TokenNotFound,
AuthError::TokenExpired => Code::TokenExpired,
AuthError::TokenRevoked => Code::TokenRevoked,
AuthError::SecurityVersionStale { .. } => Code::SecurityVersionStale,
AuthError::ScopeInsufficient { .. } => Code::ScopeInsufficient,
AuthError::Unavailable(_) => Code::Unavailable,
AuthError::Internal(_) => Code::Internal,
AuthError::Database(_) => Code::DatabaseError,
AuthError::Cache(_) => Code::CacheError,
AuthError::Session(_) => Code::CacheError,
AuthError::Email(_) => Code::EmailError,
AuthError::Serialization(_) => Code::SerializationError,
}
}
fn category(code: Code) -> u8 {
match code {
Code::Unspecified => 0,
Code::InvalidArgument
| Code::MalformedRequest
| Code::ExpiredChallenge
| Code::InvalidProof
| Code::InvalidPayload
| Code::InvalidEmail
| Code::InvalidUsername
| Code::InvalidDisplayname
| Code::InvalidBirthDate
| Code::InvalidPassword
| Code::InvalidEmailCode
| Code::InvalidResetToken
| Code::InvalidScope
| Code::InvalidExpiry
| Code::InvalidName
| Code::InvalidPublicKey => 1,
Code::Unauthenticated
| Code::InvalidCredentials
| Code::InvalidAccessToken
| Code::TokenExpired
| Code::TokenRevoked
| Code::SecurityVersionStale
| Code::MfaRequired => 2,
Code::Forbidden
| Code::LoginForbidden
| Code::UserDisabled
| Code::UnderMinimumAge
| Code::ScopeInsufficient
| Code::AgreementNotAccepted => 1,
Code::MfaNotConfigured
| Code::TooManyAccessKeys
| Code::TotpAlreadyEnabled
| Code::TotpStepReused
| Code::DeletionAlreadyRequested
| Code::DeletionNotRequested
| Code::OrganizationRoleInUse
| Code::OrganizationRoleSystem
| Code::OrganizationInviteExhausted
| Code::OrganizationInviteExpired
| Code::OrganizationOwnerRequired => 1,
Code::Conflict
| Code::UsernameTaken
| Code::EmailTaken
| Code::SshKeyTaken
| Code::OrganizationSlugTaken
| Code::OrganizationMemberExists
| Code::OrganizationRoleKeyTaken
| Code::OrganizationHasMembers
| Code::VersionMismatch
| Code::EmailUnchanged => 1,
Code::NotFound
| Code::UserNotFound
| Code::SessionNotFound
| Code::AccessKeyNotFound
| Code::SshKeyNotFound
| Code::AgreementNotFound
| Code::OrganizationNotFound
| Code::OrganizationMemberNotFound
| Code::OrganizationRoleNotFound
| Code::OrganizationInviteNotFound
| Code::TokenNotFound => 3,
Code::OrganizationInactive | Code::OrganizationMemberBanned => 1,
Code::InvalidPermission => 1,
Code::RateLimited
| Code::EmailCodeAttemptsExceeded
| Code::RecoveryCodesExhausted
| Code::PayloadTooLarge => 1,
Code::Internal
| Code::DatabaseError
| Code::CacheError
| Code::SerializationError
| Code::EmailError => 0,
Code::Unavailable => 4,
}
}
pub fn status(error: &AuthError, request_id: Option<&str>) -> KenAuthError {
status_of(code_of(error), error.message(), request_id)
}
pub fn status_of(
code: Code,
message: impl Into<String>,
_request_id: Option<&str>,
) -> KenAuthError {
let message = message.into();
match category(code) {
1 => KenAuthError::Rejected(message),
2 => KenAuthError::Unauthenticated,
3 => KenAuthError::NotFound,
4 => KenAuthError::Unavailable(message),
_ => KenAuthError::Internal(message),
}
}
pub fn invalid_argument(message: impl Into<String>, request_id: Option<&str>) -> KenAuthError {
status_of(Code::InvalidArgument, message, request_id)
}
pub fn unauthenticated(message: impl Into<String>, request_id: Option<&str>) -> KenAuthError {
status_of(Code::Unauthenticated, message, request_id)
}
pub fn forbidden(message: impl Into<String>, request_id: Option<&str>) -> KenAuthError {
status_of(Code::Forbidden, message, request_id)
}
pub fn unavailable(message: impl Into<String>, request_id: Option<&str>) -> KenAuthError {
status_of(Code::Unavailable, message, request_id)
}