-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
92 lines (78 loc) · 2.09 KB
/
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
package api
import (
"bytes"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"github.com/even0306/cloudreve_go_sdk/requrl"
)
type Auth interface {
Login() error
}
type Group struct {
Id int `json:"id"`
Name string `json:"name"`
AllowShare bool `json:"allowShare"`
AllowRemoteDownload bool `json:"allowRemoteDownload"`
AllowArchiveDownload bool `json:"allowArchiveDownload"`
ShareDownload bool `json:"shareDownload"`
Compress bool `json:"compress"`
Webdav bool `json:"webdav"`
SourceBatch int `json:"sourceBatch"`
AdvanceDelete bool `json:"advanceDelete"`
AllowWebDAVProxy bool `json:"allowWebDAVProxy"`
}
type AuthData struct {
Id string `json:"id"`
User_name string `json:"user_name"`
Nickname string `json:"nickname"`
Status int `json:"status"`
Avatar string `json:"avatar"`
Created_at string `json:"created_at"`
Preferred_theme string `json:"preferred_theme"`
Anonymous bool `json:"anonymous"`
Group Group `json:"group"`
Tags []string `json:"tags"`
}
type AuthRespBody struct {
Code int `json:"code"`
Data AuthData `json:"data"`
Msg string `json:"msg"`
}
type AuthUserInfo struct {
UserName string
Password string
CaptchaCode string
}
func NewAuthFunc() *AuthRespBody {
return &AuthRespBody{
Code: 0,
Data: AuthData{},
Msg: "",
}
}
func (respBody *AuthRespBody) Login(loginData AuthUserInfo) error {
b, err := json.Marshal(&loginData)
if err != nil {
return err
}
req, err := http.NewRequest("POST", requrl.ReqHost+"/api/v3/user/session", bytes.NewReader(b))
if err != nil {
return err
}
resp, err := requrl.Client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf(fmt.Sprint(resp.StatusCode))
}
err = json.NewDecoder(resp.Body).Decode(&respBody)
if err != nil {
return err
}
slog.Info(respBody.Msg, "Code", respBody.Code, "Data", respBody.Data)
return nil
}