-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_auth.go
148 lines (121 loc) · 3.34 KB
/
client_auth.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package huwlte
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/xml"
"errors"
"golang.org/x/xerrors"
)
type ClientUser struct {
*Client
}
type UserStateType int
const (
UserStateLoggedIn UserStateType = 0
UserStateLoggedOut UserStateType = -1
UserStateRepeat UserStateType = -2
)
func (typ UserStateType) String() string {
switch typ {
case UserStateLoggedIn:
return "logged_in"
case UserStateLoggedOut:
return "logged_out"
case UserStateRepeat:
return "repeat"
default:
return "unknown"
}
}
type UserPasswordType int
const (
UserPasswordTypeBase64 UserPasswordType = 0
UserPasswordTypeSHA256 UserPasswordType = 4
)
func (typ UserPasswordType) String() string {
switch typ {
case UserPasswordTypeBase64:
return "base64"
case UserPasswordTypeSHA256:
return "sha256"
default:
return "unknown"
}
}
type UserStateLogin struct {
XMLName xml.Name `xml:"response" human:"-"`
State UserStateType `xml:"State"`
Username string `xml:"Username"`
PasswordType UserPasswordType `xml:"password_type"`
ExternPasswordType int `xml:"extern_password_type"`
Firstlogin int `xml:"firstlogin"`
}
func (auth *ClientUser) StateLogin(ctx context.Context) (*UserStateLogin, error) {
var result UserStateLogin
if err := auth.withSessionRetry(ctx, func(ctx context.Context) error {
return auth.get(ctx, "/api/user/state-login", &result)
}); err != nil {
return nil, err
}
return &result, nil
}
func (auth *ClientUser) Login(ctx context.Context, username string, password string, relogin bool) error {
if username == "" {
username = "admin"
}
state, err := auth.StateLogin(ctx)
var modemErr *Error
if errors.As(err, &modemErr) && modemErr.Code == ErrorCodeNotSupported {
return nil
} else if err != nil {
return err
}
if state.State == UserStateLoggedIn && !relogin {
return nil
}
return auth.login(ctx, username, password, state.PasswordType)
}
func stringSHA256(v string) string {
h := sha256.New()
h.Write([]byte(v))
return hex.EncodeToString(h.Sum(nil))
}
func stringBase64(v string) string {
return base64.StdEncoding.EncodeToString([]byte(v))
}
func generateConcentratedPasswordSHA256(username, password, verificationToken string) string {
buf := &bytes.Buffer{}
buf.WriteString(username)
buf.WriteString(stringBase64(stringSHA256(password)))
buf.WriteString(verificationToken)
return stringBase64(stringSHA256(buf.String()))
}
type userLoginRequest struct {
XMLName xml.Name `xml:"request"`
Username string `xml:"Username"`
Password string `xml:"Password"`
PasswordType int `xml:"password_type"`
}
func (auth *ClientUser) login(ctx context.Context, username, password string, pt UserPasswordType) error {
if password != "" {
switch pt {
case UserPasswordTypeSHA256:
password = generateConcentratedPasswordSHA256(username, password, auth.session.Tokens[0])
default:
password = base64.StdEncoding.EncodeToString([]byte(password))
}
}
if err := auth.withSessionRetry(ctx, func(ctx context.Context) error {
return auth.post(ctx, "/api/user/login", &userLoginRequest{
Username: username,
Password: password,
PasswordType: int(pt),
}, true, nil)
}); err != nil {
return xerrors.Errorf("post /api/user/login: %w", err)
}
return nil
}