68 行
1.7 KiB
Go
68 行
1.7 KiB
Go
package httpclient
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/go-resty/resty/v2"
|
|
"integrity-checkin/data/dto"
|
|
"regexp"
|
|
"time"
|
|
)
|
|
|
|
var Client *resty.Client
|
|
|
|
func Setup() {
|
|
Client = resty.New().SetTimeout(10 * time.Second)
|
|
}
|
|
func SDULogin(username string, password string) (*dto.SDULoginData, error) {
|
|
resp, err := Client.R().SetFormData(map[string]string{
|
|
"username": username,
|
|
"password": password,
|
|
}).Post("https://pass.sdu.edu.cn/cas/restlet/tickets")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result1 := string(resp.Body())
|
|
if resp.StatusCode()/100 != 2 {
|
|
return nil, errors.New(result1)
|
|
}
|
|
resp, err = Client.R().SetBody("service=https://service.sdu.edu.cn/tp_up/view?m=up").
|
|
Post("https://pass.sdu.edu.cn/cas/restlet/tickets/" + result1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result2 := string(resp.Body())
|
|
if resp.StatusCode()/100 != 2 {
|
|
return nil, errors.New(result2)
|
|
}
|
|
resp, err = Client.R().SetQueryParams(map[string]string{
|
|
"ticket": result2,
|
|
"service": "https://service.sdu.edu.cn/tp_up/view?m=up",
|
|
}).Get("https://pass.sdu.edu.cn/cas/serviceValidate")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result3 := string(resp.Body())
|
|
if resp.StatusCode()/100 != 2 {
|
|
return nil, errors.New(result3)
|
|
}
|
|
realName := ""
|
|
username = ""
|
|
if arr := regexp.MustCompile("<cas:USER_NAME>(.*?)<").FindStringSubmatch(result3); len(arr) >= 2 {
|
|
realName = arr[1]
|
|
} else {
|
|
return nil, errors.New(result3)
|
|
}
|
|
if arr := regexp.MustCompile("<cas:ID_NUMBER>(.*?)<").FindStringSubmatch(result3); len(arr) >= 2 {
|
|
username = arr[1]
|
|
} else {
|
|
return nil, errors.New(result3)
|
|
}
|
|
if realName == "" || username == "" {
|
|
return nil, errors.New(result3)
|
|
}
|
|
return &dto.SDULoginData{
|
|
RealName: realName,
|
|
Username: username,
|
|
}, nil
|
|
}
|