forked from temporalio/samples-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oidc.go
62 lines (49 loc) · 1.29 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
package main
import (
"context"
"encoding/json"
"net/http"
"strings"
"go.temporal.io/server/common/authorization"
"go.temporal.io/server/common/config"
"google.golang.org/grpc"
)
type Provider struct {
Issuer string `json:"issuer"`
JWKSURI string `json:"jwks_uri,omitempty"`
audience string
mapper authorization.ClaimMapper
}
func (a *Provider) Audience(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo) string {
if a == nil {
return ""
}
return a.audience
}
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)
}