-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_types.go
69 lines (55 loc) · 1.73 KB
/
config_types.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
69
package clitools
import (
"context"
"errors"
"fmt"
"net/http"
"time"
)
type appConfiguration[T any] struct {
Environments map[string]*OIDCEnvironment `json:"environments"`
Configuration T `json:"configuration"`
}
type OIDCEnvironment struct {
Refreshed time.Time `json:"refreshed,omitempty,omitzero"`
OIDCConfigURL string `json:"oidc_config_url,omitempty"`
OIDCConfig *OIDCConfig `json:"oidc_config"`
}
func (ce *OIDCEnvironment) EnsureOIDCConfig(
ctx context.Context, client *http.Client, maxAge time.Duration,
) (outErr error) {
if ce.OIDCConfig != nil && (ce.OIDCConfigURL == "" || time.Since(ce.Refreshed) < maxAge) {
return nil
}
if ce.OIDCConfigURL == "" {
return errors.New("no OIDC config or URL has been set")
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ce.OIDCConfigURL, nil)
if err != nil {
return fmt.Errorf("create OIDC config request: %w", err)
}
res, err := client.Do(req)
if err != nil {
return fmt.Errorf("fetch OIDC config: %w", err)
}
defer safeClose(res.Body, "OIDC config response", &outErr)
if res.StatusCode != http.StatusOK {
return fmt.Errorf("error response: %s", res.Status)
}
var conf OIDCConfig
err = unmarshalReader(res.Body, &conf)
if err != nil {
return fmt.Errorf("parse OIDC config: %w", err)
}
ce.Refreshed = time.Now()
ce.OIDCConfig = &conf
return nil
}
type OIDCConfig struct {
Issuer string `json:"issuer"`
AuthorizationEndpoint string `json:"authorization_endpoint"`
TokenEndpoint string `json:"token_endpoint"`
IntrospectionEndpoint string `json:"introspection_endpoint"`
UserinfoEndpoint string `json:"userinfo_endpoint"`
EndSessionEndpoint string `json:"end_session_endpoint"`
}