@@ -16,53 +16,46 @@ package cmd
16
16
17
17
import (
18
18
"crypto/sha1"
19
- "encoding/json"
20
19
"fmt"
21
20
"io"
22
- "io/ioutil"
23
- "net/http"
24
- "net/http/cookiejar"
25
21
"net/url"
26
22
"os"
27
- "strings"
28
23
"syscall"
29
- "time"
30
24
31
- "e.coding.net/codingcorp/coding-cli/pkg/model "
25
+ "e.coding.net/codingcorp/coding-cli/pkg/request "
32
26
"github.com/spf13/cobra"
33
27
"golang.org/x/crypto/ssh/terminal"
34
- "golang.org/x/net/publicsuffix"
35
28
)
36
29
37
30
const (
38
- // loginURL = "https://codigncorp.coding.net /api/v2/account/login"
39
- loginURL = "http://codingcorp.coding.codingprod.net/ /api/v2/account/login"
31
+ // loginURL = "/api/v2/account/login"
32
+ loginURI = "/api/v2/account/login"
40
33
minAccountSize = 6
41
34
cookieFile = ".cookie"
42
35
)
43
36
44
- var username string
37
+ var account string
45
38
46
39
// loginCmd represents the login command
47
40
var loginCmd = & cobra.Command {
48
41
Use : "login" ,
49
42
Short : "登录到企业版" ,
50
- Long : `使用 Coding 企业版用户名和密码登录 。` ,
43
+ Long : `使用 Coding 企业版用户名(邮箱或手机号)和密码登录 。` ,
51
44
Run : func (cmd * cobra.Command , args []string ) {
52
- if len (username ) >= 6 {
45
+ if len (account ) >= 3 {
53
46
password , err := readPassword ()
54
47
if err != nil {
55
48
fmt .Fprintf (os .Stderr , "\n 登录失败,%v\n " , err )
56
49
return
57
50
}
58
- err = login (username , sha1Password (password ))
51
+ err = login (account , sha1Password (password ))
59
52
if err != nil {
60
53
fmt .Fprintln (os .Stderr , err )
61
54
return
62
55
}
63
56
return
64
57
}
65
- fmt .Fprintf (os .Stderr , "用户名至少 6 位\n " )
58
+ fmt .Fprintf (os .Stderr , "用户名至少 3 位\n " )
66
59
},
67
60
}
68
61
@@ -91,9 +84,9 @@ func readPassword() (string, error) {
91
84
func init () {
92
85
rootCmd .AddCommand (loginCmd )
93
86
94
- const userNameFlag = "username "
95
- loginCmd .Flags ().StringVarP (& username , userNameFlag , "u" , "" , "用户名" )
96
- loginCmd .MarkFlagRequired (userNameFlag )
87
+ const accountFlag = "account "
88
+ loginCmd .Flags ().StringVarP (& account , accountFlag , "u" , "" , "用户名(邮箱或手机号) " )
89
+ loginCmd .MarkFlagRequired (accountFlag )
97
90
}
98
91
99
92
func sha1Password (password string ) string {
@@ -102,115 +95,29 @@ func sha1Password(password string) string {
102
95
return fmt .Sprintf ("%x" , h .Sum (nil ))
103
96
}
104
97
105
- func login (username string , sha1Password string ) error {
106
- u , err := url .Parse (loginURL )
107
- if err != nil {
108
- return fmt .Errorf ("登录请求 URL 错误: %s, %v" , loginURL , err )
109
- }
110
-
111
- req , err := http .NewRequest (
112
- http .MethodPost ,
113
- loginURL ,
114
- urlEncodeLoginForm (username , sha1Password ),
115
- )
116
- if err != nil {
117
- return fmt .Errorf ("创建登录请求失败, 地址: %s, %v" , loginURL , err )
118
- }
119
-
120
- req = formURLEncoded (req )
121
- jar , err := newCookieJar ()
98
+ func login (account string , sha1Password string ) error {
99
+ form := url.Values {}
100
+ form .Set ("account" , account )
101
+ form .Set ("password" , sha1Password )
102
+ req := request .NewPostRequest (loginURI , & form )
103
+ req .On2fa = get2faCode
104
+ _ , err := req .Send ()
122
105
if err != nil {
123
106
return err
124
107
}
125
- cookie , err := readCookie ()
126
- if err != nil {
127
- // 读取失败也不要紧,继续执行
128
- fmt .Println (err )
129
- } else {
130
- jar .SetCookies (u , []* http.Cookie {cookie })
131
- }
132
- client := & http.Client {
133
- Timeout : 10 * time .Second ,
134
- Jar : jar ,
135
- }
136
-
137
- resp , err := client .Do (req )
138
- if err != nil {
139
- return fmt .Errorf ("发送登录请求失败, 地址: %s, %v" , loginURL , err )
140
- }
141
- defer resp .Body .Close ()
142
-
143
- err = saveCookie (jar .Cookies (u ))
144
- if err != nil {
145
- // 保存失败也不要紧,继续执行
146
- fmt .Println (err )
147
- }
148
-
149
- if resp .StatusCode != http .StatusOK {
150
- return fmt .Errorf ("登录失败, 地址: %s, 错误码: %d" , loginURL , resp .StatusCode )
151
- }
152
-
153
- bodyBytes , err := ioutil .ReadAll (resp .Body )
154
- if err != nil {
155
- return fmt .Errorf ("读取响应内容失败, %v" , err )
156
- }
157
- var result model.Result
158
- json .Unmarshal (bodyBytes , & result )
159
- if result .Code != 0 {
160
- return fmt .Errorf ("登录失败 %v" , result .Msg )
161
- }
162
-
163
- fmt .Printf ("登录成功\n " )
164
- return nil
165
- }
166
-
167
- func formURLEncoded (r * http.Request ) * http.Request {
168
- r .Header .Add ("Content-Type" , "application/x-www-form-URLencoded" )
169
- return r
170
- }
171
-
172
- func newCookieJar () (http.CookieJar , error ) {
173
- jar , err := cookiejar .New (& cookiejar.Options {PublicSuffixList : publicsuffix .List })
174
- if err != nil {
175
- return nil , fmt .Errorf ("无法创建用户保存 Session 的 Cookie Jar, %v" , err )
176
- }
177
- return jar , nil
178
- }
179
-
180
- func urlEncodeLoginForm (u string , p string ) io.Reader {
181
- form := url.Values {}
182
- form .Set ("account" , u )
183
- form .Set ("password" , p )
184
- return strings .NewReader (form .Encode ())
185
- }
186
-
187
- func saveCookie (cookies []* http.Cookie ) error {
188
- for _ , c := range cookies {
189
- if c .Name == "sid" || c .Name == "eid" {
190
- if c == nil {
191
- return fmt .Errorf ("Session Cookie 不存在" )
192
- }
193
- err := ioutil .WriteFile (cookieFile , []byte (c .Name + "=" + c .Value ), 0666 )
194
- if err != nil {
195
- return fmt .Errorf ("保存 Session Cookie 失败,%v" , err )
196
- }
197
- return nil
198
- }
199
- }
108
+ fmt .Println ("登录成功" )
200
109
return nil
201
110
}
202
111
203
- func readCookie () (* http.Cookie , error ) {
204
- b , err := ioutil .ReadFile (cookieFile )
112
+ func get2faCode () (string , error ) {
113
+ fmt .Print ("两步验证码: " )
114
+ b , err := terminal .ReadPassword (int (syscall .Stdin ))
205
115
if err != nil {
206
- return nil , fmt .Errorf ("读取 Cookie 文件错误, %v" , err )
116
+ return "" , fmt .Errorf ("读取两步验证码失败, %v" , err )
207
117
}
208
- cookiePair := strings . Split ( string (b ), "=" )
209
- if len (cookiePair ) != 2 {
210
- return nil , fmt . Errorf ( "Cookie 文件内容格式错误,文件:%s,%v" , cookieFile , cookiePair )
118
+ code := string (b )
119
+ if len (code ) == 6 {
120
+ return code , nil
211
121
}
212
- return & http.Cookie {
213
- Name : cookiePair [0 ],
214
- Value : cookiePair [1 ],
215
- }, nil
122
+ return "" , fmt .Errorf ("读取两步验为 6 位数字" )
216
123
}
0 commit comments