fusenapi/utils/auth/confirmation_link.go

101 lines
2.2 KiB
Go
Raw Normal View History

2023-07-27 02:18:49 +00:00
package auth
import (
2023-09-03 17:40:28 +00:00
"fmt"
2023-07-28 04:15:56 +00:00
"fusenapi/utils/encryption_decryption"
2023-07-27 02:18:49 +00:00
"net/url"
2023-09-03 17:40:28 +00:00
"reflect"
2023-07-27 02:18:49 +00:00
)
2023-09-03 17:40:28 +00:00
type OperateType string
2023-07-27 08:48:43 +00:00
const (
2023-09-03 17:40:28 +00:00
OpTypeRegister OperateType = "1" //注册的操作类型
OpTypeResetToken OperateType = "2" //重置密码类型
2023-07-27 08:48:43 +00:00
)
2023-07-27 02:18:49 +00:00
type ConfirmationLink[T any] struct {
2023-07-28 04:15:56 +00:00
// Secret []byte
2023-09-03 17:40:28 +00:00
secretGCM *encryption_decryption.SecretGCM[T]
2023-07-28 04:15:56 +00:00
2023-09-03 17:40:28 +00:00
defaultTokenKey string // 默认key 是 token
defaultOpTypeKey string
link *url.URL
2023-07-27 02:18:49 +00:00
}
2023-07-28 04:15:56 +00:00
func NewConfirmationLink[T any](key string, UrlStr string) *ConfirmationLink[T] {
2023-07-27 02:18:49 +00:00
u, err := url.Parse(UrlStr)
if err != nil {
panic(err)
}
return &ConfirmationLink[T]{
2023-09-03 17:40:28 +00:00
secretGCM: encryption_decryption.NewSecretGCM[T](key),
defaultTokenKey: "token",
defaultOpTypeKey: "optype",
link: u,
2023-07-27 02:18:49 +00:00
}
}
2023-09-03 17:40:28 +00:00
// WithDefaultTokenKey 设置默认Token Key
func (cl *ConfirmationLink[T]) WithDefaultTokenKey(tkey string) *ConfirmationLink[T] {
cl.defaultTokenKey = tkey
return cl
}
// WithDefaultOpTypeKey 设置默认OpType Key
func (cl *ConfirmationLink[T]) WithDefaultOpTypeKey(opkey string) *ConfirmationLink[T] {
cl.defaultTokenKey = opkey
return cl
}
2023-07-27 02:18:49 +00:00
// Generate 序列化链接传入需求的obj
func (cl *ConfirmationLink[T]) Generate(obj *T) (string, error) {
2023-09-03 17:40:28 +00:00
vValue := reflect.ValueOf(obj).Elem()
opType := vValue.FieldByName("OperateType")
if !opType.IsValid() {
return "", fmt.Errorf("传入结构体 必须继承 OperateType")
}
op := opType.Interface().(OperateType)
2023-07-27 02:18:49 +00:00
token, err := cl.Encrypt(obj)
if err != nil {
return "", err
}
2023-09-03 17:40:28 +00:00
return cl.generateWithToken(token, op)
2023-07-27 02:18:49 +00:00
}
2023-09-03 17:40:28 +00:00
// generateWithToken 序列化url带token
func (cl *ConfirmationLink[T]) generateWithToken(token string, optype OperateType) (string, error) {
2023-07-27 02:18:49 +00:00
q := cl.link.Query()
2023-09-03 17:40:28 +00:00
if q.Has(cl.defaultTokenKey) {
q.Set(cl.defaultTokenKey, token)
} else {
q.Add(cl.defaultTokenKey, token)
}
if q.Has(cl.defaultOpTypeKey) {
q.Set(cl.defaultOpTypeKey, string(optype))
2023-07-27 02:18:49 +00:00
} else {
2023-09-03 17:40:28 +00:00
q.Add(cl.defaultOpTypeKey, string(optype))
2023-07-27 02:18:49 +00:00
}
// 生成确认链接
cl.link.RawQuery = q.Encode()
return cl.link.String(), nil
}
func (cl *ConfirmationLink[T]) Encrypt(obj *T) (string, error) {
2023-09-03 17:40:28 +00:00
return cl.secretGCM.Encrypt(obj)
2023-07-27 02:18:49 +00:00
}
func (cl *ConfirmationLink[T]) Decrypt(ciphertext string) (*T, error) {
2023-09-03 17:40:28 +00:00
return cl.secretGCM.Decrypt(ciphertext)
2023-07-27 02:18:49 +00:00
}