commit 58c8f54a9a69efce6987bdd246c3b1ce71b86102 Author: Bryan Joshua Pedini Date: Tue Sep 15 09:48:54 2020 +0200 first commit diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e6de95f --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.bjphoster.com/bryanpedini/go-totp + +go 1.14 diff --git a/main.go b/main.go new file mode 100644 index 0000000..32ab54f --- /dev/null +++ b/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "encoding/json" + "net/http" + "strings" + "text/template" + "time" + + "github.com/xlzd/gotp" +) + +type secret struct { + S string `json:"secret"` +} +type code struct { + C string `json:"code"` + T int `json:"time"` +} + +func calculate(w http.ResponseWriter, r *http.Request) { + var s secret + json.NewDecoder(r.Body).Decode(&s) + totp := gotp.NewDefaultTOTP(s.S).Now() + c := &code{ + C: totp, + T: int(30 - time.Now().Unix()%30), + } + response, _ := json.Marshal(c) + w.Header().Set("Content-Type", "application/json") + w.Write(response) +} + +func index(w http.ResponseWriter, r *http.Request) { + t, _ := template.ParseFiles("templates/index.html") + t.Execute(w, nil) +} + +func main() { + staticFolder := http.FileServer(http.Dir("./static")) + http.HandleFunc("/", index) + http.HandleFunc("/calculate", calculate) + http.Handle("/static/", http.StripPrefix(strings.TrimRight("/static/", "/"), staticFolder)) + fmt.Println("Listening on 127.0.0.1:8000") + http.ListenAndServe("127.0.0.1:8000", nil) +} diff --git a/static/main.js b/static/main.js new file mode 100644 index 0000000..0445852 --- /dev/null +++ b/static/main.js @@ -0,0 +1,22 @@ +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +async function update() { + var d = new Date() + console.log(d.getMilliseconds()) +} + +function send() { + var secret = document.getElementsByClassName("secretBox")[0].value; + var xhr = new XMLHttpRequest(); + xhr.open("POST", "/calculate", true); + xhr.setRequestHeader('Content-Type', 'application/json'); + xhr.send(JSON.stringify({ + secret: secret, + })); + xhr.onload = function() { + document.getElementsByClassName("totp-response-code")[0].innerHTML = JSON.parse(this.responseText)["code"]; + document.getElementsByClassName("totp-response-time")[0].innerHTML = JSON.parse(this.responseText)["time"]; + } +} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..fd9b21e --- /dev/null +++ b/templates/index.html @@ -0,0 +1,21 @@ + + + + + + + + TOTP Web Generator + + + + + +
+ Your code:
+ Remaining time: +
+ + + +