mirror of
https://github.com/fumiama/tienyik.git
synced 2026-06-05 07:20:25 +08:00
feat: add more apis
This commit is contained in:
@@ -2,8 +2,14 @@ package auth
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/fumiama/tienyik/internal/hcli"
|
||||
"github.com/fumiama/tienyik"
|
||||
"github.com/fumiama/tienyik/hcli"
|
||||
"github.com/fumiama/tienyik/internal/horm"
|
||||
"github.com/fumiama/tienyik/internal/hson"
|
||||
"github.com/fumiama/tienyik/internal/textio"
|
||||
)
|
||||
@@ -19,11 +25,126 @@ type ResponseNegotiationEncKey struct {
|
||||
EncKey string `json:"encKey"`
|
||||
}
|
||||
|
||||
type ResponseNegotiationEncKeyData struct {
|
||||
EValue string `json:"evalue"`
|
||||
EID string `json:"eid"`
|
||||
}
|
||||
|
||||
func (r *ResponseNegotiationEncKey) Unwrap(tyr *tienyik.RSA) (*ResponseNegotiationEncKeyData, error) {
|
||||
v, err := base64.StdEncoding.DecodeString(r.EncKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
aesk, err := tyr.Decrypt(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v, err = base64.StdEncoding.DecodeString(r.EncData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v, err = tienyik.NewAES(aesk).Decrypt(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var rsp ResponseNegotiationEncKeyData
|
||||
err = json.Unmarshal(v, &rsp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &rsp, nil
|
||||
}
|
||||
|
||||
func NegotiationEncKey(r *RequestNegotiationEncKey) (*ResponseNegotiationEncKey, error) {
|
||||
resp, err := hcli.Post(textio.API(), bytes.NewReader(hson.Marshal(nil, r)))
|
||||
resp, err := hcli.NoClient.Post(
|
||||
textio.API(), textio.ContenTypeJSON,
|
||||
bytes.NewReader(hson.Marshal(nil, r)),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return hson.Unmarshal[*ResponseNegotiationEncKey](nil, resp.Body)
|
||||
}
|
||||
|
||||
type ResponseGenChallengeData struct {
|
||||
EffectiveSeconds int `json:"effectiveSeconds"`
|
||||
ChallengeID string `json:"challengeId"`
|
||||
ChallengeCode string `json:"challengeCode"`
|
||||
}
|
||||
|
||||
func GenChallengeData(tya *tienyik.AES, cli *hcli.Client) (*ResponseGenChallengeData, error) {
|
||||
resp, err := cli.Post(
|
||||
textio.API(), textio.ContenTypeJSON,
|
||||
strings.NewReader("{}"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return hson.Unmarshal[*ResponseGenChallengeData](tya, resp.Body)
|
||||
}
|
||||
|
||||
type RequestLogin struct {
|
||||
UserAccount string `form:"userAccount"`
|
||||
Password string `form:"password"`
|
||||
SHA256Password string `form:"sha256Password"`
|
||||
ChallengeID string `form:"challengeId"`
|
||||
DeviceCode string `form:"deviceCode"`
|
||||
DeviceName string `form:"deviceName"`
|
||||
DeviceType string `form:"deviceType"`
|
||||
DeviceModel string `form:"deviceModel"`
|
||||
AppVersion string `form:"appVersion"`
|
||||
SysVersion string `form:"sysVersion"`
|
||||
ClientVersion string `form:"clientVersion"`
|
||||
}
|
||||
|
||||
type ResponseLogin struct {
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
UserID int64 `json:"userId"`
|
||||
UserEid string `json:"userEid"`
|
||||
UserName string `json:"userName"`
|
||||
UserAccount string `json:"userAccount"`
|
||||
Email *string `json:"email"`
|
||||
Mobilephone string `json:"mobilephone"`
|
||||
TenantID int64 `json:"tenantId"`
|
||||
SecretKey string `json:"secretKey"`
|
||||
DeviceType string `json:"deviceType"`
|
||||
TenantName string `json:"tenantName"`
|
||||
BondedDevice bool `json:"bondedDevice"`
|
||||
RealNameStatus int64 `json:"realNameStatus"`
|
||||
HasPassword int64 `json:"hasPassword"`
|
||||
PID *string `json:"pid"`
|
||||
NeedSmsValidate bool `json:"needSmsValidate"`
|
||||
NeedUpdatePassword bool `json:"needUpdatePassword"`
|
||||
ForceUpdateInitialPwd bool `json:"forceUpdateInitialPwd"`
|
||||
AdminUser bool `json:"adminUser"`
|
||||
LoginNotify *string `json:"loginNotify"`
|
||||
CommonLoginReqHeader string `json:"commonLoginReqHeader"`
|
||||
RecordSn bool `json:"recordSn"`
|
||||
Token *string `json:"token"`
|
||||
TwoFaValidateType *string `json:"twoFaValidateType"`
|
||||
Random *string `json:"random"`
|
||||
NeedBindVirtualMfa *bool `json:"needBindVirtualMfa"`
|
||||
Tryout *string `json:"tryout"`
|
||||
CtqEncID *string `json:"ctqEncId"`
|
||||
}
|
||||
|
||||
func (r *ResponseLogin) SetClient(cli *hcli.Client) {
|
||||
cli.Tenantid = strconv.FormatInt(r.TenantID, 10)
|
||||
cli.Usereid = r.UserEid
|
||||
}
|
||||
|
||||
func Login(tya *tienyik.AES, cli *hcli.Client, r *RequestLogin) (*ResponseLogin, error) {
|
||||
resp, err := cli.Post(
|
||||
textio.API(), textio.ContenTypeForm,
|
||||
bytes.NewReader(horm.Marshal(tya, r)),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return hson.Unmarshal[*ResponseLogin](tya, resp.Body)
|
||||
}
|
||||
|
||||
50
api/auth/client/qrCode.go
Normal file
50
api/auth/client/qrCode.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/fumiama/tienyik"
|
||||
"github.com/fumiama/tienyik/hcli"
|
||||
"github.com/fumiama/tienyik/internal/hson"
|
||||
"github.com/fumiama/tienyik/internal/textio"
|
||||
)
|
||||
|
||||
type ResponseGenData struct {
|
||||
QrCodeId string `json:"qrCodeId"`
|
||||
QrCodeEndpoint *string `json:"qrCodeEndpoint"`
|
||||
ServerHost string `json:"serverHost"`
|
||||
}
|
||||
|
||||
func GenData(tya *tienyik.AES, cli *hcli.Client) (*ResponseGenData, error) {
|
||||
resp, err := cli.Post(textio.API(), textio.ContenTypeForm, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return hson.Unmarshal[*ResponseGenData](tya, resp.Body)
|
||||
}
|
||||
|
||||
type ResponseGetStatusData struct {
|
||||
CodeId string `json:"codeId"`
|
||||
CodeStatus string `json:"codeStatus"`
|
||||
LoginToken *string `json:"loginToken"`
|
||||
ClientCustomData *string `json:"clientCustomData"`
|
||||
SupplementData *string `json:"supplementData"`
|
||||
}
|
||||
|
||||
func GetStatus(tya *tienyik.AES, cli *hcli.Client, qrCodeId string) (*ResponseGetStatusData, error) {
|
||||
u, err := url.Parse(textio.API())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
q := u.Query()
|
||||
q.Set("qrCodeId", qrCodeId)
|
||||
u.RawQuery = tya.EUrlParams(q)
|
||||
|
||||
resp, err := cli.Get(u.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return hson.Unmarshal[*ResponseGetStatusData](tya, resp.Body)
|
||||
}
|
||||
18
api/auth/client/qrCode_test.go
Normal file
18
api/auth/client/qrCode_test.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/fumiama/tienyik/hcli"
|
||||
)
|
||||
|
||||
func TestGenData(t *testing.T) {
|
||||
r, err := GenData(nil, hcli.NewClient())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(r)
|
||||
if r.QrCodeId == "" {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package auth
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"encoding/base64"
|
||||
"testing"
|
||||
|
||||
"github.com/fumiama/tienyik"
|
||||
@@ -32,23 +31,8 @@ func TestNegotiationEncKey(t *testing.T) {
|
||||
t.Logf("EncData: %s", r.EncData)
|
||||
t.Logf("EncKey: %s", r.EncKey)
|
||||
|
||||
v, err := base64.StdEncoding.DecodeString(r.EncKey)
|
||||
_, err = r.Unwrap(tyr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
aesk, err := tyr.Decrypt(v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(string(aesk))
|
||||
|
||||
v, err = base64.StdEncoding.DecodeString(r.EncData)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
v, err = tienyik.NewAES([]byte(aesk)).Decrypt(v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(string(v))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user