Exemplo n.º 1
0
        /// <summary>
        /// authorization check
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public static bool CheckAuthorized(RPCRequest req)
        {
            if (!authorizedDict.TryGetValue(req.RPCFuncName, out IEnumerable <JRPCAuthorizedAttribute> attrs))
            {
                return(true);
            }

            if (_membership == null)
            {
                throw new JRPCException("The IMembership is null, cannot perform auhorization check");
            }

            if (_tokenManager == null)
            {
                throw new JRPCException("The ITokenManager is null, cannot perform auhorization check");
            }

            var account = _tokenManager.ValidateToken(req.AccessToken);

            if (string.IsNullOrWhiteSpace(account))
            {
                return(false);
            }

            req.Account = account;

            if (_membership.GetUserByAccount(account).IsAdmin)
            {
                return(true);
            }

            foreach (var attr in attrs)
            {
                if (attr.Resource == null && attr.Actions == null)
                {
                    // [JRPCAuthorized] only
                    return(true);
                }

                if (attr.Actions.Count() == 0)
                {
                    // [JRPCAuthorized("resource")
                    if (_membership.IsGranted(account, attr.Resource))
                    {
                        return(true);
                    }
                }

                // [JRPCAuthorized("resource", "action1" "action2"...)]
                foreach (var action in attr.Actions)
                {
                    if (_membership.IsGranted(account, attr.Resource, action))
                    {
                        return(true);
                    }
                }
            }

            return(true);
        }
Exemplo n.º 2
0
 public static Task <RPCResponse> ExecuteAsync(RPCRequest input)
 {
     if (funcDict.TryGetValue(input.RPCFuncName, out IRPCFunc f))
     {
         return(f.ExecuteAsync(input));
     }
     else
     {
         throw new JRPCMethodNotFoundException($"cannot find method: {input.RPCFuncName}");
     }
 }
Exemplo n.º 3
0
        public async Task<RPCResponse> ExecuteAsync(RPCRequest request)
        {
            var reply = new RPCResponse();

            if (funcInputWithRPCRequest != null)
            {
                reply.Pack(await funcInputWithRPCRequest.Invoke(request.Unpack<TInput>(), request));
            }
            else
            {
                reply.Pack(await funcInput.Invoke(request.Unpack<TInput>()));
            }

            return reply;
        }
Exemplo n.º 4
0
        public async Task<RPCResponse> ExecuteAsync(RPCRequest request)
        {
            var reply = new RPCResponse();

            if (funcNoInput != null)
            {
                await funcNoInput.Invoke();
            }
            else
            {
                await funcRPCRequest.Invoke(request);
            }

            reply.Pack(new object());

            return reply;
        }