47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package secretlogic
|
|
|
|
import (
|
|
"context"
|
|
"tianyuan-api/apps/sentinel/internal/model"
|
|
"tianyuan-api/apps/sentinel/internal/svc"
|
|
"tianyuan-api/apps/sentinel/sentinel"
|
|
"tianyuan-api/pkg/crypto"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type CreateSecretLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewCreateSecretLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateSecretLogic {
|
|
return &CreateSecretLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
// Secret methods
|
|
func (l *CreateSecretLogic) CreateSecret(in *sentinel.CreateSecretRequest) (*sentinel.Secret, error) {
|
|
secretId, err := crypto.GenerateSecretId()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
aesKey, err := crypto.GenerateSecretKey()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_, err = l.svcCtx.SecretsModel.Insert(l.ctx, &model.Secrets{
|
|
UserId: in.UserId,
|
|
SecretId: secretId,
|
|
AesKey: aesKey,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &sentinel.Secret{}, nil
|
|
}
|