Files
certmanager/internal/grpc/handler/account.go
Олег Бородин 90a9d94405 certmanager updates
2024-08-10 15:28:32 +02:00

81 lines
2.3 KiB
Go

package handler
import (
"context"
"certmanager/pkg/cmctl"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
func (hand *Handler) Authentificate(ctx context.Context) (int64, error) {
var err error
var accountID int64
meta, _ := metadata.FromIncomingContext(ctx)
usernameArr := meta["username"]
passwordArr := meta["password"]
if len(usernameArr) == 0 || len(passwordArr) == 0 {
err := status.Errorf(codes.PermissionDenied, "Empty auth data")
return accountID, err
}
username := meta["username"][0]
password := meta["password"][0]
validated, accountID, err := hand.lg.ValidateAcount(ctx, username, password)
if !validated {
err := status.Errorf(codes.PermissionDenied, "Wrong auth data")
return accountID, err
}
return accountID, err
}
func (hand *Handler) CreateAccount(ctx context.Context, params *cmctl.CreateAccountParams) (*cmctl.CreateAccountResult, error) {
var err error
hand.log.Debugf("Handle CreateAccount call")
res := &cmctl.CreateAccountResult{}
accountID, err := hand.Authentificate(ctx)
if err != nil {
return res, err
}
res, err = hand.lg.CreateAccount(ctx, accountID, params)
return res, err
}
func (hand *Handler) DeleteAccount(ctx context.Context, params *cmctl.DeleteAccountParams) (*cmctl.DeleteAccountResult, error) {
var err error
hand.log.Debugf("Handle DeleteAccount call")
res := &cmctl.DeleteAccountResult{}
accountID, err := hand.Authentificate(ctx)
if err != nil {
return res, err
}
res, err = hand.lg.DeleteAccount(ctx, accountID, params)
return res, err
}
func (hand *Handler) ListAccounts(ctx context.Context, params *cmctl.ListAccountsParams) (*cmctl.ListAccountsResult, error) {
var err error
hand.log.Debugf("Handle ListAccounts call")
res := &cmctl.ListAccountsResult{}
accountID, err := hand.Authentificate(ctx)
if err != nil {
return res, err
}
res, err = hand.lg.ListAccounts(ctx, accountID, params)
return res, err
}
func (hand *Handler) UpdateAccount(ctx context.Context, params *cmctl.UpdateAccountParams) (*cmctl.UpdateAccountResult, error) {
var err error
hand.log.Debugf("Handle UpdateAccount call")
res := &cmctl.UpdateAccountResult{}
accountID, err := hand.Authentificate(ctx)
if err != nil {
return res, err
}
res, err = hand.lg.UpdateAccount(ctx, accountID, params)
return res, err
}