-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtoken.go
68 lines (61 loc) · 1.85 KB
/
token.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
package main
import (
"errors"
"fmt"
"hash/adler32"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
)
const separator = ":"
func storeToken(args *GlobalOptions, host string, token string) error {
err := ensureConfigPathExists(args.TokenDir)
if err != nil {
return err
}
if args.Verbose {
fmt.Println("Storing login token " + tokenFilename(args.TokenDir, host))
}
data := fmt.Sprintf("%s%s%s", args.model, separator, token)
return os.WriteFile(tokenFilename(args.TokenDir, host), []byte(data), 0644)
}
func tokenFilename(configDir string, host string) string {
hash32 := adler32.New()
io.WriteString(hash32, host)
return filepath.Join(dotConfigDirName(configDir), "token-"+fmt.Sprintf("%x", hash32.Sum(nil)))
}
func readTokenAndModel2GlobalOptions(args *GlobalOptions, host string) (NetgearModel, string, error) {
if len(args.model) > 0 {
return args.model, args.token, nil
}
if args.Verbose {
fmt.Println("reading token from: " + tokenFilename(args.TokenDir, host))
}
bytes, err := os.ReadFile(tokenFilename(args.TokenDir, host))
if errors.Is(err, fs.ErrNotExist) {
return "", "", errors.New("no session (token) exists. please login first")
}
data := strings.SplitN(string(bytes), separator, 2)
if len(data) != 2 {
return "", "", errors.New("you did an upgrade from a former ntgrcc version. please login again")
}
if !isSupportedModel(data[0]) {
return "", "", errors.New("unknown model stored in token. please login again")
}
args.model = NetgearModel(data[0])
args.token = data[1]
return args.model, args.token, err
}
func ensureConfigPathExists(configDir string) error {
dotConfigNtgrrc := dotConfigDirName(configDir)
err := os.MkdirAll(dotConfigNtgrrc, os.ModeDir|0700)
return err
}
func dotConfigDirName(configDir string) string {
if configDir == "" {
configDir = os.TempDir()
}
return filepath.Join(configDir, ".config", "ntgrrc")
}