-
Notifications
You must be signed in to change notification settings - Fork 473
/
openid_auth.go
144 lines (119 loc) · 4.39 KB
/
openid_auth.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package business
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/log"
)
type OpenIdMetadata struct {
// Taken from https://github.com/coreos/go-oidc/blob/8d771559cf6e5111c9b9159810d0e4538e7cdc82/oidc.go
Issuer string `json:"issuer"`
AuthURL string `json:"authorization_endpoint"`
TokenURL string `json:"token_endpoint"`
JWKSURL string `json:"jwks_uri"`
UserInfoURL string `json:"userinfo_endpoint"`
Algorithms []string `json:"id_token_signing_alg_values_supported"`
// Some extra fields
ScopesSupported []string `json:"scopes_supported"`
ResponseTypesSupported []string `json:"response_types_supported"`
}
var cachedOpenIdMetadata *OpenIdMetadata
// GetConfiguredOpenIdScopes gets the list of scopes set in Kiali configuration making sure
// that the mandatory "openid" scope is present in the returned list.
func GetConfiguredOpenIdScopes() []string {
cfg := config.Get().Auth.OpenId
scopes := cfg.Scopes
isOpenIdScopePresent := false
for _, s := range scopes {
if s == "openid" {
isOpenIdScopePresent = true
break
}
}
if !isOpenIdScopePresent {
scopes = append(scopes, "openid")
}
return scopes
}
// GetOpenIdMetadata fetches the OpenId metadata using the configured Issuer URI and
// downloading the metadata from the well-known path '/.well-known/openid-configuration'. Some
// validations are performed and the parsed metadata is returned. Since the metadata should be
// rare to change, the retrieved metadata is cached on first call and subsequent calls return
// the cached metadata.
func GetOpenIdMetadata() (*OpenIdMetadata, error) {
if cachedOpenIdMetadata != nil {
return cachedOpenIdMetadata, nil
}
cfg := config.Get().Auth.OpenId
// Remove trailing slash from issuer URI, if needed
trimmedIssuerUri := strings.TrimRight(cfg.IssuerUri, "/")
// Create HTTP client
httpTransport := &http.Transport{}
if cfg.InsecureSkipVerifyTLS {
httpTransport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
httpClient := http.Client{
Timeout: time.Second * 10,
Transport: httpTransport,
}
// Fetch IdP metadata
response, err := httpClient.Get(trimmedIssuerUri + "/.well-known/openid-configuration")
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return nil, fmt.Errorf("cannot fetch OpenId Metadata (HTTP response status = %s)", response.Status)
}
// Parse JSON document
var metadata OpenIdMetadata
rawMetadata, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("failed to read OpenId Metadata: %s", err.Error())
}
err = json.Unmarshal(rawMetadata, &metadata)
if err != nil {
return nil, fmt.Errorf("cannot parse OpenId Metadata: %s", err.Error())
}
// Validate issuer == issuerUri
if metadata.Issuer != cfg.IssuerUri {
return nil, fmt.Errorf("mismatch between the configured issuer_uri (%s) and the exposed Issuer URI in OpenId provider metadata (%s)", cfg.IssuerUri, metadata.Issuer)
}
// Validate there is an authorization endpoint
if len(metadata.AuthURL) == 0 {
return nil, errors.New("the OpenID provider does not expose an authorization endpoint")
}
// Log warning if OpenId provider Metadata does not expose "id_token" in it's supported response types.
// It's possible to try authentication. If metadata is right, the error will be evident to the user when trying to login.
responseTypes := strings.Join(metadata.ResponseTypesSupported, " ")
if !strings.Contains(responseTypes, "id_token") {
log.Warning("Configured OpenID provider informs response_type=id_token is unsupported. Users may not be able to login.")
}
// Log warning if OpenId provider informs that some of the configured scopes are not supported
// It's possible to try authentication. If metadata is right, the error will be evident to the user when trying to login.
scopes := GetConfiguredOpenIdScopes()
for _, scope := range scopes {
isScopeSupported := false
for _, supportedScope := range metadata.ScopesSupported {
if scope == supportedScope {
isScopeSupported = true
break
}
}
if !isScopeSupported {
log.Warning("Configured OpenID provider informs some of the configured scopes are unsupported. Users may not be able to login.")
break
}
}
// Return parsed metadata
cachedOpenIdMetadata = &metadata
return cachedOpenIdMetadata, nil
}