示例#1
0
        public async Task CreatePackage(HttpContext context)
        {
            using (var outputStream = new StreamWriter(context.Response.Body))
            {
                using (var jsonStream = new JsonTextWriter(outputStream))
                {
                    if (!_hasAuthorization)
                    {
                        context.Response.ContentType = "application/json; charset=utf-8";

                        _serializer.Serialize(jsonStream, new { endpoints = _dataPackages, dataTypes = _typeDefinitions });

                        return;
                    }

                    var returnList = new List <JsonDataPackage>();

                    foreach (var dataPackage in _dataPackages)
                    {
                        var authorizedMethods = new List <RpcMethodInfo>();

                        foreach (var method in dataPackage.Methods)
                        {
                            var authorizations = method.Method.MethodAuthorizations;

                            if (authorizations == null || authorizations.Length == 0)
                            {
                                authorizedMethods.Add(method);
                            }
                            else
                            {
                                var methodInformation = method.Method;

                                var callContext =
                                    new CallExecutionContext(context, methodInformation.Type,
                                                             methodInformation.MethodInfo,
                                                             new RpcRequestMessage());
                                var add = true;

                                foreach (var authorization in methodInformation.MethodAuthorizations)
                                {
                                    if (!await authorization.AsyncAuthorize(callContext))
                                    {
                                        add = false;
                                        break;
                                    }
                                }

                                if (add)
                                {
                                    authorizedMethods.Add(method);
                                }
                            }
                        }

                        if (authorizedMethods.Count > 0)
                        {
                            returnList.Add(new JsonDataPackage
                            {
                                DisplayName = dataPackage.DisplayName,
                                Methods     = authorizedMethods,
                                Route       = dataPackage.Route
                            });
                        }
                    }

                    context.Response.ContentType = "application/json; charset=utf-8";

                    _serializer.Serialize(jsonStream, new { endpoints = returnList, dataTypes = _typeDefinitions });
                }
            }
        }
示例#2
0
 public void SetUp()
 {
     context = new CallExecutionContext();
 }