Beispiel #1
0
        private async Task BuildHeaders(Dictionary <string, string> headres, HttpRequestMessage request)
        {
            foreach (var header in _httpContextProxy.GetHeaders())
            {
                if (header.Key == CommonConst.CommonField.OAUTH_CLIENT_ID || header.Key == CommonConst.CommonField.OAUTH_CLIENT_SECRET)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
            }
            if (headres != null)
            {
                foreach (var header in headres)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
            }
            if (!request.Headers.Contains("Authorization"))
            {
                var accessToken = await _httpContextProxy.GetAccessTokenAync();

                if (!string.IsNullOrEmpty(accessToken))
                {
                    request.Headers.Add("Authorization", $"Bearer {accessToken}");
                }
            }
            if (!request.Headers.Contains(CommonConst.CommonField.TRANSACTION_ID))
            {
                request.Headers.Add(CommonConst.CommonField.TRANSACTION_ID, _logger.TransactionId);
            }
            if (!request.Headers.Contains(CommonConst.CommonField.API_AUTH_TOKEN))
            {
                request.Headers.Add(CommonConst.CommonField.API_AUTH_TOKEN, CommonUtility.GetApiAuthKey());
            }
            if (!request.Headers.Contains(CommonConst.CommonValue.TENANT_KEY))
            {
                var orgkey = _httpContextProxy.GetHeader(CommonConst.CommonValue.TENANT_KEY);
                if (string.IsNullOrEmpty(orgkey))
                {
                    orgkey = _httpContextProxy.GetQueryString(CommonConst.CommonValue.TENANT_KEY);
                }
                request.Headers.Add(CommonConst.CommonValue.TENANT_KEY, orgkey);
            }
        }
Beispiel #2
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);
            }
        }
Beispiel #3
0
        public async Task <string> AccessToken()
        {
            var token = await _httpContextProxy.GetAccessTokenAync();

            return(token);
        }