-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
602b6c1
commit 1ea495f
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package token | ||
|
||
import ( | ||
"crypto" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/sec51/twofactor" | ||
) | ||
|
||
type token struct { | ||
totp *twofactor.Totp | ||
} | ||
|
||
func New() *token { | ||
//todo save secret to disk and load from there (but where?) | ||
//todo multiple tokens so we can expire/rotate them? | ||
totp, err := twofactor.NewTOTP("friend", "wg-sync", crypto.SHA256, 6) | ||
if err != nil { | ||
panic(err) //can't generate otop token | ||
} | ||
//Todo have this also serve up a one time token also by returning a mux? | ||
return &token{totp: totp} | ||
|
||
} | ||
|
||
func (o *token) ServeHTTP(resp http.ResponseWriter, _ *http.Request) { | ||
qr, err := o.totp.QR() | ||
if err != nil { | ||
log.Printf("Error generating QR code: %v", err) | ||
resp.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
resp.Header().Set("Content-Type", "image/png") | ||
_, err = resp.Write(qr) | ||
if err != nil { | ||
log.Printf("Error generating QR code: %v", err) | ||
resp.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
resp.WriteHeader(http.StatusOK) | ||
} |