Skip to content

Commit ee55900

Browse files
committed
refactor: refactor in golang
1 parent 0e128cc commit ee55900

File tree

8 files changed

+180
-839
lines changed

8 files changed

+180
-839
lines changed

.prettierrc.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/geekdada/steam-otp
2+
3+
go 1.15
4+
5+
require (
6+
github.com/funny/binary v0.0.0-20151214134736-b048dcb0f179
7+
github.com/jessevdk/go-flags v1.4.0
8+
github.com/manifoldco/promptui v0.8.0
9+
)

go.sum

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
2+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
3+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
4+
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
5+
github.com/funny/binary v0.0.0-20151214134736-b048dcb0f179 h1:i+sPtS01ifIDV7EP+GJMwqermatkNjzgDbEzYhv36IY=
6+
github.com/funny/binary v0.0.0-20151214134736-b048dcb0f179/go.mod h1:0NTmabtiIl9h02d11pe02xPTFvnH5K56lrE0cpeq3eI=
7+
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
8+
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
9+
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU=
10+
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU=
11+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
12+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
13+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
14+
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a h1:weJVJJRzAJBFRlAiJQROKQs8oC9vOxvm4rZmBBk0ONw=
15+
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
16+
github.com/manifoldco/promptui v0.8.0 h1:R95mMF+McvXZQ7j1g8ucVZE1gLP3Sv6j9vlF9kyRqQo=
17+
github.com/manifoldco/promptui v0.8.0/go.mod h1:n4zTdgP0vr0S3w7/O/g98U+e0gwLScEXGwov2nIKuGQ=
18+
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
19+
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
20+
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
21+
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
22+
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
23+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

index.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

main.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/geekdada/steam-otp/otp"
6+
"github.com/jessevdk/go-flags"
7+
"github.com/manifoldco/promptui"
8+
"os"
9+
"strings"
10+
)
11+
12+
func main() {
13+
var options struct {
14+
Secret string `short:"s" long:"secret" description:"The shared secret you get from SteamDesktopAuthenticator"`
15+
}
16+
17+
_, err := flags.Parse(&options)
18+
19+
if err != nil {
20+
switch err.(type) {
21+
case *flags.Error:
22+
fe, _ := err.(*flags.Error)
23+
if fe.Type == flags.ErrHelp {
24+
os.Exit(0)
25+
}
26+
fmt.Println(err)
27+
os.Exit(1)
28+
default:
29+
fmt.Println(err)
30+
os.Exit(1)
31+
}
32+
}
33+
34+
if options.Secret != "" {
35+
code, err := otp.GenerateAuthCode(strings.TrimSpace(options.Secret), 0)
36+
37+
if err != nil {
38+
fmt.Printf("Failed to generate code %v\n", err)
39+
}
40+
41+
fmt.Println(code)
42+
43+
return
44+
}
45+
46+
prompt := promptui.Prompt{
47+
Label: "Input the shared secret you get from SteamDesktopAuthenticator",
48+
Mask: '*',
49+
}
50+
51+
result, err := prompt.Run()
52+
53+
if err != nil {
54+
fmt.Printf("Invalid input %v\n", err)
55+
return
56+
}
57+
58+
code, err := otp.GenerateAuthCode(strings.TrimSpace(result), 0)
59+
60+
if err != nil {
61+
fmt.Printf("Failed to generate code %v\n", err)
62+
}
63+
64+
fmt.Println(code)
65+
}

otp/utils.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package otp
2+
3+
import (
4+
hmac2 "crypto/hmac"
5+
"crypto/sha1"
6+
"encoding/base64"
7+
"encoding/hex"
8+
"github.com/funny/binary"
9+
"math"
10+
"regexp"
11+
"time"
12+
)
13+
14+
/**
15+
Returns the current local Unix time
16+
*/
17+
func getTime(timeOffset int64) int64 {
18+
now := time.Now()
19+
return now.Unix() + timeOffset
20+
}
21+
22+
func bufferizeSecret(secret string) (*binary.Buffer, error) {
23+
matched, err := regexp.MatchString(`[0-9a-f]{40}`, secret)
24+
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
if matched {
30+
hexValue, err := hex.DecodeString(secret)
31+
32+
if err != nil {
33+
return nil, err
34+
} else {
35+
return &binary.Buffer{Data: hexValue}, nil
36+
}
37+
}
38+
39+
base64Value, err := base64.StdEncoding.DecodeString(secret)
40+
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
return &binary.Buffer{Data: base64Value}, nil
46+
}
47+
48+
/**
49+
Generate a Steam-style TOTP authentication code
50+
*/
51+
func GenerateAuthCode(secret string, timeOffset int64) (string, error) {
52+
localTime := getTime(timeOffset)
53+
secretBuffer, err := bufferizeSecret(secret)
54+
55+
if err != nil {
56+
return "", err
57+
}
58+
59+
buf := binary.Buffer{Data: make([]byte, 8)}
60+
61+
buf.WriteUint32BE(0)
62+
buf.WriteUint32BE(uint32(math.Floor(float64(localTime) / 30)))
63+
64+
hmac := hmac2.New(sha1.New, secretBuffer.Data)
65+
hmac.Write(buf.Data)
66+
secretHash := hmac.Sum(nil)
67+
68+
startPos := secretHash[19] & 0x0F
69+
slicedSecretHashBuf := binary.Buffer{Data: secretHash[startPos : startPos + 4]}
70+
71+
fullCode := slicedSecretHashBuf.ReadUint32BE() & 0x7FFFFFFF
72+
chars := "23456789BCDFGHJKMNPQRTVWXY"
73+
code := ""
74+
75+
for i := 0; i < 5; i++ {
76+
code += string([]rune(chars)[int(fullCode) % len(chars)])
77+
fullCode /= uint32(len(chars))
78+
}
79+
80+
return code, nil
81+
}
82+
83+

package.json

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)