-
Notifications
You must be signed in to change notification settings - Fork 0
/
oidc.go
88 lines (71 loc) · 1.91 KB
/
oidc.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
package main
import (
"encoding/json"
"net/http"
"strings"
"go.temporal.io/server/common/authorization"
"go.temporal.io/server/common/config"
"go.temporal.io/server/common/log/tag"
)
type Provider struct {
Issuer string `json:"issuer"`
JWKSURI string `json:"jwks_uri,omitempty"`
Audience string
mapper authorization.ClaimMapper
}
func (p *Provider) Authorize(namespace string, r *http.Request) bool {
token := r.Header.Get("Authorization")
if token == "" {
logger.Warn("Authorization header not set")
return false
}
authInfo := authorization.AuthInfo{
AuthToken: token,
Audience: p.Audience,
}
claims, err := p.mapper.GetClaims(&authInfo)
if err != nil {
logger.Warn("unable to parse claims", tag.NewErrorTag(err))
return false
}
// If they have no role in this namespace they will get RoleUndefined
role := claims.Namespaces[namespace]
switch {
case strings.HasSuffix(r.URL.Path, "/decode"):
if role >= authorization.RoleReader {
return true
}
case strings.HasSuffix(r.URL.Path, "/encode"):
if role >= authorization.RoleWriter {
return true
}
}
return false
}
func newProvider(providerURL string) (*Provider, error) {
var provider Provider
res, err := http.Get(strings.TrimSuffix(providerURL, "/") + "/.well-known/openid-configuration")
if err != nil {
return nil, err
}
defer func() { _ = res.Body.Close() }()
err = json.NewDecoder(res.Body).Decode(&provider)
if err != nil {
return nil, err
}
provider.mapper = newClaimMapper(provider.JWKSURI)
return &provider, nil
}
func newClaimMapper(providerKeysURL string) authorization.ClaimMapper {
authConfig := config.Authorization{
JWTKeyProvider: config.JWTKeyProvider{
KeySourceURIs: []string{providerKeysURL},
},
ClaimMapper: "default",
}
provider := authorization.NewDefaultTokenKeyProvider(
&authConfig,
logger,
)
return authorization.NewDefaultJWTClaimMapper(provider, &authConfig, logger)
}