-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (54 loc) · 1.27 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
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/pquerna/otp/totp"
)
func main() {
fmt.Println("Starting server on port :8787 ...")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/totp/" {
http.Redirect(w, r, "/totp", http.StatusFound)
return
} else if r.URL.Path != "/" {
http.Error(w, "Not found", http.StatusNotFound)
return
}
if r.Method == "GET" {
html := `
<!DOCTYPE html>
<html>
<head>
<title>OTP Generator</title>
</head>
<body>
<form action="/totp" method="GET">
<label for="secret">Secret:</label>
<input type="password" id="secret" name="secret">
<input type="submit" value="Generate">
</form>
</body>
</html>
`
w.Write([]byte(html))
} else {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
})
http.HandleFunc("/totp", func(w http.ResponseWriter, r *http.Request) {
secret := r.FormValue("secret")
if secret == "" {
http.Error(w, "secret is required", http.StatusBadRequest)
return
}
otp, err := totp.GenerateCode(secret, time.Now())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte(otp))
})
log.Fatal(http.ListenAndServe(":8787", nil))
}