mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-28 12:45:47 +00:00
feat: add flag decoder (candidate)
This commit is contained in:
@@ -174,3 +174,13 @@ type AppPath struct {
|
||||
Allow string
|
||||
Block string
|
||||
}
|
||||
|
||||
// Flags
|
||||
|
||||
type Providers struct {
|
||||
Providers map[string]ProviderConfig
|
||||
}
|
||||
|
||||
type ProviderConfig struct {
|
||||
Config OAuthServiceConfig
|
||||
}
|
||||
|
||||
55
internal/utils/decoders/flags_decoder.go
Normal file
55
internal/utils/decoders/flags_decoder.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package decoders
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"tinyauth/internal/config"
|
||||
"tinyauth/internal/utils"
|
||||
|
||||
"github.com/traefik/paerser/parser"
|
||||
)
|
||||
|
||||
func DecodeFlags(flags map[string]string) (config.Providers, error) {
|
||||
// Normalize flags (sorry to whoever has to read this)
|
||||
// --providers-client1-client-id -> tinyauth.providers.client1.clientId
|
||||
normalized := make(map[string]string)
|
||||
for k, v := range flags {
|
||||
newKey := ""
|
||||
|
||||
nk := strings.TrimPrefix(k, "--")
|
||||
parts := strings.SplitN(nk, "-", 4)
|
||||
|
||||
for i, part := range parts {
|
||||
if i == 3 {
|
||||
subParts := strings.Split(part, "-")
|
||||
for j, subPart := range subParts {
|
||||
if j == 0 {
|
||||
newKey += "." + subPart
|
||||
} else {
|
||||
newKey += utils.Capitalize(subPart)
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if i == 0 {
|
||||
newKey += part
|
||||
continue
|
||||
}
|
||||
newKey += "." + part
|
||||
}
|
||||
|
||||
newKey = "tinyauth." + newKey
|
||||
normalized[newKey] = v
|
||||
|
||||
}
|
||||
|
||||
// Decode
|
||||
var providers config.Providers
|
||||
|
||||
err := parser.Decode(normalized, &providers, "tinyauth", "tinyauth.providers")
|
||||
|
||||
if err != nil {
|
||||
return config.Providers{}, err
|
||||
}
|
||||
|
||||
return providers, nil
|
||||
}
|
||||
60
internal/utils/decoders/flags_decoder_test.go
Normal file
60
internal/utils/decoders/flags_decoder_test.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package decoders_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"tinyauth/internal/config"
|
||||
"tinyauth/internal/utils/decoders"
|
||||
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
func TestDecodeFlags(t *testing.T) {
|
||||
// Variables
|
||||
expected := config.Providers{
|
||||
Providers: map[string]config.ProviderConfig{
|
||||
"client1": {
|
||||
Config: config.OAuthServiceConfig{
|
||||
ClientID: "client1-id",
|
||||
ClientSecret: "client1-secret",
|
||||
Scopes: []string{"client1-scope1", "client1-scope2"},
|
||||
RedirectURL: "client1-redirect-url",
|
||||
AuthURL: "client1-auth-url",
|
||||
UserinfoURL: "client1-user-info-url",
|
||||
InsecureSkipVerify: false,
|
||||
},
|
||||
},
|
||||
"client2": {
|
||||
Config: config.OAuthServiceConfig{
|
||||
ClientID: "client2-id",
|
||||
ClientSecret: "client2-secret",
|
||||
Scopes: []string{"client2-scope1", "client2-scope2"},
|
||||
RedirectURL: "client2-redirect-url",
|
||||
AuthURL: "client2-auth-url",
|
||||
UserinfoURL: "client2-user-info-url",
|
||||
InsecureSkipVerify: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
test := map[string]string{
|
||||
"--providers-client1-config-client-id": "client1-id",
|
||||
"--providers-client1-config-client-secret": "client1-secret",
|
||||
"--providers-client1-config-scopes": "client1-scope1,client1-scope2",
|
||||
"--providers-client1-config-redirect-url": "client1-redirect-url",
|
||||
"--providers-client1-config-auth-url": "client1-auth-url",
|
||||
"--providers-client1-config-user-info-url": "client1-user-info-url",
|
||||
"--providers-client1-config-insecure-skip-verify": "false",
|
||||
"--providers-client2-config-client-id": "client2-id",
|
||||
"--providers-client2-config-client-secret": "client2-secret",
|
||||
"--providers-client2-config-scopes": "client2-scope1,client2-scope2",
|
||||
"--providers-client2-config-redirect-url": "client2-redirect-url",
|
||||
"--providers-client2-config-auth-url": "client2-auth-url",
|
||||
"--providers-client2-config-user-info-url": "client2-user-info-url",
|
||||
"--providers-client2-config-insecure-skip-verify": "false",
|
||||
}
|
||||
|
||||
// Test
|
||||
res, err := decoders.DecodeFlags(test)
|
||||
assert.NilError(t, err)
|
||||
assert.DeepEqual(t, expected, res)
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
package decoders_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"tinyauth/internal/config"
|
||||
"tinyauth/internal/utils/decoders"
|
||||
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
func TestDecodeLabels(t *testing.T) {
|
||||
@@ -62,12 +63,6 @@ func TestDecodeLabels(t *testing.T) {
|
||||
|
||||
// Test
|
||||
result, err := decoders.DecodeLabels(test)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if reflect.DeepEqual(expected, result) == false {
|
||||
t.Fatalf("Expected %v but got %v", expected, result)
|
||||
}
|
||||
assert.NilError(t, err)
|
||||
assert.DeepEqual(t, expected, result)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user