Fix up KeyStore directory read.

This commit is contained in:
Captain ALM 2024-06-09 21:00:18 +01:00
parent a0d03c0dfb
commit a94ed7a2e5
Signed by untrusted user: alfred
GPG Key ID: 4E4ADD02609997B1

View File

@ -42,12 +42,14 @@ func NewMJwtKeyStoreFromDirectory(directory, keyPrvExt, keyPubExt string) (KeySt
} }
// Import keys from files, based on extension // Import keys from files, based on extension
for _, entry := range dirEntries { for _, entry := range dirEntries {
if !entry.IsDir() { if entry.IsDir() {
firstDotIdx := strings.Index(entry.Name(), ".") continue
lastDotIdx := strings.LastIndex(entry.Name(), ".") }
if firstDotIdx > 0 && lastDotIdx+1 < len(entry.Name()) { kID, _, _ := strings.Cut(entry.Name(), ".")
if entry.Name()[lastDotIdx+1:] == keyPrvExt { if kID == "" {
kID := entry.Name()[:firstDotIdx] continue
}
if path.Ext(entry.Name()) == "."+keyPrvExt {
// Load rsa private key with the file name as the kID (Up to the first .) // Load rsa private key with the file name as the kID (Up to the first .)
key, err2 := rsaprivate.Read(path.Join(directory, entry.Name())) key, err2 := rsaprivate.Read(path.Join(directory, entry.Name()))
if err2 == nil { if err2 == nil {
@ -56,8 +58,7 @@ func NewMJwtKeyStoreFromDirectory(directory, keyPrvExt, keyPubExt string) (KeySt
} else { } else {
err = err2 err = err2
} }
} else if entry.Name()[lastDotIdx+1:] == keyPubExt { } else if path.Ext(entry.Name()) == "."+keyPubExt {
kID := entry.Name()[:firstDotIdx]
// Load rsa public key with the file name as the kID (Up to the first .) // Load rsa public key with the file name as the kID (Up to the first .)
key, err2 := rsapublic.Read(path.Join(directory, entry.Name())) key, err2 := rsapublic.Read(path.Join(directory, entry.Name()))
if err2 == nil { if err2 == nil {
@ -71,8 +72,6 @@ func NewMJwtKeyStoreFromDirectory(directory, keyPrvExt, keyPubExt string) (KeySt
} }
} }
} }
}
}
return ks, err return ks, err
} }