-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathoidc_server_test.go
More file actions
156 lines (133 loc) · 3.93 KB
/
Copy pathoidc_server_test.go
File metadata and controls
156 lines (133 loc) · 3.93 KB
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package oidcauthextension
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha1" // #nosec
"crypto/sha256"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"math/big"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// oidcServer is an overly simplified OIDC mock server, good enough to sign the tokens required by the test
// and pass the verification done by the underlying libraries
type oidcServer struct {
*httptest.Server
x509Cert []byte
privateKey *rsa.PrivateKey
}
func newOIDCServer() (*oidcServer, error) {
jwks := map[string]any{}
mux := http.NewServeMux()
server := httptest.NewUnstartedServer(mux)
mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err := json.NewEncoder(w).Encode(map[string]any{
"issuer": server.URL,
"jwks_uri": fmt.Sprintf("%s/.well-known/jwks.json", server.URL),
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
})
mux.HandleFunc("/.well-known/jwks.json", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err := json.NewEncoder(w).Encode(jwks); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
})
privateKey, err := createPrivateKey()
if err != nil {
return nil, err
}
x509Cert, err := createCertificate(privateKey)
if err != nil {
return nil, err
}
eBytes := make([]byte, 8)
binary.BigEndian.PutUint64(eBytes, uint64(privateKey.E))
eBytes = bytes.TrimLeft(eBytes, "\x00")
// #nosec
sum := sha1.Sum(x509Cert)
jwks["keys"] = []map[string]any{{
"alg": "RS256",
"kty": "RSA",
"use": "sig",
"x5c": []string{base64.StdEncoding.EncodeToString(x509Cert)},
"n": base64.RawURLEncoding.EncodeToString(privateKey.N.Bytes()),
"e": base64.RawURLEncoding.EncodeToString(eBytes),
"kid": base64.RawURLEncoding.EncodeToString(sum[:]),
"x5t": base64.RawURLEncoding.EncodeToString(sum[:]),
}}
return &oidcServer{server, x509Cert, privateKey}, nil
}
func newReverseProxy(t *testing.T, dst string) *httptest.Server {
remote, err := url.Parse(dst)
require.NoError(t, err)
handler := func(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
r.Host = remote.Host
p.ServeHTTP(w, r)
}
}
proxy := httputil.NewSingleHostReverseProxy(remote)
server := httptest.NewServer(http.HandlerFunc(handler(proxy)))
return server
}
func (s *oidcServer) token(jsonPayload []byte) (string, error) {
jsonHeader, err := json.Marshal(map[string]any{
"alg": "RS256",
"typ": "JWT",
})
if err != nil {
return "", err
}
header := base64.RawURLEncoding.EncodeToString(jsonHeader)
payload := base64.RawURLEncoding.EncodeToString(jsonPayload)
digest := sha256.Sum256(fmt.Appendf(nil, "%s.%s", header, payload))
signature, err := rsa.SignPKCS1v15(rand.Reader, s.privateKey, crypto.SHA256, digest[:])
if err != nil {
return "", err
}
encodedSignature := base64.RawURLEncoding.EncodeToString(signature)
token := fmt.Sprintf("%s.%s.%s", header, payload, encodedSignature)
return token, nil
}
func createCertificate(privateKey *rsa.PrivateKey) ([]byte, error) {
cert := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
Organization: []string{"Ecorp, Inc"},
},
NotBefore: time.Now(),
NotAfter: time.Now().Add(5 * time.Minute),
}
x509Cert, err := x509.CreateCertificate(rand.Reader, &cert, &cert, &privateKey.PublicKey, privateKey)
if err != nil {
return nil, err
}
return x509Cert, nil
}
func createPrivateKey() (*rsa.PrivateKey, error) {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, err
}
return priv, nil
}