forked from tstranex/u2f
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
196 lines (156 loc) · 4.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
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
188
189
190
191
192
193
194
195
196
// FIDO U2F Go Library
// Copyright 2015 The FIDO U2F Go Library Authors. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package main
import (
"crypto/tls"
"encoding/json"
"log"
"net/http"
"github.com/ryankurte/go-u2f"
)
const appID = "https://localhost:3483"
var trustedFacets = []string{appID}
// Normally these state variables would be stored in a database.
// For the purposes of the demo, we just store them in memory.
var challenge *u2f.Challenge
var registrations []u2f.Registration
func registerRequest(w http.ResponseWriter, r *http.Request) {
c, err := u2f.NewChallenge(appID, trustedFacets, registrations)
if err != nil {
log.Printf("u2f.NewChallenge error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
challenge = c
req := c.RegisterRequest()
log.Printf("registerRequest: %+v", req)
json.NewEncoder(w).Encode(req)
}
func registerResponse(w http.ResponseWriter, r *http.Request) {
var regResp u2f.RegisterResponse
if err := json.NewDecoder(r.Body).Decode(®Resp); err != nil {
http.Error(w, "invalid response: "+err.Error(), http.StatusBadRequest)
return
}
if challenge == nil {
http.Error(w, "challenge not found", http.StatusBadRequest)
return
}
reg, err := challenge.Register(regResp, &u2f.RegistrationConfig{SkipAttestationVerify: true})
if err != nil {
log.Printf("u2f.Register error: %v", err)
http.Error(w, "error verifying response", http.StatusInternalServerError)
return
}
registrations = append(registrations, *reg)
log.Printf("Registration success: %+v", reg)
w.Write([]byte("success"))
}
func signRequest(w http.ResponseWriter, r *http.Request) {
if registrations == nil {
http.Error(w, "registrations missing", http.StatusBadRequest)
return
}
c, err := u2f.NewChallenge(appID, trustedFacets, registrations)
if err != nil {
log.Printf("u2f.NewChallenge error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
challenge = c
req := c.SignRequest()
log.Printf("authenticateRequest: %+v", req)
json.NewEncoder(w).Encode(req)
}
func signResponse(w http.ResponseWriter, r *http.Request) {
var signResp u2f.SignResponse
if err := json.NewDecoder(r.Body).Decode(&signResp); err != nil {
http.Error(w, "invalid response: "+err.Error(), http.StatusBadRequest)
return
}
log.Printf("signResponse: %+v", signResp)
if challenge == nil {
http.Error(w, "challenge missing", http.StatusBadRequest)
return
}
if registrations == nil {
http.Error(w, "registrations missing", http.StatusBadRequest)
return
}
reg, err := challenge.Authenticate(signResp)
if err == nil {
log.Printf("newCounter: %d", reg.Counter)
w.Write([]byte("success"))
return
}
log.Printf("VerifySignResponse error: %v", err)
http.Error(w, "error verifying response", http.StatusInternalServerError)
}
const indexHTML = `
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<!-- The original u2f-api.js code can be found here:
https://github.com/google/u2f-ref-code/blob/master/u2f-gae-demo/war/js/u2f-api.js -->
<script type="text/javascript" src="https://demo.yubico.com/js/u2f-api.js"></script>
</head>
<body>
<h1>FIDO U2F Go Library Demo</h1>
<ul>
<li><a href="javascript:register();">Register token</a></li>
<li><a href="javascript:sign();">Authenticate</a></li>
</ul>
<script>
function u2fRegistered(resp) {
console.log(resp);
$.post('/registerResponse', JSON.stringify(resp)).done(function() {
alert('Success');
});
}
function register() {
$.getJSON('/registerRequest').done(function(req) {
console.log("Registration request:")
console.log(req);
u2f.register(req.appId, req.registerRequests, req.registeredKeys, u2fRegistered, 60);
});
}
function u2fSigned(resp) {
console.log(resp);
console.log("Sign response:")
$.post('/signResponse', JSON.stringify(resp)).done(function() {
alert('Success');
});
}
function sign() {
$.getJSON('/signRequest').done(function(req) {
console.log("Sign request:")
console.log(req);
u2f.sign(req.appId, req.challenge, req.registeredKeys, u2fSigned, 60);
});
}
</script>
</body>
</html>
`
func indexHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(indexHTML))
}
func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/registerRequest", registerRequest)
http.HandleFunc("/registerResponse", registerResponse)
http.HandleFunc("/signRequest", signRequest)
http.HandleFunc("/signResponse", signResponse)
certs, err := tls.X509KeyPair([]byte(tlsCert), []byte(tlsKey))
if err != nil {
log.Fatal(err)
}
log.Printf("Running on %s", appID)
var s http.Server
s.Addr = ":3483"
s.TLSConfig = &tls.Config{Certificates: []tls.Certificate{certs}}
log.Fatal(s.ListenAndServeTLS("", ""))
}