package handler import ( "context" "mbase/pkg/mbctl" "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" ) func (hand *Handler) Authentificate(ctx context.Context) (string, error) { var err error var accountID string 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] //hand.log.Debugf("Auth pair: [%s:%s]", username, password) validated, accountID, err := hand.oper.ValidateAccount(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 *mbctl.CreateAccountParams) (*mbctl.CreateAccountResult, error) { var err error hand.log.Debugf("Handle CreateAccount call") res := &mbctl.CreateAccountResult{} accountID, err := hand.Authentificate(ctx) if err != nil { return res, err } res, err = hand.oper.CreateAccount(ctx, accountID, params) return res, err } func (hand *Handler) DeleteAccount(ctx context.Context, params *mbctl.DeleteAccountParams) (*mbctl.DeleteAccountResult, error) { var err error hand.log.Debugf("Handle DeleteAccount call") res := &mbctl.DeleteAccountResult{} accountID, err := hand.Authentificate(ctx) if err != nil { return res, err } res, err = hand.oper.DeleteAccount(ctx, accountID, params) return res, err } func (hand *Handler) ListAccounts(ctx context.Context, params *mbctl.ListAccountsParams) (*mbctl.ListAccountsResult, error) { var err error hand.log.Debugf("Handle ListAccounts call") res := &mbctl.ListAccountsResult{} accountID, err := hand.Authentificate(ctx) if err != nil { return res, err } res, err = hand.oper.ListAccounts(ctx, accountID, params) return res, err } func (hand *Handler) UpdateAccount(ctx context.Context, params *mbctl.UpdateAccountParams) (*mbctl.UpdateAccountResult, error) { var err error hand.log.Debugf("Handle UpdateAccount call") res := &mbctl.UpdateAccountResult{} accountID, err := hand.Authentificate(ctx) if err != nil { return res, err } res, err = hand.oper.UpdateAccount(ctx, accountID, params) return res, err }