Ejemplo n.º 1
0
        private List <T> GetCacheValue <T>(string tablename, JObject filter = null) where T : class
        {
            if (filter == null)
            {
                filter = new JObject();
            }

            var dbroles = _inMemoryCacheService.Get <List <T> >($"{cacheprefix}-{tablename}-{filter.ToString()}");

            if (dbroles == null)
            {
                dbroles = GetDBValueAddToCache <T>(tablename, null, filter);
            }
            return(dbroles);
        }
Ejemplo n.º 2
0
        private async Task <JObject> GetAllRoutes()
        {
            const string routeCacheKey = "GatewayRouteCache";
            var          routes        = _inMemoryCacheService.Get <JObject>(routeCacheKey);

            if (routes == null)
            {
                if (!string.IsNullOrEmpty(ApplicationConfig.ApiGatewayEndpoint))
                {
                    routes = await CallAsync(CommonConst.ActionMethods.GET, "/gateway/routes", "", null, null, ApplicationConfig.ApiGatewayEndpoint);

                    _inMemoryCacheService.Put <JObject>(routeCacheKey, routes);
                }
            }

            return(routes);
        }
Ejemplo n.º 3
0
        public virtual OAuthClient GetClient(string clientId)
        {
            var client = _inMemoryCacheService.Get <OAuthClient>($"{cachePrefix}{clientId}");

            if (client == null)
            {
                var cln = SSOConfig.GetClients().FirstOrDefault(f => f.ClientId == clientId);
                if (cln != null)
                {
                    client = new OAuthClient {
                        Client = cln
                    };
                }
            }
            if (client == null)
            {
                client = FetchClient(clientId);
            }
            return(client);
        }
Ejemplo n.º 4
0
        public virtual bool AuthorizedRoute(HttpContext context, RoutingModel route, IAuthorizationService authorizationService)
        {
            var ssourl = CommonUtility.GetAppConfigValue(CommonConst.CommonValue.SSOURL_CONFIG_KEY);

            if (!route.auth_users.Where(f => f == CommonConst.CommonValue.ACCESS_ALL).Any() && !string.IsNullOrEmpty(ssourl))
            {
                try
                {
                    if (route.auth_users.IndexOf(CommonConst.CommonField.API_AUTH_TOKEN) != -1)
                    {
                        var api_access_key = _httpContextProxy.GetHeader(CommonConst.CommonField.API_AUTH_TOKEN);
                        return(api_access_key == CommonUtility.GetApiAuthKey());
                    }

                    var oauthclient = context.Request.Headers[CommonConst.CommonField.OAUTH_CLIENT_ID];
                    if (!string.IsNullOrEmpty(oauthclient))
                    {
                        var oauthUser = ValidateOAuthRequest(oauthclient, context, route);
                        return(oauthUser != null);
                    }

                    UserModel userModel = null;
                    userModel = _httpContextProxy.User;

                    if (userModel == null) // || (userModel != null && userModel.user_id == "auth2")
                    {
                        var accessToken = _httpContextProxy.GetAccessTokenAync().GetAwaiter().GetResult();
                        var cackeKey    = $"{accessToken}";
                        userModel = _inMemoryCacheService.Get <UserModel>(cackeKey);
                        if (userModel == null)
                        {
                            var endpoint = ApplicationConfig.AppEndpoint;
                            if (endpoint == ApplicationConfig.SSOEndpoint)
                            {
                                endpoint = ApplicationConfig.ApiGatewayEndpoint;
                            }
                            var response = _apiGatewayService.CallAsync(CommonConst.ActionMethods.GET, "~/user/getuserinfo", "", null, new Dictionary <string, string>()
                            {
                            }, endpoint).GetAwaiter().GetResult();
                            if (response["user"] != null)
                            {
                                userModel = JsonConvert.DeserializeObject <UserModel>(response["user"].ToString());
                                _inMemoryCacheService.Put <UserModel>(cackeKey, userModel);
                            }
                        }
                        if (userModel != null)
                        {
                            var identity = new ClaimsIdentity();
                            foreach (var claim in userModel.claims)
                            {
                                if (claim.Key == "roles")
                                {
                                    var roles = new List <string>();
                                    roles.AddRange(userModel.roles);
                                    identity.AddClaim(new System.Security.Claims.Claim("roles", Newtonsoft.Json.JsonConvert.SerializeObject(roles)));
                                }
                                else
                                {
                                    identity.AddClaim(new System.Security.Claims.Claim(claim.Key, claim.Value));
                                }
                            }
                            context.User = new ClaimsPrincipal(identity);
                        }
                    }

                    if (userModel != null)
                    {
                        if (userModel.tenants != null && userModel.tenants.Any())
                        {
                            context.Response.Headers[CommonConst.CommonField.TENANT_ID] = userModel.tenants.First().tenant_id;
                        }

                        var u = _httpContextProxy.User;
                        _logger.Debug($"Assign user id :{u.user_id} Claims:{string.Join(", ", u.claims.Select(f => $"{f.Key}:{f.Value}"))} OrgRoles: { string.Join(",", userModel.roles)}");

                        var hasaccess = false;

                        hasaccess = userModel.roles.Where(f => route.auth_users.IndexOf(f) != -1).Any();
                        if (!hasaccess)
                        {
                            _logger.Debug($"Access :{hasaccess}:{route.ToString()}:{  string.Join(",", route.auth_users)}");
                        }
                        return(hasaccess);
                    }
                    return(false);
                }
                catch (UnauthorizedAccessException)
                {
                    return(false);
                }
            }
            else
            {
                return(true);
            }
        }