-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
66 lines (54 loc) · 1.35 KB
/
server.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
package main
import (
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"github.com/lestrrat-go/jwx/v2/jwk"
)
const authEndpoint = "http://imaginary-server.test/fake-auth-route"
var srv *server
type discoveryDoc struct {
JWKsURI string `json:"jwks_uri"`
AuthorizationEndpoint string `json:"authorization_endpoint"`
}
type server struct {
origin string
numConfigRequests int
numKeysRequests int
}
func initServer(keys jwk.Set) {
var origin string
http.HandleFunc("/.well-known/openid-configuration", func(rw http.ResponseWriter, r *http.Request) {
srv.numConfigRequests++
body, err := json.Marshal(&discoveryDoc{
JWKsURI: fmt.Sprintf("%s/test-keys", origin),
AuthorizationEndpoint: authEndpoint,
})
if err != nil {
log.Fatal("json.Marshal error:", err)
}
rw.Write(body)
})
http.HandleFunc("/test-keys", func(rw http.ResponseWriter, r *http.Request) {
srv.numKeysRequests++
body, err := json.Marshal(keys)
if err != nil {
log.Fatal("json.Marshal error:", err)
}
rw.Write(body)
})
listener, err := net.Listen("tcp", "[::1]:0")
if err != nil {
log.Fatal("net.Listen error:", err)
}
go func() {
log.Fatal(http.Serve(listener, nil))
}()
origin = fmt.Sprintf("http://%s", listener.Addr().String())
log.Print("started test server: ", origin)
srv = &server{
origin: origin,
}
}