-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
68 lines (56 loc) · 1.75 KB
/
config.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
56
57
58
59
60
61
62
63
64
65
66
67
68
package config
import (
"fmt"
"os"
"path"
"strings"
"golang.org/x/oauth2"
"golang.org/x/oauth2/microsoft"
"gopkg.in/yaml.v3"
)
// Config is the configuration for the client OAUTH2 system
type Config struct {
Username string `yaml:"username"`
TenantID string `yaml:"tenantID"`
ClientID string `yaml:"clientID"`
ClientSecret string `yaml:"clientSecret"`
Scopes []string `yaml:"scopes"`
Redirect *RedirectConfig `yaml:"redirect"`
}
// RedirectConfig describes the OAUTH2 delegationr redirect setup (from client config on Microsoft)
type RedirectConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Path string `yaml:"path"`
}
// URL returns the RedirectURL.
func (rc *RedirectConfig) URL() string {
return fmt.Sprintf("http://%s:%d/%s", rc.Host, rc.Port, strings.Trim(rc.Path, "/"))
}
// OAuth2 returns the OAuth2 config from this configuration.
func (cfg *Config) OAuth2() *oauth2.Config {
return &oauth2.Config{
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
Scopes: cfg.Scopes,
RedirectURL: cfg.Redirect.URL(),
Endpoint: microsoft.AzureADEndpoint(cfg.TenantID),
}
}
// LoadConfig loads the configuration from the default configuration file.
func LoadConfig() (*Config, error) {
cfgDir, err := os.UserConfigDir()
if err != nil {
return nil, fmt.Errorf("failed to determind configuration directory")
}
fn := path.Join(cfgDir, "azure", "config.yaml")
f, err := os.Open(fn)
if err != nil {
return nil, fmt.Errorf("failed to open configuration file %q: %w", fn, err)
}
cfg := new(Config)
if err := yaml.NewDecoder(f).Decode(cfg); err != nil {
return nil, fmt.Errorf("failed to parse configuration file %q: %w", fn, err)
}
return cfg, nil
}