83 lines
2.4 KiB
Go
83 lines
2.4 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 userID int64
|
|
|
|
meta, _ := metadata.FromIncomingContext(ctx)
|
|
hand.log.Debugf("Reqest username: %s", meta["username"])
|
|
hand.log.Debugf("Reqest password: %s", meta["password"])
|
|
usernameArr := meta["username"]
|
|
passwordArr := meta["password"]
|
|
if len(usernameArr) == 0 || len(passwordArr) == 0 {
|
|
err := status.Errorf(codes.PermissionDenied, "Empty auth data")
|
|
return userID, err
|
|
}
|
|
username := meta["username"][0]
|
|
password := meta["password"][0]
|
|
validated, userID, err := hand.lg.ValidateAcount(ctx, username, password)
|
|
if !validated {
|
|
err := status.Errorf(codes.PermissionDenied, "Wrong auth data")
|
|
return userID, err
|
|
}
|
|
return userID, 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{}
|
|
userID, err := hand.Authentificate(ctx)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res, err = hand.lg.CreateAccount(ctx, userID, 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{}
|
|
userID, err := hand.Authentificate(ctx)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res, err = hand.lg.DeleteAccount(ctx, userID, 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{}
|
|
userID, err := hand.Authentificate(ctx)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res, err = hand.lg.ListAccounts(ctx, userID, 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{}
|
|
userID, err := hand.Authentificate(ctx)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res, err = hand.lg.UpdateAccount(ctx, userID, params)
|
|
return res, err
|
|
}
|