Skip to content

Commit f39c64d

Browse files
authored
Merge pull request #23 from dim13/rev
Add -rev flag to generate otpauth-migration:// QR-code
2 parents 9098da1 + 5213089 commit f39c64d

File tree

5 files changed

+28
-14
lines changed

5 files changed

+28
-14
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ to plain [otpauth links](https://github.com/google/google-authenticator/wiki/Key
2626
-link string
2727
migration link (required)
2828
-qr
29-
generate QR-codes
29+
generate QR-codes (optauth://)
30+
-rev
31+
reverse QR-code (otpauth-migration://)
3032
```
3133

3234
## Example

main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Google Authenticator migration decoder
22
//
33
// convert "otpauth-migration" links to plain "otpauth" links
4-
//
54
package main
65

76
import (
@@ -32,7 +31,8 @@ func main() {
3231
cache = flag.String("cache", "migration.bin", "cache file")
3332
http = flag.String("http", "", "serve http (e.g. localhost:6060)")
3433
eval = flag.Bool("eval", false, "evaluate otps")
35-
qr = flag.Bool("qr", false, "generate QR-codes")
34+
qr = flag.Bool("qr", false, "generate QR-codes (optauth://)")
35+
rev = flag.Bool("rev", false, "reverse QR-code (otpauth-migration://)")
3636
info = flag.Bool("info", false, "display batch info")
3737
)
3838
flag.Parse()
@@ -54,10 +54,14 @@ func main() {
5454
}
5555
case *qr:
5656
for _, op := range p.OtpParameters {
57-
if err := op.WriteFile(op.FileName() + ".png"); err != nil {
57+
if err := migration.PNG(op.FileName()+".png", op.URL()); err != nil {
5858
log.Fatal("write file: ", err)
5959
}
6060
}
61+
case *rev:
62+
if err := migration.PNG("otpauth-migration.png", migration.URL(data)); err != nil {
63+
log.Fatal(err)
64+
}
6165
case *eval:
6266
for _, op := range p.OtpParameters {
6367
fmt.Printf("%06d %s\n", op.Evaluate(), op.Name)

migration/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package migration
33
import "net/http"
44

55
func (op *Payload_OtpParameters) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6-
pic, err := op.QR()
6+
pic, err := QR(op.URL())
77
if err != nil {
88
http.Error(w, err.Error(), http.StatusInternalServerError)
99
return

migration/qrcode.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@ package migration
22

33
import (
44
"io/ioutil"
5+
"net/url"
56

67
"github.com/skip2/go-qrcode"
78
)
89

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

14-
// WriteFile writes QR code as PNG to specified file
15-
func (op *Payload_OtpParameters) WriteFile(fname string) error {
16-
pic, err := op.QR()
15+
// PNG writes QR code as PNG to specified file
16+
func PNG(filename string, u *url.URL) error {
17+
pic, err := QR(u)
1718
if err != nil {
1819
return err
1920
}
20-
if err := ioutil.WriteFile(fname, pic, 0600); err != nil {
21-
return err
22-
}
23-
return nil
21+
return ioutil.WriteFile(filename, pic, 0600)
2422
}

migration/unmarshal.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ func Data(link string) ([]byte, error) {
3131
return base64.StdEncoding.DecodeString(data)
3232
}
3333

34+
func URL(data []byte) *url.URL {
35+
v := make(url.Values)
36+
v.Add("data", base64.StdEncoding.EncodeToString(data))
37+
return &url.URL{
38+
Scheme: "otpauth-migration",
39+
Host: "offline",
40+
RawQuery: v.Encode(),
41+
}
42+
}
43+
3444
// Unmarshal otpauth-migration data
3545
func Unmarshal(data []byte) (*Payload, error) {
3646
var p Payload

0 commit comments

Comments
 (0)