forked from oauth2-proxy/mockoidc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
encryption.go
187 lines (162 loc) · 5.32 KB
/
encryption.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package mockoidc
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/golang-jwt/jwt"
"gopkg.in/square/go-jose.v2"
)
const (
CodeChallengeMethodPlain = "plain"
CodeChallengeMethodS256 = "S256"
)
const DefaultKey = `MIIEowIBAAKCAQEAtI1Jf2zmfwLzpAjVarORtjKtmCHQtgNxqWDdVNVa` +
`gCb092tLrBRv0fTfHIJG-YpmmTrRN5yKax9bI3oSYNZJufAN3gu4TIrlLoFv6npC-k3rK-s` +
`biD2m0iz9duxe7uVSEHCJlcMas86Wa-VGBlAZQpnqh2TlaHXhyVbm-gHFGU0u26Pgv5Esw2` +
`DEwRh0l7nK1ygg8dL_NNdtnaxTYhWAVPo4Vqcl2a9n-bs65maK02IgBLpaLRUtjfjSIV17Y` +
`Bzlr6ekr7GwkDTD79d3Uc2GSSGzWqKlFtXmM9cFkfGGOYcaQLoELbkxaGfLmKI53HIxXUK2` +
`8JjVCxITGl60u_Z5bQIDAQABAoIBADzUXS7RQdcI540cbMrGNRFtgY7_1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GpoQKBgDMXVHVXNCJWO13_rwakBe4a9W_lbKuVX27wgCBcu3i_lGYjggm8GPkaWk14` +
`b-reOmP3tZyZxDyX2zFyjkJpu2SWd5TlAL59vP3dzx-uyj6boWCCZHxzepli5eHXOeVW-S-` +
`gwlCAF0U0n_XJ7Qhv0_SQnxSqT-D6V1-KbbeXnO7w`
// Keypair is an RSA Keypair & JWT KeyID used for OIDC Token signing
type Keypair struct {
PrivateKey *rsa.PrivateKey
PublicKey *rsa.PublicKey
Kid string
}
// NewKeypair makes a Keypair off the provided rsa.PrivateKey or returns
// the package default if nil was passed
func NewKeypair(key *rsa.PrivateKey) (*Keypair, error) {
if key == nil {
return DefaultKeypair()
}
return &Keypair{
PrivateKey: key,
PublicKey: &key.PublicKey,
}, nil
}
// RandomKeypair creates a random rsa.PrivateKey and generates a key pair.
// This can be compute intensive, and should be avoided if called many
// times in a test suite.
func RandomKeypair(size int) (*Keypair, error) {
key, err := rsa.GenerateKey(rand.Reader, size)
if err != nil {
return nil, err
}
return &Keypair{
PrivateKey: key,
PublicKey: &key.PublicKey,
}, nil
}
// Returns the default Keypair built from DefaultKey
func DefaultKeypair() (*Keypair, error) {
keyBytes, err := base64.RawURLEncoding.DecodeString(DefaultKey)
if err != nil {
return nil, err
}
key, err := x509.ParsePKCS1PrivateKey(keyBytes)
if err != nil {
return nil, err
}
return &Keypair{
PrivateKey: key,
PublicKey: &key.PublicKey,
}, nil
}
// If not manually set, computes the JWT headers' `kid`
func (k *Keypair) KeyID() (string, error) {
if k.Kid != "" {
return k.Kid, nil
}
publicKeyDERBytes, err := x509.MarshalPKIXPublicKey(k.PublicKey)
if err != nil {
return "", err
}
hasher := crypto.SHA256.New()
if _, err := hasher.Write(publicKeyDERBytes); err != nil {
return "", err
}
publicKeyDERHash := hasher.Sum(nil)
k.Kid = base64.RawURLEncoding.EncodeToString(publicKeyDERHash)
return k.Kid, nil
}
// JWKS is the JSON JWKS representation of the rsa.PublicKey
func (k *Keypair) JWKS() ([]byte, error) {
kid, err := k.KeyID()
if err != nil {
return nil, err
}
jwk := jose.JSONWebKey{
Use: "sig",
Algorithm: string(jose.RS256),
Key: k.PublicKey,
KeyID: kid,
}
jwks := &jose.JSONWebKeySet{
Keys: []jose.JSONWebKey{jwk},
}
return json.Marshal(jwks)
}
// SignJWT signs jwt.Claims with the Keypair and returns a token string
func (k *Keypair) SignJWT(claims jwt.Claims) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
kid, err := k.KeyID()
if err != nil {
return "", err
}
token.Header["kid"] = kid
return token.SignedString(k.PrivateKey)
}
// VerifyJWT verifies the signature of a token was signed with this Keypair
func (k *Keypair) VerifyJWT(token string) (*jwt.Token, error) {
return jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
kid, err := k.KeyID()
if err != nil {
return nil, err
}
if tk, ok := token.Header["kid"]; ok && tk == kid {
return k.PublicKey, nil
}
return nil, errors.New("token kid does not match or is not present")
})
}
func randomNonce(length int) (string, error) {
b := make([]byte, length)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(b), nil
}
func GenerateCodeChallenge(method, codeVerifier string) (string, error) {
switch method {
case CodeChallengeMethodPlain:
return codeVerifier, nil
case CodeChallengeMethodS256:
shaSum := sha256.Sum256([]byte(codeVerifier))
return base64.RawURLEncoding.EncodeToString(shaSum[:]), nil
default:
return "", fmt.Errorf("unknown challenge method: %v", method)
}
}