2024-07-25 16:02:26 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2024-09-19 18:24:06 +00:00
|
|
|
"context"
|
2024-07-25 16:02:26 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-08-02 07:43:31 +00:00
|
|
|
type RequestPermissionsCommand struct{}
|
2024-07-25 16:02:26 +00:00
|
|
|
|
|
|
|
type Permission struct {
|
|
|
|
ParentCapability string `json:"parentCapability"`
|
|
|
|
Date string `json:"date"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNoRequestPermissionsParamsFound = errors.New("no request permission params found")
|
2024-08-02 07:43:31 +00:00
|
|
|
ErrMultipleKeysFound = errors.New("multiple methodNames found in request permissions params")
|
|
|
|
ErrInvalidParamType = errors.New("invalid parameter type")
|
2024-07-25 16:02:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r *RPCRequest) getRequestPermissionsParam() (string, error) {
|
|
|
|
if r.Params == nil || len(r.Params) == 0 {
|
|
|
|
return "", ErrEmptyRPCParams
|
|
|
|
}
|
|
|
|
|
|
|
|
paramMap, ok := r.Params[0].(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return "", ErrInvalidParamType
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(paramMap) > 1 {
|
|
|
|
return "", ErrMultipleKeysFound
|
|
|
|
}
|
|
|
|
|
|
|
|
for methodName := range paramMap {
|
|
|
|
return methodName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", ErrNoRequestPermissionsParamsFound
|
|
|
|
}
|
|
|
|
|
2024-08-02 07:43:31 +00:00
|
|
|
func (c *RequestPermissionsCommand) getPermissionResponse(methodName string) Permission {
|
2024-07-25 16:02:26 +00:00
|
|
|
date := time.Now().UnixNano() / int64(time.Millisecond)
|
|
|
|
|
|
|
|
response := Permission{
|
|
|
|
ParentCapability: methodName,
|
|
|
|
Date: fmt.Sprintf("%d", date),
|
|
|
|
}
|
|
|
|
|
2024-08-02 07:43:31 +00:00
|
|
|
return response
|
2024-07-25 16:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 18:24:06 +00:00
|
|
|
func (c *RequestPermissionsCommand) Execute(ctx context.Context, request RPCRequest) (interface{}, error) {
|
2024-07-25 16:02:26 +00:00
|
|
|
err := request.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
methodName, err := request.getRequestPermissionsParam()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-08-02 07:43:31 +00:00
|
|
|
return c.getPermissionResponse(methodName), nil
|
2024-07-25 16:02:26 +00:00
|
|
|
}
|