-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
main.go
95 lines (86 loc) · 2.28 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Google Authenticator migration decoder
//
// convert "otpauth-migration" links to plain "otpauth" links
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"github.com/dim13/otpauth/migration"
)
const (
cacheFilename = "migration.bin"
revFile = "otpauth-migration.png"
)
func migrationData(fname, link string) ([]byte, error) {
if link == "" {
// read from cache
return os.ReadFile(fname)
}
data, err := migration.Data(link)
if err != nil {
return nil, err
}
// write to cache
return data, os.WriteFile(fname, data, 0600)
}
func main() {
var (
link = flag.String("link", "", "migration link (required)")
workdir = flag.String("workdir", "", "working directory")
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 (optauth://)")
rev = flag.Bool("rev", false, "reverse QR-code (otpauth-migration://)")
info = flag.Bool("info", false, "display batch info")
)
flag.Parse()
if *workdir != "" {
if err := os.MkdirAll(*workdir, 0700); err != nil {
log.Fatal("error creating working directory: ", err)
}
}
cacheFile := filepath.Join(*workdir, cacheFilename)
data, err := migrationData(cacheFile, *link)
if err != nil {
log.Fatal("-link parameter or cache file missing: ", err)
}
p, err := migration.Unmarshal(data)
if err != nil {
log.Fatal("decode data: ", err)
}
switch {
case *http != "":
if err := serve(*http, p); err != nil {
log.Fatal("serve http: ", err)
}
case *qr:
for _, op := range p.OtpParameters {
fileName := op.FileName() + ".png"
qrFile := filepath.Join(*workdir, fileName)
if err := migration.PNG(qrFile, op.URL()); err != nil {
log.Fatal("write file: ", err)
}
}
case *rev:
revFile := filepath.Join(*workdir, revFile)
if err := migration.PNG(revFile, migration.URL(data)); err != nil {
log.Fatal(err)
}
case *eval:
for _, op := range p.OtpParameters {
fmt.Printf("%06d %s\n", op.Evaluate(), op.Name)
}
case *info:
fmt.Println("version", p.Version)
fmt.Println("batch size", p.BatchSize)
fmt.Println("batch index", p.BatchIndex)
fmt.Println("batch id", p.BatchId)
default:
for _, op := range p.OtpParameters {
fmt.Println(op.URL())
}
}
}