liuhaijun e94826ce29 add server
Change-Id: I0760f17f6a01c0121b59fcbfafc666032dbc30af
2024-09-19 09:44:15 +00:00

49 lines
1.1 KiB
Go

package auth
import (
"encoding/base64"
"encoding/json"
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/pkg/crypto"
)
// if extra info should be traced in the future,
// add in this struct, it's not wise to touch the
// auth logic, what should be as stable as possible.
type LoginInfo struct {
Username string `json:"username"`
Password string `json:"password"`
DisplayName string `json:"displayName"`
Token string `json:"token"`
UserID string `json:"userId"`
Role int32 `json:"role"`
}
func (i LoginInfo) Encrypt() (encrypted string, err error) {
var buffer []byte
if buffer, err = json.Marshal(i); err != nil {
return
}
if buffer, err = crypto.AEADEncrypt(buffer); err != nil {
return
}
encrypted = base64.StdEncoding.EncodeToString(buffer)
return
}
func Decrypt(encrypted string) (decrypted *LoginInfo, err error) {
var buffer []byte
buffer, err = base64.StdEncoding.DecodeString(encrypted)
if err != nil {
return
}
if buffer, err = crypto.AEADDecrypt(buffer); err != nil {
return
}
var t LoginInfo
if err = json.Unmarshal(buffer, &t); err != nil {
return
}
decrypted = &t
return
}