From a75d7d556a553f32b5f3c089dc1cbbf44248a00c Mon Sep 17 00:00:00 2001 From: Andrey Rusakov Date: Tue, 15 Oct 2024 10:52:44 +0200 Subject: [PATCH] Add dev mode to the configuration --- example/server/config/config.go | 5 +++++ example/server/config/config_test.go | 7 ++++++- example/server/dynamic/op.go | 2 +- example/server/main.go | 2 +- example/server/storage/client.go | 4 ++-- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/example/server/config/config.go b/example/server/config/config.go index 121eb75e..be52608a 100644 --- a/example/server/config/config.go +++ b/example/server/config/config.go @@ -15,6 +15,7 @@ type Config struct { RedirectURIs []string UsersFile string Issuer string + DevMode bool } // FromEnvVars loads configuration parameters from environment variables. @@ -28,6 +29,7 @@ func FromEnvVars(defaults *Config) *Config { RedirectURIs: defaults.RedirectURIs, UsersFile: defaults.UsersFile, Issuer: defaults.Issuer, + DevMode: defaults.DevMode, } if value, ok := os.LookupEnv("PORT"); ok { cfg.Port = value @@ -41,5 +43,8 @@ func FromEnvVars(defaults *Config) *Config { if value, ok := os.LookupEnv("ISSUER"); ok { cfg.Issuer = value } + if value, ok := os.LookupEnv("DEV_MODE"); ok { + cfg.DevMode = value == "true" + } return cfg } diff --git a/example/server/config/config_test.go b/example/server/config/config_test.go index b8e4acfe..96620605 100644 --- a/example/server/config/config_test.go +++ b/example/server/config/config_test.go @@ -27,12 +27,14 @@ func TestFromEnvVars(t *testing.T) { UsersFile: "/default/user/path", RedirectURIs: []string{"re", "direct", "uris"}, Issuer: "123", + DevMode: true, }, want: &Config{ Port: "6666", UsersFile: "/default/user/path", RedirectURIs: []string{"re", "direct", "uris"}, Issuer: "123", + DevMode: true, }, }, { @@ -42,18 +44,21 @@ func TestFromEnvVars(t *testing.T) { "USERS_FILE": "/path/to/users", "REDIRECT_URIS": "http://redirect/redirect", "ISSUER": "someissuer", + "DEV_MODE": "true", }, defaults: &Config{ Port: "6666", UsersFile: "/default/user/path", RedirectURIs: []string{"re", "direct", "uris"}, Issuer: "someissuer", + DevMode: false, }, want: &Config{ Port: "1234", UsersFile: "/path/to/users", RedirectURIs: []string{"http://redirect/redirect"}, Issuer: "someissuer", + DevMode: true, }, }, { @@ -75,7 +80,7 @@ func TestFromEnvVars(t *testing.T) { } cfg := FromEnvVars(tc.defaults) if fmt.Sprint(cfg) != fmt.Sprint(tc.want) { - t.Errorf("Expected FromEnvVars()=%q, but got %q", tc.want, cfg) + t.Errorf("Expected FromEnvVars()=%v, but got %v", tc.want, cfg) } }) } diff --git a/example/server/dynamic/op.go b/example/server/dynamic/op.go index 432a5754..076e21f7 100644 --- a/example/server/dynamic/op.go +++ b/example/server/dynamic/op.go @@ -28,7 +28,7 @@ var ( func init() { storage.RegisterClients( - storage.NativeClient("native"), + storage.NativeClient("native", false), storage.WebClient("web", "secret"), storage.WebClient("api", "secret"), ) diff --git a/example/server/main.go b/example/server/main.go index 430a5dd9..1f29b2a2 100644 --- a/example/server/main.go +++ b/example/server/main.go @@ -28,7 +28,7 @@ func main() { ) storage.RegisterClients( - storage.NativeClient("native", cfg.RedirectURIs...), + storage.NativeClient("native", cfg.DevMode, cfg.RedirectURIs...), storage.WebClient("web", "secret"), storage.WebClient("api", "secret"), ) diff --git a/example/server/storage/client.go b/example/server/storage/client.go index 010b9ce7..641ae7e6 100644 --- a/example/server/storage/client.go +++ b/example/server/storage/client.go @@ -144,7 +144,7 @@ func RegisterClients(registerClients ...*Client) { // - http://localhost without port specification (e.g. http://localhost/auth/callback) // - custom protocol (e.g. custom://auth/callback) // (the examples will be used as default, if none is provided) -func NativeClient(id string, redirectURIs ...string) *Client { +func NativeClient(id string, devMode bool, redirectURIs ...string) *Client { if len(redirectURIs) == 0 { redirectURIs = []string{ "http://localhost/auth/callback", @@ -161,7 +161,7 @@ func NativeClient(id string, redirectURIs ...string) *Client { responseTypes: []oidc.ResponseType{oidc.ResponseTypeCode}, grantTypes: []oidc.GrantType{oidc.GrantTypeCode, oidc.GrantTypeRefreshToken}, accessTokenType: op.AccessTokenTypeBearer, - devMode: false, + devMode: devMode, idTokenUserinfoClaimsAssertion: false, clockSkew: 0, }