49 lines
1.1 KiB
Go
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
|
|
}
|