github.com/wtfutil/wtf@v0.43.0/modules/hibp/settings.go (about) 1 package hibp 2 3 import ( 4 "os" 5 "time" 6 7 "github.com/olebedev/config" 8 "github.com/wtfutil/wtf/cfg" 9 "github.com/wtfutil/wtf/utils" 10 ) 11 12 const ( 13 defaultFocusable = false 14 defaultTitle = "HIBP" 15 minRefreshInterval = 6 * time.Hour 16 ) 17 18 type colors struct { 19 ok string 20 pwned string 21 } 22 23 // Settings defines the configuration properties for this module 24 type Settings struct { 25 colors 26 *cfg.Common 27 28 accounts []string `help:"A list of the accounts to check the HIBP database for."` 29 apiKey string `help:"Your HIBP API v3 API key"` 30 since string `help:"Only check for breaches after this date. Set this if you’ve been breached in the past, have taken steps to mitigate that (changing passwords, cancelling accounts, etc.) and now only want to know about future breaches." values:"A date string in the format 'yyyy-mm-dd', ie. '2019-06-22'" optional:"true"` 31 } 32 33 // NewSettingsFromYAML creates a new settings instance from a YAML config block 34 func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings { 35 settings := &Settings{ 36 Common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig), 37 38 apiKey: ymlConfig.UString("apiKey", ymlConfig.UString("apikey", os.Getenv("WTF_HIBP_TOKEN"))), 39 accounts: utils.ToStrs(ymlConfig.UList("accounts")), 40 since: ymlConfig.UString("since", ""), 41 } 42 43 cfg.ModuleSecret(name, globalConfig, &settings.apiKey).Load() 44 45 settings.colors.ok = ymlConfig.UString("colors.ok", "white") 46 settings.colors.pwned = ymlConfig.UString("colors.pwned", "red") 47 48 // HIBP data doesn't need to be reloaded very often so to be gentle on this API we 49 // enforce a minimum refresh interval 50 if settings.RefreshInterval < minRefreshInterval { 51 settings.RefreshInterval = minRefreshInterval 52 } 53 54 return settings 55 } 56 57 // HasSince returns TRUE if there's a valid "since" value setting, FALSE if there is not 58 func (sett *Settings) HasSince() bool { 59 if sett.since == "" { 60 return false 61 } 62 63 _, err := sett.SinceDate() 64 return err == nil 65 } 66 67 // SinceDate returns the "since" settings as a proper Time instance 68 func (sett *Settings) SinceDate() (time.Time, error) { 69 dt, err := time.Parse("2006-01-02", sett.since) 70 if err != nil { 71 return time.Now(), err 72 } 73 74 return dt, nil 75 }