Esempio n. 1
0
        public async Task OnActionExecuting(ActionExecutingContext filterContext)
        {
            var gatewayAppConfig = AppConfig.Options.ApiGetWay;

            if (filterContext.Message.RoutePath == gatewayAppConfig.AuthorizationRoutePath)
            {
                var token = await _authorizationServerProvider.GenerateTokenCredential(new Dictionary <string, object>(filterContext.Message.Parameters));

                if (token != null)
                {
                    filterContext.Result = HttpResultMessage <object> .Create(true, token);

                    filterContext.Result.StatusCode = StatusCode.Success;
                }
                else
                {
                    filterContext.Result = new HttpResultMessage <object> {
                        IsSucceed = false, StatusCode = StatusCode.UnAuthentication, Message = "Invalid authentication credentials"
                    };
                }
            }
            else if (filterContext.Route.ServiceDescriptor.AuthType() == AuthorizationType.AppSecret.ToString())
            {
                if (!ValidateAppSecretAuthentication(filterContext, out HttpResultMessage <object> result))
                {
                    filterContext.Result = result;
                }
            }
        }
Esempio n. 2
0
        public async Task <ServiceResult <object> > Path([FromServices] IServicePartProvider servicePartProvider, string path, [FromBody] Dictionary <string, object> model)
        {
            string serviceKey = this.Request.Query["servicekey"];

            if (model == null)
            {
                model = new Dictionary <string, object>();
            }
            foreach (string n in this.Request.Query.Keys)
            {
                model[n] = this.Request.Query[n].ToString();
            }
            ServiceResult <object> result = ServiceResult <object> .Create(false, null);

            path = path.ToLower() == GateWayAppConfig.TokenEndpointPath.ToLower() ?
                   GateWayAppConfig.AuthorizationRoutePath : path.ToLower();
            if (servicePartProvider.IsPart(path))
            {
                result = ServiceResult <object> .Create(true, await servicePartProvider.Merge(path, model));

                result.StatusCode = (int)ServiceStatusCode.Success;
            }
            else
            if (OnAuthorization(path, model, ref result))
            {
                if (path == GateWayAppConfig.AuthorizationRoutePath)
                {
                    var token = await _authorizationServerProvider.GenerateTokenCredential(model);

                    if (token != null)
                    {
                        result = ServiceResult <object> .Create(true, token);

                        result.StatusCode = (int)ServiceStatusCode.Success;
                    }
                    else
                    {
                        result = new ServiceResult <object> {
                            IsSucceed = false, StatusCode = (int)ServiceStatusCode.AuthorizationFailed, Message = "Invalid authentication credentials"
                        };
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(serviceKey))
                    {
                        result = ServiceResult <object> .Create(true, await _serviceProxyProvider.Invoke <object>(model, path, serviceKey));

                        result.StatusCode = (int)ServiceStatusCode.Success;
                    }
                    else
                    {
                        result = ServiceResult <object> .Create(true, await _serviceProxyProvider.Invoke <object>(model, path));

                        result.StatusCode = (int)ServiceStatusCode.Success;
                    }
                }
            }
            return(result);
        }
