Skip to content

Commit

Permalink
Add -rev flag to generate otpauth-migration:// QR-code
Browse files Browse the repository at this point in the history
Resolves #22
  • Loading branch information
dim13 committed Aug 11, 2022
1 parent 9098da1 commit 5213089
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ to plain [otpauth links](https://github.com/google/google-authenticator/wiki/Key
-link string
migration link (required)
-qr
generate QR-codes
generate QR-codes (optauth://)
-rev
reverse QR-code (otpauth-migration://)
```

## Example
Expand Down
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Google Authenticator migration decoder
//
// convert "otpauth-migration" links to plain "otpauth" links
//
package main

import (
Expand Down Expand Up @@ -32,7 +31,8 @@ func main() {
cache = flag.String("cache", "migration.bin", "cache file")
http = flag.String("http", "", "serve http (e.g. localhost:6060)")
eval = flag.Bool("eval", false, "evaluate otps")
qr = flag.Bool("qr", false, "generate QR-codes")
qr = flag.Bool("qr", false, "generate QR-codes (optauth://)")
rev = flag.Bool("rev", false, "reverse QR-code (otpauth-migration://)")
info = flag.Bool("info", false, "display batch info")
)
flag.Parse()
Expand All @@ -54,10 +54,14 @@ func main() {
}
case *qr:
for _, op := range p.OtpParameters {
if err := op.WriteFile(op.FileName() + ".png"); err != nil {
if err := migration.PNG(op.FileName()+".png", op.URL()); err != nil {
log.Fatal("write file: ", err)
}
}
case *rev:
if err := migration.PNG("otpauth-migration.png", migration.URL(data)); err != nil {
log.Fatal(err)
}
case *eval:
for _, op := range p.OtpParameters {
fmt.Printf("%06d %s\n", op.Evaluate(), op.Name)
Expand Down
2 changes: 1 addition & 1 deletion migration/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package migration
import "net/http"

func (op *Payload_OtpParameters) ServeHTTP(w http.ResponseWriter, r *http.Request) {
pic, err := op.QR()
pic, err := QR(op.URL())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
16 changes: 7 additions & 9 deletions migration/qrcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ package migration

import (
"io/ioutil"
"net/url"

"github.com/skip2/go-qrcode"
)

// QR image bytes as PNG
func (op *Payload_OtpParameters) QR() ([]byte, error) {
return qrcode.Encode(op.URL().String(), qrcode.Medium, -3)
func QR(u *url.URL) ([]byte, error) {
return qrcode.Encode(u.String(), qrcode.Medium, -3)
}

// WriteFile writes QR code as PNG to specified file
func (op *Payload_OtpParameters) WriteFile(fname string) error {
pic, err := op.QR()
// PNG writes QR code as PNG to specified file
func PNG(filename string, u *url.URL) error {
pic, err := QR(u)
if err != nil {
return err
}
if err := ioutil.WriteFile(fname, pic, 0600); err != nil {
return err
}
return nil
return ioutil.WriteFile(filename, pic, 0600)
}
10 changes: 10 additions & 0 deletions migration/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ func Data(link string) ([]byte, error) {
return base64.StdEncoding.DecodeString(data)
}

func URL(data []byte) *url.URL {
v := make(url.Values)
v.Add("data", base64.StdEncoding.EncodeToString(data))
return &url.URL{
Scheme: "otpauth-migration",
Host: "offline",
RawQuery: v.Encode(),
}
}

// Unmarshal otpauth-migration data
func Unmarshal(data []byte) (*Payload, error) {
var p Payload
Expand Down

0 comments on commit 5213089

Please sign in to comment.