mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-28 12:45:47 +00:00
feat: multiple oauth providers (#355)
* feat: add flag decoder (candidate) * refactor: finalize flags decoder * feat: add env decoder * feat: add oauth config parsing logic * feat: implement backend logic for multiple oauth providers * feat: implement multiple oauth providers in the frontend * feat: add some default icons * chore: add credits for parser * feat: style oauth auto redirect screen * fix: bot suggestions * refactor: rework decoders using simpler and more efficient pattern * refactor: rework oauth name database migration
This commit is contained in:
81
internal/utils/decoders/decoders.go
Normal file
81
internal/utils/decoders/decoders.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package decoders
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"tinyauth/internal/config"
|
||||
)
|
||||
|
||||
func NormalizeKeys(keys map[string]string, rootName string, sep string) map[string]string {
|
||||
normalized := make(map[string]string)
|
||||
knownKeys := getKnownKeys()
|
||||
|
||||
for k, v := range keys {
|
||||
var finalKey []string
|
||||
var suffix string
|
||||
var camelClientName string
|
||||
var camelField string
|
||||
|
||||
finalKey = append(finalKey, rootName)
|
||||
finalKey = append(finalKey, "providers")
|
||||
cebabKey := strings.ToLower(k)
|
||||
|
||||
for _, known := range knownKeys {
|
||||
if strings.HasSuffix(cebabKey, strings.ReplaceAll(known, "-", sep)) {
|
||||
suffix = known
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if suffix == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
clientNameParts := strings.Split(strings.TrimPrefix(strings.TrimSuffix(cebabKey, sep+strings.ReplaceAll(suffix, "-", sep)), "providers"+sep), sep)
|
||||
|
||||
for i, p := range clientNameParts {
|
||||
if i == 0 {
|
||||
camelClientName += p
|
||||
continue
|
||||
}
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
camelClientName += strings.ToUpper(string([]rune(p)[0])) + string([]rune(p)[1:])
|
||||
}
|
||||
|
||||
finalKey = append(finalKey, camelClientName)
|
||||
|
||||
filedParts := strings.Split(suffix, "-")
|
||||
|
||||
for i, p := range filedParts {
|
||||
if i == 0 {
|
||||
camelField += p
|
||||
continue
|
||||
}
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
camelField += strings.ToUpper(string([]rune(p)[0])) + string([]rune(p)[1:])
|
||||
}
|
||||
|
||||
finalKey = append(finalKey, camelField)
|
||||
normalized[strings.Join(finalKey, ".")] = v
|
||||
}
|
||||
|
||||
return normalized
|
||||
}
|
||||
|
||||
func getKnownKeys() []string {
|
||||
var known []string
|
||||
|
||||
p := config.OAuthServiceConfig{}
|
||||
v := reflect.ValueOf(p)
|
||||
typeOfP := v.Type()
|
||||
|
||||
for field := range typeOfP.NumField() {
|
||||
known = append(known, typeOfP.Field(field).Tag.Get("key"))
|
||||
}
|
||||
|
||||
return known
|
||||
}
|
||||
Reference in New Issue
Block a user