-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
109 lines (88 loc) · 2.88 KB
/
main.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
// Copyright (c) 2019 Faye Amacker. All rights reserved.
// Use of this source code is governed by Apache License 2.0 found in the LICENSE file.
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
_ "github.com/fxamacker/webauthn/androidkeystore"
_ "github.com/fxamacker/webauthn/androidsafetynet"
_ "github.com/fxamacker/webauthn/fidou2f"
_ "github.com/fxamacker/webauthn/packed"
_ "github.com/fxamacker/webauthn/tpm"
_ "github.com/lib/pq"
)
type contextKey string
const (
sessionNameLoginSession string = "LoginSession" // session store name for login session
sessionMapKeyUserSession string = "UserSession" // session map key for *userSession
sessionMapKeyWebAuthnCreationOptions string = "WebAuthnCreationOptions" // session map key for *webauthn.PublicKeyCredentialCreationOptions
sessionMapKeyWebAuthnRequestOptions string = "WebAuthnRequestOptions" // session map key for *webauthn.PublicKeyCredentialRequestOptions
contextKeyLoginSession contextKey = contextKey(sessionNameLoginSession) // context key for login session
)
func main() {
var serverAddr, configFilePath, certFilePath, keyFilePath string
flag.StringVar(&serverAddr, "addr", "", "web server address")
flag.StringVar(&configFilePath, "config", "", "config file path")
flag.StringVar(&certFilePath, "cert", "", "cert file path")
flag.StringVar(&keyFilePath, "key", "", "key file path")
flag.Parse()
if serverAddr == "" || configFilePath == "" || certFilePath == "" || keyFilePath == "" {
flag.Usage()
return
}
configFile, err := os.Open(configFilePath)
if err != nil {
fmt.Println("Failed to open config file: " + err.Error())
flag.Usage()
return
}
if _, err := os.Stat(certFilePath); os.IsNotExist(err) {
fmt.Println("Cert file " + certFilePath + " doesn't exist.")
flag.Usage()
return
}
if _, err := os.Stat(keyFilePath); os.IsNotExist(err) {
fmt.Println("Key file " + keyFilePath + " doesn't exist.")
flag.Usage()
return
}
c, err := newConfig(configFile)
if err != nil {
panic(err)
}
s, err := newServer(c)
if err != nil {
panic(err)
}
defer s.close()
s.routes()
server := &http.Server{
Addr: serverAddr,
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: s.router,
}
go func() {
if err := server.ListenAndServeTLS(certFilePath, keyFilePath); err != http.ErrServerClosed {
// Error starting or closing listener
log.Printf("HTTP server ListenAndServeTLS: %v\n", err)
}
}()
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
// Error from closing listeners, or context timeout
log.Printf("HTTP server Shutdown: %v\n", err)
}
}