Esempio n. 3
0
        public async Task <ServiceResult <object> > Path([FromServices] IServicePartProvider servicePartProvider, string path, [FromBody] Dictionary <string, object> model)
        {
            string serviceKey = this.Request.Query["servicekey"];

            path = path.IndexOf("/") < 0 ? $"/{path}" : path;
            if (model == null)
            {
                model = new Dictionary <string, object>();
            }
            foreach (string n in this.Request.Query.Keys)
            {
                model[n] = this.Request.Query[n].ToString();
            }
            ServiceResult <object> result = ServiceResult <object> .Create(false, null);

            path = String.Compare(path, GateWayAppConfig.TokenEndpointPath, true) == 0 ?
                   GateWayAppConfig.AuthorizationRoutePath : path.ToLower();
            if (await GetAllowRequest(path) == false)
            {
                return new ServiceResult <object> {
                           IsSucceed = false, StatusCode = (int)ServiceStatusCode.RequestError, Message = _servicesLocalizer["RequestError"]
                }
            }
            ;
            if (servicePartProvider.IsPart(path))
            {
                result = ServiceResult <object> .Create(true, await servicePartProvider.Merge(path, model));

                result.StatusCode = (int)ServiceStatusCode.Success;
            }
            else
            {
                var auth = await OnAuthorization(path, model);

                result = auth.Item2;
                if (auth.Item1)
                {
                    if (path == GateWayAppConfig.AuthorizationRoutePath)
                    {
                        var token = await _authorizationServerProvider.GenerateTokenCredential(model);

                        if (token != null)
                        {
                            result = ServiceResult <object> .Create(true, token);

                            result.StatusCode = (int)ServiceStatusCode.Success;
                        }
                        else
                        {
                            result = new ServiceResult <object> {
                                IsSucceed = false, StatusCode = (int)ServiceStatusCode.AuthorizationFailed, Message = "Invalid authentication credentials"
                            };
                        }
                    }
                    else
                    {
Esempio n. 4
0
        public async Task <ServiceResult <object> > Post(string path, string serviceKey, Dictionary <string, object> model)
        {
            model = model ?? new Dictionary <string, object>();
            ServiceResult <object> result = ServiceResult <object> .Create(false, null);

            path = string.IsNullOrEmpty(path) ?
                   GateWayAppConfig.AuthorizationRoutePath : path.ToLower();
            if (OnAuthorization(path, model, ref result))
            {
                if (path == GateWayAppConfig.AuthorizationRoutePath)
                {
                    var token = await _authorizationServerProvider.GenerateTokenCredential(model);

                    if (token != null)
                    {
                        result = ServiceResult <object> .Create(true, token);

                        result.StatusCode = (int)ServiceStatusCode.Success;
                    }
                    else
                    {
                        result = new ServiceResult <object> {
                            IsSucceed = false, StatusCode = (int)ServiceStatusCode.AuthorizationFailed, Message = "Invalid authentication credentials"
                        };
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(serviceKey))
                    {
                        result = ServiceResult <object> .Create(true, await _serviceProxyProvider.Invoke <object>(model, path, serviceKey));

                        result.StatusCode = (int)ServiceStatusCode.Success;
                    }
                    else
                    {
                        result = ServiceResult <object> .Create(true, await _serviceProxyProvider.Invoke <object>(model, path));

                        result.StatusCode = (int)ServiceStatusCode.Success;
                    }
                }
            }
            return(result);
        }
Esempio n. 5
0
        public async Task OnActionExecuting(ActionExecutingContext filterContext)
        {
            var gatewayAppConfig = AppConfig.Options.ApiGetWay;

            if (filterContext.Message.RoutePath == gatewayAppConfig.AuthorizationRoutePath)
            {
                var token = await _authorizationServerProvider.GenerateTokenCredential(new Dictionary <string, object>(filterContext.Message.Parameters));

                if (token != null)
                {
                    filterContext.Result = HttpResultMessage <object> .Create(true, token);

                    filterContext.Result.StatusCode = (int)ServiceStatusCode.Success;
                }
                else
                {
                    filterContext.Result = new HttpResultMessage <object> {
                        IsSucceed = false, StatusCode = (int)ServiceStatusCode.AuthorizationFailed, Message = "Invalid authentication credentials"
                    };
                }
            }
        }
Esempio n. 6
0
        public async Task <ServiceResult <object> > Path([FromServices] IServicePartProvider servicePartProvider, string path, [FromBody] Dictionary <string, object> model)
        {
            var serviceKey = this.Request.Query["servicekey"];

            if (model == null)
            {
                model = new Dictionary <string, object>();
            }
            foreach (string n in this.Request.Query.Keys)
            {
                model[n] = this.Request.Query[n].ToString();
            }
            var result = ServiceResult <object> .Create(false, null);

            path = path.ToLower();
            if (await GetAllowRequest(path) == false)
            {
                return(new ServiceResult <object> {
                    IsSucceed = false, StatusCode = (int)MessageStatusCode.RequestError, Message = "请求错误"
                });
            }
            var appConfig = GateWayAppConfig.ServicePart;

            if (servicePartProvider.IsPart(path))
            {
                var data = (string)await servicePartProvider.Merge(path, model);

                return(CreateServiceResult(data));
            }
            else
            {
                if (path == GateWayAppConfig.AuthenticationRoutePath)
                {
                    try
                    {
                        var token = await _authorizationServerProvider.GenerateTokenCredential(model);

                        if (token != null)
                        {
                            result = ServiceResult <object> .Create(true, token);

                            result.StatusCode = (int)MessageStatusCode.Ok;
                        }
                        else
                        {
                            result = new ServiceResult <object> {
                                IsSucceed = false, StatusCode = (int)MessageStatusCode.UnAuthentication, Message = "不合法的身份凭证"
                            };
                        }
                    }
                    catch (CPlatformException ex)
                    {
                        result = new ServiceResult <object> {
                            IsSucceed = false, StatusCode = (int)MessageStatusCode.CPlatformError, Message = ex.Message
                        };
                    }
                    catch (Exception ex)
                    {
                        result = new ServiceResult <object> {
                            IsSucceed = false, StatusCode = (int)MessageStatusCode.UnKnownError, Message = ex.Message
                        };
                    }
                }
                else
                {
                    if (OnAuthorization(path, model, ref result))
                    {
                        try
                        {
                            if (!string.IsNullOrEmpty(serviceKey))
                            {
                                var data = await _serviceProxyProvider.Invoke <object>(model, path, serviceKey);

                                return(CreateServiceResult(data));
                            }
                            else
                            {
                                var data = await _serviceProxyProvider.Invoke <object>(model, path);

                                if (data == null)
                                {
                                    return(new ServiceResult <object> {
                                        IsSucceed = false, StatusCode = (int)MessageStatusCode.UnKnownError, Message = "服务异常"
                                    });
                                }
                                return(CreateServiceResult(data));
                            }
                        }
                        catch (CPlatformException ex)
                        {
                            return(new ServiceResult <object> {
                                IsSucceed = false, StatusCode = (int)ex.ExceptionCode, Message = ex.Message
                            });
                        }
                        catch (Exception ex)
                        {
                            return(new ServiceResult <object> {
                                IsSucceed = false, StatusCode = (int)MessageStatusCode.UnKnownError, Message = ex.Message
                            });
                        }
                    }
                }
            }

            return(result);
        }
        public async Task <Surging.Core.ApiGateWay.ServiceResult <object> > Path([FromServices] IServicePartProvider servicePartProvider, string path, [FromBody] Dictionary <string, object> model)
        {
            string serviceKey = this.Request.Query["servicekey"];

            path = path.IndexOf("/") < 0 ? $"/{path}" : path;
            if (model == null)
            {
                model = new Dictionary <string, object>();
            }
            foreach (string n in this.Request.Query.Keys)
            {
                model[n] = this.Request.Query[n].ToString();
            }
            Surging.Core.ApiGateWay.ServiceResult <object> result = Surging.Core.ApiGateWay.ServiceResult <object> .Create(false, null);

            path = String.Compare(path.ToLower(), GateWayAppConfig.TokenEndpointPath, true) == 0 ?
                   GateWayAppConfig.AuthorizationRoutePath : path.ToLower();
            var route = await _serviceRouteProvider.GetRouteByPathRegex(path);

            var httpMethods = route.ServiceDescriptor.HttpMethod();

            if (!string.IsNullOrEmpty(httpMethods) &&
                !httpMethods.Contains(Request.Method))
            {
                return new Surging.Core.ApiGateWay.ServiceResult <object> {
                           IsSucceed = false, StatusCode = (int)ServiceStatusCode.Http405Endpoint, Message = "405 HTTP Method Not Supported"
                }
            }
            ;
            if (!GetAllowRequest(route))
            {
                return new Surging.Core.ApiGateWay.ServiceResult <object> {
                           IsSucceed = false, StatusCode = (int)ServiceStatusCode.RequestError, Message = "Request error"
                }
            }
            ;
            if (servicePartProvider.IsPart(path))
            {
                //result = ServiceResult<object>.Create(true, await servicePartProvider.Merge(path, model));
                //result.StatusCode = (int)ServiceStatusCode.Success;
                var data = (string)await servicePartProvider.Merge(path, model);

                return(AuthenticationCommon.CreateServiceResult(data));
            }
            else
            {
                var auth = await OnAuthorization(path, route, model);

                result = auth.result;
                if (auth.isSuccess)
                {
                    if (path == GateWayAppConfig.AuthorizationRoutePath)
                    {
                        var oAuthUser = await _authorizationServerProvider.GenerateTokenCredential(model);

                        if (oAuthUser.IsSucceed)
                        {
                            result = Surging.Core.ApiGateWay.ServiceResult <object> .Create(true, oAuthUser);

                            result.StatusCode = (int)ServiceStatusCode.Success;
                        }
                        else
                        {
                            result = new Surging.Core.ApiGateWay.ServiceResult <object> {
                                IsSucceed = false, StatusCode = (int)ServiceStatusCode.AuthorizationFailed, Entity = oAuthUser
                            };
                        }
                    }
                    else
                    {
                        if (String.Compare(route.ServiceDescriptor.RoutePath, path, true) != 0)
                        {
                            var pamars = RouteTemplateSegmenter.Segment(route.ServiceDescriptor.RoutePath, path);
                            foreach (KeyValuePair <string, object> item in pamars)
                            {
                                model.Add(item.Key, item.Value);
                            }
                        }
                        if (!string.IsNullOrEmpty(serviceKey))
                        {
                            //result = ServiceResult<object>.Create(true, await _serviceProxyProvider.Invoke<object>(model, route.ServiceDescriptor.RoutePath, serviceKey));
                            //result.StatusCode = (int)ServiceStatusCode.Success;
                            var data = await _serviceProxyProvider.Invoke <object>(model, path, serviceKey);

                            return(AuthenticationCommon.CreateServiceResult(data));
                        }
                        else
                        {
                            //result = ServiceResult<object>.Create(true, await _serviceProxyProvider.Invoke<object>(model, route.ServiceDescriptor.RoutePath));
                            //result.StatusCode = (int)ServiceStatusCode.Success;
                            var data = await _serviceProxyProvider.Invoke <object>(model, path);

                            return(AuthenticationCommon.CreateServiceResult(data));
                        }
                    }
                }
            }
            return(result);
        }
Esempio n. 8
0
        public async Task <ServiceResult <object> > Path([FromServices] IServicePartProvider servicePartProvider, string path, [FromBody] Dictionary <string, object> model)
        {
            string serviceKey = this.Request.Query["servicekey"];

            path = path.IndexOf("/") < 0 ? $"/{path}" : path;
            if (model == null)
            {
                model = new Dictionary <string, object>();
            }
            foreach (string n in this.Request.Query.Keys)
            {
                model[n] = this.Request.Query[n].ToString();
            }
            ServiceResult <object> result = ServiceResult <object> .Create(false, null);

            path = String.Compare(path.ToLower(), GateWayAppConfig.TokenEndpointPath, true) == 0 ?
                   GateWayAppConfig.AuthorizationRoutePath : path.ToLower();
            var route = await _serviceRouteProvider.GetRouteByPathRegex(path);

            if (!GetAllowRequest(route))
            {
                return new ServiceResult <object> {
                           IsSucceed = false, StatusCode = (int)ServiceStatusCode.RequestError, Message = "Request error"
                }
            }
            ;
            if (servicePartProvider.IsPart(path))
            {
                result = ServiceResult <object> .Create(true, await servicePartProvider.Merge(path, model));

                result.StatusCode = (int)ServiceStatusCode.Success;
            }
            else
            {
                var auth = OnAuthorization(route, model);
                result = auth.Item2;
                if (auth.Item1)
                {
                    if (path == GateWayAppConfig.AuthorizationRoutePath)
                    {
                        var token = await _authorizationServerProvider.GenerateTokenCredential(model);

                        if (token != null)
                        {
                            result = ServiceResult <object> .Create(true, token);

                            result.StatusCode = (int)ServiceStatusCode.Success;
                        }
                        else
                        {
                            result = new ServiceResult <object> {
                                IsSucceed = false, StatusCode = (int)ServiceStatusCode.AuthorizationFailed, Message = "Invalid authentication credentials"
                            };
                        }
                    }
                    else
                    {
                        if (String.Compare(route.ServiceDescriptor.RoutePath, path, true) != 0)
                        {
                            var pamars = RouteTemplateSegmenter.Segment(route.ServiceDescriptor.RoutePath, path);
                            foreach (KeyValuePair <string, object> item in pamars)
                            {
                                model.Add(item.Key, item.Value);
                            }
                        }
                        if (!string.IsNullOrEmpty(serviceKey))
                        {
                            result = ServiceResult <object> .Create(true, await _serviceProxyProvider.Invoke <object>(model, route.ServiceDescriptor.RoutePath, serviceKey));

                            result.StatusCode = (int)ServiceStatusCode.Success;
                        }
                        else
                        {
                            result = ServiceResult <object> .Create(true, await _serviceProxyProvider.Invoke <object>(model, route.ServiceDescriptor.RoutePath));

                            result.StatusCode = (int)ServiceStatusCode.Success;
                        }
                    }
                }
            }
            return(result);
        }
        public async Task <ServiceResult <object> > Path([FromServices] IServicePartProvider servicePartProvider, string path, [FromBody] Dictionary <string, object> model)
        {
            var serviceKey = this.Request.Query["servicekey"];
            var rpcParams  = new Dictionary <string, object>();

            switch (Request.Method)
            {
            case "GET":

                foreach (string n in this.Request.Query.Keys)
                {
                    rpcParams[n] = this.Request.Query[n].ToString();
                }
                rpcParams = await ParserRpcParamsFromPath(path, rpcParams);

                break;

            case "POST":
                if (model == null || !model.Any())
                {
                    return(new ServiceResult <object> {
                        IsSucceed = false, StatusCode = MessageStatusCode.RequestError, Message = $"请使用GET方法重试"
                    });
                }
                rpcParams = model;
                break;

            default:
                return(new ServiceResult <object> {
                    IsSucceed = false, StatusCode = MessageStatusCode.RequestError, Message = $"暂不支持{Request.Method}请求方法"
                });
            }


            var result = ServiceResult <object> .Create(false, null);

            path = path.ToLower();
            if (await GetAllowRequest(path) == false)
            {
                return(new ServiceResult <object> {
                    IsSucceed = false, StatusCode = MessageStatusCode.RequestError, Message = "请求错误"
                });
            }
            var appConfig = GateWayAppConfig.ServicePart;

            //if (servicePartProvider.IsPart(path))
            //{
            //    var data = (string)await servicePartProvider.Merge(path, rpcParams);
            //    return CreateServiceResult(data);
            //}
            //else
            //{
            if (path == GateWayAppConfig.AuthorizationRoutePath)
            {
                try
                {
                    var token = await _authorizationServerProvider.GenerateTokenCredential(rpcParams);

                    if (token != null)
                    {
                        result = ServiceResult <object> .Create(true, token);

                        result.StatusCode = MessageStatusCode.Success;
                    }
                    else
                    {
                        result = new ServiceResult <object> {
                            IsSucceed = false, StatusCode = MessageStatusCode.UnAuthentication, Message = "不合法的身份凭证"
                        };
                    }
                }
                catch (CPlatformException ex)
                {
                    result = new ServiceResult <object> {
                        IsSucceed = false, StatusCode = MessageStatusCode.RequestError, Message = ex.Message
                    };
                }
                catch (Exception ex)
                {
                    result = new ServiceResult <object> {
                        IsSucceed = false, StatusCode = MessageStatusCode.RequestError, Message = ex.Message
                    };
                }
            }
            else
            {
                if (OnAuthorization(path, rpcParams, ref result))
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(serviceKey))
                        {
                            var data = await _serviceProxyProvider.Invoke <object>(rpcParams, path, serviceKey);

                            return(CreateServiceResult(data));
                        }
                        else
                        {
                            var data = await _serviceProxyProvider.Invoke <object>(rpcParams, path);

                            if (data == null)
                            {
                                return(new ServiceResult <object> {
                                    IsSucceed = false, StatusCode = MessageStatusCode.UnAuthentication, Message = "服务异常"
                                });
                            }
                            return(CreateServiceResult(data));
                        }
                    }
                    catch (CPlatformException ex)
                    {
                        return(new ServiceResult <object> {
                            IsSucceed = false, StatusCode = MessageStatusCode.RequestError, Message = ex.Message
                        });
                    }
                    catch (Exception ex)
                    {
                        return(new ServiceResult <object> {
                            IsSucceed = false, StatusCode = MessageStatusCode.RequestError, Message = ex.Message
                        });
                    }
                }
            }

            //}

            return(result);
        }
Esempio n. 10
0
        public async Task <ServiceResult <object> > SignIn(LoginReq req)
        {
            //要注意参数类型
            var model = new Dictionary <string, object>();

            model.Add("req", JsonConvert.SerializeObject(req));
            var serviceKey = "Auth";
            ServiceResult <object> result = ServiceResult <object> .Create(false, null);

            var path = GateWayAppConfig.AuthorizationRoutePath;

            if (OnAuthorization(path, model, ref result))
            {
                if (path == GateWayAppConfig.AuthorizationRoutePath)
                {
                    var token = await _authorizationServerProvider.GenerateTokenCredential(model);

                    if (token != null)
                    {
                        //查询当前用户的权限,返回给客户端
                        var tmp      = JsonConvert.DeserializeObject <string>(_authorizationServerProvider.GetPayloadString(token));
                        var identify = JsonConvert.DeserializeObject <TokenDto>(tmp);
                        Dictionary <string, object> reqQueryUserPermission = new Dictionary <string, object>();
                        reqQueryUserPermission.Add("req", JsonConvert.SerializeObject(new
                        {
                            Identify = identify
                        }));
                        string servicePath = "api/orgapp/QueryUserPermission";
                        var    res         = await _serviceProxyProvider.Invoke <BaseListResponseDto>(reqQueryUserPermission, servicePath);

                        if (res != null && res.OperateFlag)
                        {
                            result = ServiceResult <object> .Create(true, new { token = token, auth = res.Result });

                            result.StatusCode = (int)ServiceStatusCode.Success;
                        }
                        else
                        {
                            result = new ServiceResult <object> {
                                IsSucceed = false, StatusCode = (int)ServiceStatusCode.AuthorizationFailed, Message = "Invalid authentication credentials"
                            };
                        }
                    }
                    else
                    {
                        result = new ServiceResult <object> {
                            IsSucceed = false, StatusCode = (int)ServiceStatusCode.AuthorizationFailed, Message = "Invalid authentication credentials"
                        };
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(serviceKey))
                    {
                        result = ServiceResult <object> .Create(true, await _serviceProxyProvider.Invoke <object>(model, path, serviceKey));

                        result.StatusCode = (int)ServiceStatusCode.Success;
                    }
                    else
                    {
                        result = ServiceResult <object> .Create(true, await _serviceProxyProvider.Invoke <object>(model, path));

                        result.StatusCode = (int)ServiceStatusCode.Success;
                    }
                }
            }
            return(result);
        }