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
|
||||
}
|
||||
44
internal/utils/decoders/decoders_test.go
Normal file
44
internal/utils/decoders/decoders_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package decoders_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"tinyauth/internal/utils/decoders"
|
||||
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
func TestNormalizeKeys(t *testing.T) {
|
||||
// Test with env
|
||||
test := map[string]string{
|
||||
"PROVIDERS_CLIENT1_CLIENT_ID": "my-client-id",
|
||||
"PROVIDERS_CLIENT1_CLIENT_SECRET": "my-client-secret",
|
||||
"PROVIDERS_MY_AWESOME_CLIENT_CLIENT_ID": "my-awesome-client-id",
|
||||
"PROVIDERS_MY_AWESOME_CLIENT_CLIENT_SECRET_FILE": "/path/to/secret",
|
||||
}
|
||||
expected := map[string]string{
|
||||
"tinyauth.providers.client1.clientId": "my-client-id",
|
||||
"tinyauth.providers.client1.clientSecret": "my-client-secret",
|
||||
"tinyauth.providers.myAwesomeClient.clientId": "my-awesome-client-id",
|
||||
"tinyauth.providers.myAwesomeClient.clientSecretFile": "/path/to/secret",
|
||||
}
|
||||
|
||||
normalized := decoders.NormalizeKeys(test, "tinyauth", "_")
|
||||
assert.DeepEqual(t, normalized, expected)
|
||||
|
||||
// Test with flags (assume -- is already stripped)
|
||||
test = map[string]string{
|
||||
"providers-client1-client-id": "my-client-id",
|
||||
"providers-client1-client-secret": "my-client-secret",
|
||||
"providers-my-awesome-client-client-id": "my-awesome-client-id",
|
||||
"providers-my-awesome-client-client-secret-file": "/path/to/secret",
|
||||
}
|
||||
expected = map[string]string{
|
||||
"tinyauth.providers.client1.clientId": "my-client-id",
|
||||
"tinyauth.providers.client1.clientSecret": "my-client-secret",
|
||||
"tinyauth.providers.myAwesomeClient.clientId": "my-awesome-client-id",
|
||||
"tinyauth.providers.myAwesomeClient.clientSecretFile": "/path/to/secret",
|
||||
}
|
||||
|
||||
normalized = decoders.NormalizeKeys(test, "tinyauth", "-")
|
||||
assert.DeepEqual(t, normalized, expected)
|
||||
}
|
||||
20
internal/utils/decoders/env_decoder.go
Normal file
20
internal/utils/decoders/env_decoder.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package decoders
|
||||
|
||||
import (
|
||||
"tinyauth/internal/config"
|
||||
|
||||
"github.com/traefik/paerser/parser"
|
||||
)
|
||||
|
||||
func DecodeEnv(env map[string]string) (config.Providers, error) {
|
||||
normalized := NormalizeKeys(env, "tinyauth", "_")
|
||||
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/env_decoder_test.go
Normal file
60
internal/utils/decoders/env_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 TestDecodeEnv(t *testing.T) {
|
||||
// Variables
|
||||
expected := config.Providers{
|
||||
Providers: map[string]config.OAuthServiceConfig{
|
||||
"client1": {
|
||||
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",
|
||||
Name: "Client1",
|
||||
InsecureSkipVerify: false,
|
||||
},
|
||||
"client2": {
|
||||
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",
|
||||
Name: "My Awesome Client2",
|
||||
InsecureSkipVerify: false,
|
||||
},
|
||||
},
|
||||
}
|
||||
test := map[string]string{
|
||||
"PROVIDERS_CLIENT1_CLIENT_ID": "client1-id",
|
||||
"PROVIDERS_CLIENT1_CLIENT_SECRET": "client1-secret",
|
||||
"PROVIDERS_CLIENT1_SCOPES": "client1-scope1,client1-scope2",
|
||||
"PROVIDERS_CLIENT1_REDIRECT_URL": "client1-redirect-url",
|
||||
"PROVIDERS_CLIENT1_AUTH_URL": "client1-auth-url",
|
||||
"PROVIDERS_CLIENT1_USER_INFO_URL": "client1-user-info-url",
|
||||
"PROVIDERS_CLIENT1_NAME": "Client1",
|
||||
"PROVIDERS_CLIENT1_INSECURE_SKIP_VERIFY": "false",
|
||||
"PROVIDERS_CLIENT2_CLIENT_ID": "client2-id",
|
||||
"PROVIDERS_CLIENT2_CLIENT_SECRET": "client2-secret",
|
||||
"PROVIDERS_CLIENT2_SCOPES": "client2-scope1,client2-scope2",
|
||||
"PROVIDERS_CLIENT2_REDIRECT_URL": "client2-redirect-url",
|
||||
"PROVIDERS_CLIENT2_AUTH_URL": "client2-auth-url",
|
||||
"PROVIDERS_CLIENT2_USER_INFO_URL": "client2-user-info-url",
|
||||
"PROVIDERS_CLIENT2_NAME": "My Awesome Client2",
|
||||
"PROVIDERS_CLIENT2_INSECURE_SKIP_VERIFY": "false",
|
||||
}
|
||||
|
||||
// Test
|
||||
res, err := decoders.DecodeEnv(test)
|
||||
assert.NilError(t, err)
|
||||
assert.DeepEqual(t, expected, res)
|
||||
}
|
||||
30
internal/utils/decoders/flags_decoder.go
Normal file
30
internal/utils/decoders/flags_decoder.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package decoders
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"tinyauth/internal/config"
|
||||
|
||||
"github.com/traefik/paerser/parser"
|
||||
)
|
||||
|
||||
func DecodeFlags(flags map[string]string) (config.Providers, error) {
|
||||
filtered := filterFlags(flags)
|
||||
normalized := NormalizeKeys(filtered, "tinyauth", "-")
|
||||
var providers config.Providers
|
||||
|
||||
err := parser.Decode(normalized, &providers, "tinyauth", "tinyauth.providers")
|
||||
|
||||
if err != nil {
|
||||
return config.Providers{}, err
|
||||
}
|
||||
|
||||
return providers, nil
|
||||
}
|
||||
|
||||
func filterFlags(flags map[string]string) map[string]string {
|
||||
filtered := make(map[string]string)
|
||||
for k, v := range flags {
|
||||
filtered[strings.TrimPrefix(k, "--")] = v
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
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.OAuthServiceConfig{
|
||||
"client1": {
|
||||
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",
|
||||
Name: "Client1",
|
||||
InsecureSkipVerify: false,
|
||||
},
|
||||
"client2": {
|
||||
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",
|
||||
Name: "My Awesome Client2",
|
||||
InsecureSkipVerify: false,
|
||||
},
|
||||
},
|
||||
}
|
||||
test := map[string]string{
|
||||
"--providers-client1-client-id": "client1-id",
|
||||
"--providers-client1-client-secret": "client1-secret",
|
||||
"--providers-client1-scopes": "client1-scope1,client1-scope2",
|
||||
"--providers-client1-redirect-url": "client1-redirect-url",
|
||||
"--providers-client1-auth-url": "client1-auth-url",
|
||||
"--providers-client1-user-info-url": "client1-user-info-url",
|
||||
"--providers-client1-name": "Client1",
|
||||
"--providers-client1-insecure-skip-verify": "false",
|
||||
"--providers-client2-client-id": "client2-id",
|
||||
"--providers-client2-client-secret": "client2-secret",
|
||||
"--providers-client2-scopes": "client2-scope1,client2-scope2",
|
||||
"--providers-client2-redirect-url": "client2-redirect-url",
|
||||
"--providers-client2-auth-url": "client2-auth-url",
|
||||
"--providers-client2-user-info-url": "client2-user-info-url",
|
||||
"--providers-client2-name": "My Awesome Client2",
|
||||
"--providers-client2-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