-
Notifications
You must be signed in to change notification settings - Fork 1
/
oidc.go
72 lines (57 loc) · 1.72 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
package main
import (
"context"
"crypto"
"crypto/rsa"
"fmt"
"net/http"
"github.com/awnumar/memguard"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jws"
"github.com/openpubkey/openpubkey/client"
oidcclient "github.com/zitadel/oidc/v2/pkg/client"
)
type oidcPiperOP struct {
OIDCIssuer string
}
var _ client.OpenIdProvider = (*oidcPiperOP)(nil)
func (a *oidcPiperOP) RequestTokens(ctx context.Context, cicHash string) (*memguard.LockedBuffer, error) {
return nil, fmt.Errorf("not implemented")
}
func (a *oidcPiperOP) VerifyCICHash(ctx context.Context, idt []byte, expectedCICHash string) error {
cicHash, err := client.ExtractClaim(idt, "nonce")
if err != nil {
return err
}
if cicHash != expectedCICHash {
return fmt.Errorf("nonce claim doesn't match, got %q, expected %q", cicHash, expectedCICHash)
}
return nil
}
func (a *oidcPiperOP) Issuer() string {
return a.OIDCIssuer
}
func (a *oidcPiperOP) PublicKey(ctx context.Context, headers jws.Headers) (crypto.PublicKey, error) {
discConf, err := oidcclient.Discover(a.Issuer(), http.DefaultClient)
if err != nil {
return nil, fmt.Errorf("failed to call OIDC discovery endpoint: %w", err)
}
jwks, err := jwk.Fetch(ctx, discConf.JwksURI)
if err != nil {
return nil, fmt.Errorf("failed to fetch to JWKS: %w", err)
}
kid := headers.KeyID()
key, ok := jwks.LookupKeyID(kid)
if !ok {
return nil, fmt.Errorf("key %q isn't in JWKS", kid)
}
pubKey := new(rsa.PublicKey)
err = key.Raw(pubKey)
if err != nil {
return nil, fmt.Errorf("failed to decode public key: %w", err)
}
return pubKey, err
}
func (a *oidcPiperOP) VerifyNonGQSig(ctx context.Context, idt []byte, expectedNonce string) error {
return fmt.Errorf("not implemented")
}