-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
81 lines (65 loc) · 2.14 KB
/
main.go
File metadata and controls
81 lines (65 loc) · 2.14 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
/*
* Copyright (c) 2020-2025 Joe Siltberg
*
* You should have received a copy of the MIT license along with this project.
* If not, see <https://opensource.org/licenses/MIT>.
*/
//nolint:errcheck
package main
import (
"crypto/tls"
"fmt"
"log"
"net/http"
"time"
"github.com/joesiltberg/bowness/fedtls"
"github.com/joesiltberg/bowness/server"
)
// An HTTP handler that illustrates how to get information about the
// authenticated peer from the request context
func myHandler(w http.ResponseWriter, r *http.Request) {
entityID := server.EntityIDFromContext(r.Context())
org := server.OrganizationFromContext(r.Context())
orgID := server.OrganizationIDFromContext(r.Context())
fmt.Fprintf(w, "Hello world %s!\n", entityID)
if org != nil {
fmt.Fprintf(w, "Organization: %s\n", *org)
}
if orgID != nil {
fmt.Fprintf(w, "OrganizationID: %s\n", *orgID)
}
}
func main() {
mdstore := fedtls.NewMetadataStore(
//"https://md.swefed.se/kontosynk/kontosynk-prod-1.jws",
//"jwks",
"https://fedscim-poc.skolfederation.se/md/skolfederation-fedscim-0_1.json",
"jwks.poc",
"metadata-cache.json",
fedtls.DefaultCacheTTL(2*time.Hour),
fedtls.NetworkRetry(1*time.Minute),
fedtls.BadContentRetry(1*time.Hour))
certFile := "cert.pem"
keyFile := "key.pem"
mdTLSConfigManager, err := server.NewMetadataTLSConfigManager(certFile, keyFile, mdstore)
if err != nil {
log.Fatalf("Failed to create TLS configuration: %v", err)
}
srv := &http.Server{
// Wrap the HTTP handler with authentication middleware.
Handler: server.AuthMiddleware(http.HandlerFunc(myHandler), mdstore, nil),
// In order to use the authentication middleware, the server needs
// to have a ConnContext configured so the middleware can access
// connection specific information.
ConnContext: server.ContextModifier(),
}
// Set up a TLS listener with certificate authorities loaded from
// federation metadata (and dynamically updated as metadata gets refreshed).
address := ":8443"
listener, err := tls.Listen("tcp", address, mdTLSConfigManager.Config())
if err != nil {
log.Fatalf("Failed to listen to %s (%v)", address, err)
}
srv.Serve(listener)
mdstore.Quit()
}