Browse Source
refactor(sts): replace hardcoded strings with constants
refactor(sts): replace hardcoded strings with constants
- Add comprehensive constants.go with all string literals - Replace hardcoded strings in sts_service.go, provider_factory.go, token_utils.go - Update error messages to use consistent constants - Standardize configuration field names and store types - Add JWT claim constants for token handling - Update tests to use test constants - Improve maintainability and reduce typos - Enhance distributed deployment consistency - Add CONSTANTS.md documentation All existing functionality preserved with improved type safety.pull/7160/head
5 changed files with 204 additions and 67 deletions
-
137weed/iam/sts/constants.go
-
28weed/iam/sts/cross_instance_token_test.go
-
44weed/iam/sts/provider_factory.go
-
34weed/iam/sts/sts_service.go
-
28weed/iam/sts/token_utils.go
@ -0,0 +1,137 @@ |
|||||
|
package sts |
||||
|
|
||||
|
// Store Types
|
||||
|
const ( |
||||
|
StoreTypeMemory = "memory" |
||||
|
StoreTypeFiler = "filer" |
||||
|
StoreTypeRedis = "redis" |
||||
|
) |
||||
|
|
||||
|
// Provider Types
|
||||
|
const ( |
||||
|
ProviderTypeOIDC = "oidc" |
||||
|
ProviderTypeLDAP = "ldap" |
||||
|
ProviderTypeSAML = "saml" |
||||
|
ProviderTypeMock = "mock" |
||||
|
) |
||||
|
|
||||
|
// Policy Effects
|
||||
|
const ( |
||||
|
EffectAllow = "Allow" |
||||
|
EffectDeny = "Deny" |
||||
|
) |
||||
|
|
||||
|
// Default Paths
|
||||
|
const ( |
||||
|
DefaultSessionBasePath = "/seaweedfs/iam/sessions" |
||||
|
DefaultPolicyBasePath = "/seaweedfs/iam/policies" |
||||
|
DefaultRoleBasePath = "/seaweedfs/iam/roles" |
||||
|
) |
||||
|
|
||||
|
// Default Values
|
||||
|
const ( |
||||
|
DefaultTokenDuration = 3600 // 1 hour in seconds
|
||||
|
DefaultMaxSessionLength = 43200 // 12 hours in seconds
|
||||
|
DefaultIssuer = "seaweedfs-sts" |
||||
|
MinSigningKeyLength = 16 // Minimum signing key length in bytes
|
||||
|
) |
||||
|
|
||||
|
// Configuration Field Names
|
||||
|
const ( |
||||
|
ConfigFieldFilerAddress = "filerAddress" |
||||
|
ConfigFieldBasePath = "basePath" |
||||
|
ConfigFieldIssuer = "issuer" |
||||
|
ConfigFieldClientID = "clientId" |
||||
|
ConfigFieldClientSecret = "clientSecret" |
||||
|
ConfigFieldJWKSUri = "jwksUri" |
||||
|
ConfigFieldScopes = "scopes" |
||||
|
ConfigFieldUserInfoUri = "userInfoUri" |
||||
|
ConfigFieldRedirectUri = "redirectUri" |
||||
|
) |
||||
|
|
||||
|
// Error Messages
|
||||
|
const ( |
||||
|
ErrConfigCannotBeNil = "config cannot be nil" |
||||
|
ErrProviderCannotBeNil = "provider cannot be nil" |
||||
|
ErrProviderNameEmpty = "provider name cannot be empty" |
||||
|
ErrProviderTypeEmpty = "provider type cannot be empty" |
||||
|
ErrTokenCannotBeEmpty = "token cannot be empty" |
||||
|
ErrSessionTokenCannotBeEmpty = "session token cannot be empty" |
||||
|
ErrSessionIDCannotBeEmpty = "session ID cannot be empty" |
||||
|
ErrSTSServiceNotInitialized = "STS service not initialized" |
||||
|
ErrProviderNotInitialized = "provider not initialized" |
||||
|
ErrInvalidTokenDuration = "token duration must be positive" |
||||
|
ErrInvalidMaxSessionLength = "max session length must be positive" |
||||
|
ErrIssuerRequired = "issuer is required" |
||||
|
ErrSigningKeyTooShort = "signing key must be at least %d bytes" |
||||
|
ErrFilerAddressRequired = "filer address is required" |
||||
|
ErrClientIDRequired = "clientId is required for OIDC provider" |
||||
|
ErrUnsupportedStoreType = "unsupported store type: %s" |
||||
|
ErrUnsupportedProviderType = "unsupported provider type: %s" |
||||
|
ErrInvalidTokenFormat = "invalid session token format: %w" |
||||
|
ErrSessionValidationFailed = "session validation failed: %w" |
||||
|
ErrInvalidToken = "invalid token: %w" |
||||
|
ErrTokenNotValid = "token is not valid" |
||||
|
ErrInvalidTokenClaims = "invalid token claims" |
||||
|
ErrInvalidIssuer = "invalid issuer" |
||||
|
ErrMissingSessionID = "missing session ID" |
||||
|
) |
||||
|
|
||||
|
// JWT Claims
|
||||
|
const ( |
||||
|
JWTClaimIssuer = "iss" |
||||
|
JWTClaimSubject = "sub" |
||||
|
JWTClaimAudience = "aud" |
||||
|
JWTClaimExpiration = "exp" |
||||
|
JWTClaimIssuedAt = "iat" |
||||
|
JWTClaimTokenType = "token_type" |
||||
|
) |
||||
|
|
||||
|
// Token Types
|
||||
|
const ( |
||||
|
TokenTypeSession = "session" |
||||
|
TokenTypeAccess = "access" |
||||
|
TokenTypeRefresh = "refresh" |
||||
|
) |
||||
|
|
||||
|
// AWS STS Actions
|
||||
|
const ( |
||||
|
ActionAssumeRole = "sts:AssumeRole" |
||||
|
ActionAssumeRoleWithWebIdentity = "sts:AssumeRoleWithWebIdentity" |
||||
|
ActionAssumeRoleWithCredentials = "sts:AssumeRoleWithCredentials" |
||||
|
ActionValidateSession = "sts:ValidateSession" |
||||
|
ActionRevokeSession = "sts:RevokeSession" |
||||
|
) |
||||
|
|
||||
|
// Session File Prefixes
|
||||
|
const ( |
||||
|
SessionFilePrefix = "session_" |
||||
|
SessionFileExt = ".json" |
||||
|
PolicyFilePrefix = "policy_" |
||||
|
PolicyFileExt = ".json" |
||||
|
RoleFileExt = ".json" |
||||
|
) |
||||
|
|
||||
|
// HTTP Headers
|
||||
|
const ( |
||||
|
HeaderAuthorization = "Authorization" |
||||
|
HeaderContentType = "Content-Type" |
||||
|
HeaderUserAgent = "User-Agent" |
||||
|
) |
||||
|
|
||||
|
// Content Types
|
||||
|
const ( |
||||
|
ContentTypeJSON = "application/json" |
||||
|
ContentTypeFormURLEncoded = "application/x-www-form-urlencoded" |
||||
|
) |
||||
|
|
||||
|
// Default Test Values
|
||||
|
const ( |
||||
|
TestSigningKey32Chars = "test-signing-key-32-characters-long" |
||||
|
TestIssuer = "test-sts" |
||||
|
TestClientID = "test-client" |
||||
|
TestSessionID = "test-session-123" |
||||
|
TestValidToken = "valid_test_token" |
||||
|
TestInvalidToken = "invalid_token" |
||||
|
TestExpiredToken = "expired_token" |
||||
|
) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue