Skip to content

Commit

Permalink
feat: increase test coverage in conf package to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Stockton committed Feb 6, 2025
1 parent 8078cdc commit 911954c
Show file tree
Hide file tree
Showing 9 changed files with 1,555 additions and 89 deletions.
83 changes: 52 additions & 31 deletions internal/conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,15 @@ func LoadDirectory(configDir string) error {
// If at least one path was found we load the configuration files in the
// directory. We don't call override without config files because it will
// override the env vars previously set with a ".env", if one exists.
if len(paths) > 0 {
if err := godotenv.Overload(paths...); err != nil {
return loadDirectoryPaths(paths...)
}

func loadDirectoryPaths(p ...string) error {
// If at least one path was found we load the configuration files in the
// directory. We don't call override without config files because it will
// override the env vars previously set with a ".env", if one exists.
if len(p) > 0 {
if err := godotenv.Overload(p...); err != nil {
return err
}
}
Expand Down Expand Up @@ -811,7 +818,10 @@ func loadGlobal(config *GlobalConfiguration) error {
if err := config.Validate(); err != nil {
return err
}
return populateGlobal(config)
}

func populateGlobal(config *GlobalConfiguration) error {
if config.Hook.PasswordVerificationAttempt.Enabled {
if err := config.Hook.PasswordVerificationAttempt.PopulateExtensibilityPoint(); err != nil {
return err
Expand Down Expand Up @@ -892,37 +902,9 @@ func (config *GlobalConfiguration) ApplyDefaults() error {

if len(config.JWT.Keys) == 0 {
// transform the secret into a JWK for consistency
privKey, err := jwk.FromRaw([]byte(config.JWT.Secret))
if err != nil {
if err := config.applyDefaultsJWT([]byte(config.JWT.Secret)); err != nil {
return err
}
if config.JWT.KeyID != "" {
if err := privKey.Set(jwk.KeyIDKey, config.JWT.KeyID); err != nil {
return err
}
}
if privKey.Algorithm().String() == "" {
if err := privKey.Set(jwk.AlgorithmKey, jwt.SigningMethodHS256.Name); err != nil {
return err
}
}
if err := privKey.Set(jwk.KeyUsageKey, "sig"); err != nil {
return err
}
if len(privKey.KeyOps()) == 0 {
if err := privKey.Set(jwk.KeyOpsKey, jwk.KeyOperationList{jwk.KeyOpSign, jwk.KeyOpVerify}); err != nil {
return err
}
}
pubKey, err := privKey.PublicKey()
if err != nil {
return err
}
config.JWT.Keys = make(JwtKeysDecoder)
config.JWT.Keys[config.JWT.KeyID] = JwkInfo{
PublicKey: pubKey,
PrivateKey: privKey,
}
}

if config.JWT.ValidMethods == nil {
Expand Down Expand Up @@ -1036,6 +1018,45 @@ func (config *GlobalConfiguration) ApplyDefaults() error {

return nil
}
func (config *GlobalConfiguration) applyDefaultsJWT(secret []byte) error {
// transform the secret into a JWK for consistency
privKey, err := jwk.FromRaw(secret)
if err != nil {
return err
}
return config.applyDefaultsJWTPrivateKey(privKey)
}

func (config *GlobalConfiguration) applyDefaultsJWTPrivateKey(privKey jwk.Key) error {
if config.JWT.KeyID != "" {
if err := privKey.Set(jwk.KeyIDKey, config.JWT.KeyID); err != nil {
return err
}
}
if privKey.Algorithm().String() == "" {
if err := privKey.Set(jwk.AlgorithmKey, jwt.SigningMethodHS256.Name); err != nil {
return err
}
}
if err := privKey.Set(jwk.KeyUsageKey, "sig"); err != nil {
return err
}
if len(privKey.KeyOps()) == 0 {
if err := privKey.Set(jwk.KeyOpsKey, jwk.KeyOperationList{jwk.KeyOpSign, jwk.KeyOpVerify}); err != nil {
return err
}
}
pubKey, err := privKey.PublicKey()
if err != nil {
return err
}
config.JWT.Keys = make(JwtKeysDecoder)
config.JWT.Keys[config.JWT.KeyID] = JwkInfo{
PublicKey: pubKey,
PrivateKey: privKey,
}
return nil
}

// Validate validates all of configuration.
func (c *GlobalConfiguration) Validate() error {
Expand Down
Loading

0 comments on commit 911954c

Please sign in to comment.