40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
package auth
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// IsAuthTokenPayloadKey returns true if key is a credential or token lifecycle field
|
|
// that should not overwrite newly acquired OAuth credentials during metadata merge.
|
|
func IsAuthTokenPayloadKey(key string) bool {
|
|
switch strings.ToLower(strings.TrimSpace(key)) {
|
|
case "access_token", "refresh_token", "id_token", "session_id",
|
|
"expired", "last_refresh", "expires_in", "timestamp",
|
|
"token_type", "user_code", "verification_uri", "verification_uri_complete":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// MergeExistingAuthMetadata merges user-configured metadata fields from existingMap
|
|
// into target.Metadata and target.Storage if target does not already define them.
|
|
func MergeExistingAuthMetadata(target *Auth, existingMap map[string]any) {
|
|
if target == nil || len(existingMap) == 0 {
|
|
return
|
|
}
|
|
if target.Metadata == nil {
|
|
target.Metadata = make(map[string]any)
|
|
}
|
|
for k, v := range existingMap {
|
|
if IsAuthTokenPayloadKey(k) {
|
|
continue
|
|
}
|
|
if _, exists := target.Metadata[k]; !exists {
|
|
target.Metadata[k] = v
|
|
}
|
|
}
|
|
if setter, ok := target.Storage.(interface{ SetMetadata(map[string]any) }); ok {
|
|
setter.SetMetadata(target.Metadata)
|
|
}
|
|
}
|