diff --git a/app/settings/api-keys.go b/app/settings/api-keys.go new file mode 100644 index 0000000..f3c9f83 --- /dev/null +++ b/app/settings/api-keys.go @@ -0,0 +1,20 @@ +// Copyright 2023 wanderer +// SPDX-License-Identifier: AGPL-3.0-only + +package settings + +import ( + "os" +) + +func (s *Settings) setAPIKeys() { + if hibpK := os.Getenv("PCMT_HIBP_API_KEY"); hibpK != "" { + log.Info("setting HIBP API key from env var") + s.SetAPIKeyHIBP(hibpK) + } + + if dehashedK := os.Getenv("PCMT_DEHASHED_API_KEY"); dehashedK != "" { + log.Info("setting dehashed.com API key from env var") + s.SetAPIKeyDehashed(dehashedK) + } +} diff --git a/app/settings/settings.go b/app/settings/settings.go index c3f2a30..5b77c8b 100644 --- a/app/settings/settings.go +++ b/app/settings/settings.go @@ -40,6 +40,8 @@ type Settings struct { dbType string dbIsSetUp bool RegistrationAllowed bool + hibpAPIKey string + dehashedAPIKey string } const ( @@ -52,6 +54,8 @@ const ( defaultLoggerSkipAssets = true ) +var log slogging.Slogger + // cleantgt is a list of ENV vars pertaining to pcmt. var cleantgt = []string{ "PCMT_LIVE", @@ -62,6 +66,7 @@ var cleantgt = []string{ "PCMT_SESSION_ENCR_SECRET", "PCMT_INIT_ADMIN_PASSWORD", "PCMT_HIBP_API_KEY", + "PCMT_DEHASHED_API_KEY", } // New returns a new instance of the settings struct. @@ -89,8 +94,8 @@ func (s *Settings) DefaultLoggerSkipAssets() bool { // the settings struct. Order of preference for values is (from higher to // lower) as follows: flag -> Env var -> configuration file. func (s *Settings) Consolidate(conf *config.Config, host *string, port *int, devel *bool, version string) { - log := *slogging.Logger() // have a local copy. - log.Logger = log.Logger.With( + log = *slogging.Logger() // have a local copy. + log.Logger = log.With( slog.Group("pcmt extra", slog.String("module", "app/settings")), ) @@ -174,6 +179,8 @@ func (s *Settings) Consolidate(conf *config.Config, host *string, port *int, dev s.RegistrationAllowed = true } + s.setAPIKeys() + s.SetVersion(version) } @@ -302,6 +309,16 @@ func (s *Settings) DbIsSetUp() bool { return s.dbIsSetUp } +// APIKeyHIBP returns the hibpAPIKey. +func (s *Settings) APIKeyHIBP() string { + return s.hibpAPIKey +} + +// APIKeyDehashed returns the dehashedAPIKey. +func (s *Settings) APIKeyDehashed() string { + return s.dehashedAPIKey +} + // DbConnstring returns the dbConnString. func (s *Settings) DbConnstring() string { return s.dbConnstring @@ -441,6 +458,16 @@ func (s *Settings) SetDbIsSetUp(is bool) { s.dbIsSetUp = is } +// SetAPIKeyHIBP sets the hibpAPIKey. +func (s *Settings) SetAPIKeyHIBP(k string) { + s.hibpAPIKey = k +} + +// SetAPIKeyDehashed sets the dehashedAPIKey. +func (s *Settings) SetAPIKeyDehashed(k string) { + s.dehashedAPIKey = k +} + // EraseENVs attempts to clear environment vars pertaining to pcmt. func (s *Settings) EraseENVs() error { for _, v := range cleantgt { diff --git a/handlers/manage-apikeys.go b/handlers/manage-apikeys.go index 1d6855a..406b28d 100644 --- a/handlers/manage-apikeys.go +++ b/handlers/manage-apikeys.go @@ -19,6 +19,14 @@ func ManageAPIKeys() echo.HandlerFunc { p.Current = "api-keys" p.User = u + if setting.APIKeyHIBP() != "" { + p.Data["hibpApiKey"] = setting.APIKeyHIBP() + } + + if setting.APIKeyDehashed() != "" { + p.Data["dehashedApiKey"] = setting.APIKeyDehashed() + } + err := c.Render(http.StatusOK, "manage/apikeys.tmpl", p, ) diff --git a/templates/manage/apikeys.tmpl b/templates/manage/apikeys.tmpl index e6950cc..d51a01d 100644 --- a/templates/manage/apikeys.tmpl +++ b/templates/manage/apikeys.tmpl @@ -14,8 +14,9 @@ Manage Your API keys -
+ {{- $hibpK := .Data.hibpApiKey -}} +
@@ -23,21 +24,42 @@ Have I Been Pwned? -

Adds support for querying Troy Hunt's Have I Been Pwned? API.

+ disabled + class="py-3 px-8 mt-2 w-full valid:border text-gray-500 bg-white border rounded-lg dark:bg-gray-900 dark:text-gray-300 dark:valid:border-gray-600 focus:border-blue-400 dark:focus:border-blue-300 focus:ring-blue-300 focus:outline-none focus:ring focus:ring-opacity-40"> +

+ Adds support for querying Troy Hunt's + Have I Been Pwned? API + + +

-
- -

+ + {{- $dehashedK := .Data.dehashedApiKey -}}
@@ -46,16 +68,31 @@ DeHashed.com -

Adds support for querying DeHashed API.

+ disabled + class="py-3 px-8 mt-2 w-full valid:border text-gray-500 bg-white border rounded-lg dark:bg-gray-900 dark:text-gray-300 dark:valid:border-gray-600 focus:border-blue-400 dark:focus:border-blue-300 focus:ring-blue-300 focus:outline-none focus:ring focus:ring-opacity-40" + aria-described-by="dehashed-expl"> +

Adds support for querying DeHashed API + +

-
- -