/
decode.go
61 lines (55 loc) · 1.76 KB
/
decode.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
package cli
import (
"fmt"
"strings"
"github.com/uaraven/clotp/migration"
"github.com/uaraven/gotp"
)
type DecodeCmd struct {
Uri string `arg:"positional,required" help:"Google Authenticator Export URI"`
Parse bool `arg:"-p,--parse" help:"Parse URIs and print each part separately"`
}
func parseOTP(account string, issuer string, otp gotp.OTP) (string, error) {
switch otpg := otp.(type) {
case *gotp.TOTP:
hash, _ := gotp.HashAlgorithmName(otpg.Hash)
return fmt.Sprintf("AccountName='%s', Issuer='%s', Secret='%s', Hash=%s, Digits=%d, TimeStep=%d", account, issuer,
gotp.EncodeKey(otpg.Secret), hash, otpg.Digits, otpg.TimeStep), nil
case *gotp.HOTP:
hash, _ := gotp.HashAlgorithmName(otpg.Hash)
return fmt.Sprintf("AccountName='%s', Issuer='%s', Secret='%s', Hash=%s, Digits=%d, Counter=%d", account, issuer,
gotp.EncodeKey(otpg.Secret), hash, otpg.Digits, otpg.GetCounter()), nil
default:
return "", fmt.Errorf("unknown OTP type")
}
}
func decodeMigrationUri(migrationUri string, parse bool) ([]string, error) {
payload, err := migration.UnmarshalURL(migrationUri)
if err != nil {
return nil, err
}
var result = make([]string, 0)
for _, otpParam := range payload.OtpParameters {
otp, err := otpFromParameters(otpParam)
if err != nil {
return nil, err
}
if parse {
line, err := parseOTP(otpParam.Name, otpParam.Issuer, otp)
if err != nil {
return nil, err
}
result = append(result, fmt.Sprintf("%s\n", line))
} else {
result = append(result, fmt.Sprintf("%s\n", otp.ProvisioningUri(otpParam.Name, otpParam.Issuer)))
}
}
return result, nil
}
func Decode(cmd *DecodeCmd) (string, error) {
results, err := decodeMigrationUri(cmd.Uri, cmd.Parse)
if err != nil {
return "", err
}
return strings.Join(results, "\n"), nil
}