/
options.go
55 lines (44 loc) · 1.48 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package auth
import (
"os"
"path"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/pflag"
)
type Options struct {
ClientId string `mapstructure:"client-id"`
TokenFile string `mapstructure:"token-file"`
LocalAddr string `mapstructure:"local-addr"`
AuthorizationServerURL string `mapstructure:"server-url"`
InsecureClient bool `mapstructure:"insecure-client"`
RedirectURL string
}
func NewOptions() *Options {
return &Options{}
}
func (o *Options) AddFlags(fs *pflag.FlagSet, prefix string) {
if prefix != "" {
prefix = prefix + "."
}
fs.String(prefix+"client-id", "todo-app", "the clientId issued by the authorization server that represents the application")
fs.StringP(prefix+"token-file", "t", "$HOME/.config/doit/oidc-token", "the path to the file that holds the id-token")
fs.String(prefix+"local-addr", "localhost:8080", "the local address that starts the OAuth2 flow")
fs.String(prefix+"server-url", "https://localhost/realms/todoapp", "the URL to the authorization server")
fs.BoolP(prefix+"insecure-client", "k", false, "validate authorization server certs?")
}
func (o *Options) Validate() []error {
return nil
}
func (o *Options) Complete() error {
if o.TokenFile == "" {
home, err := homedir.Dir()
if err != nil {
return err
}
o.TokenFile = path.Join(home, ".config", "doit", "oidc-token")
} else {
o.TokenFile = os.ExpandEnv(o.TokenFile)
}
o.RedirectURL = "http://" + o.LocalAddr
return nil
}