public async Task <List <Claim> > GetDeviceInfo(string token)
        {
            Dictionary <string, object> deviceInfo;

            try
            {
                deviceInfo = await openAmIntegration.ValidateDevice(token);
            }
            catch (Exception)
            {
                throw PrincipalExceptionFactory.Create(PrincipalExceptionType.OpenAm,
                                                       LocalizationKeys.SystemUnavailable, null);
            }

            var camsClaims  = ClaimsManager.ParseClaims(deviceInfo);
            var codedClaims = new List <Claim>()
                              .TryAddClaim(ClaimsNames.Language, "en-us")               // set language to en-us...it is not provided
                              .TryAddClaim(ClaimsNames.Roles, MgiAwRole.Dt4Device)      // set role to Dt4Device
                              .TryAddClaim(ClaimsNames.DSL, DeviceSecurityLevel.Fifty); // set DSL to fifty

            var claims = camsClaims.Concat(codedClaims).ToList();

            var missingClaims = ClaimsInventoryHelper.CheckCamsDeviceClaims(claims);

            if (missingClaims.Any())
            {
                // Throw exception
                throw PrincipalExceptionFactory.Create(PrincipalExceptionType.OpenAm,
                                                       LocalizationKeys.InvalidUserProfile, missingClaims);
            }

            return(claims);
        }
        public List <Claim> GetAgentProfileClaims(string agentId, string posNumber, string password, string language, string sessionId)
        {
            var agentProfileKey          = string.Format(AuthCacheKeys.AGENTPROFILECLAIMS, sessionId);
            var agentProfileCachedClaims = cacheManager.Contains <CachedObjectResponseContainer <List <Tuple <string, string> > > >(agentProfileKey, CacheRegion.Global);

            if (agentProfileCachedClaims.Exists)
            {
                var missingClaimsInCache = ClaimsInventoryHelper.CheckAgentProfileClaims(agentProfileCachedClaims.CachedObj.DataObject.ToClaims());
                if (missingClaimsInCache.Any())
                {
                    agentProfileCachedClaims.Exists = false;
                    var agentProfileKeyFormatted = string.Format(AuthCacheKeys.AGENTPROFILEKEY, agentId, posNumber);

                    cacheManager.Remove(agentProfileKey, CacheRegion.Global);
                    cacheManager.Remove(agentProfileKeyFormatted, CacheRegion.Global);
                }
                else
                {
                    // Cache exists and there are no missing claims, use cached claims
                    return(agentProfileCachedClaims.CachedObj.DataObject.ToClaims());
                }
            }

            try
            {
                var agentProfile       = GetAgentProfile(agentId, posNumber, password, language);
                var agentProfileClaims = ParseClaimsFromAgentProfile(agentProfile);
                var missingClaims      = ClaimsInventoryHelper.CheckAgentProfileClaims(agentProfileClaims);

                if (missingClaims.Any())
                {
                    // Throw exception
                    throw PrincipalExceptionFactory.Create(PrincipalExceptionType.OpenAm,
                                                           LocalizationKeys.InvalidAgentProfile, missingClaims);
                }

                var agentProfileClaimsCacheContainer = CacheAheadHelper.PopulateCacheMetadata <List <Tuple <string, string> > >(agentProfileClaims.ToTupleList(), CachePolicies.FourHours);
                cacheManager.Save(agentProfileKey, agentProfileClaimsCacheContainer, CacheRegion.Global, CachePolicies.FourHours);

                return(agentProfileClaims);
            }
            catch (Exception ex)
            {
                if (ex is CommunicationException)
                {
                    throw PrincipalExceptionFactory.Create(PrincipalExceptionType.OpenAm,
                                                           LocalizationKeys.SystemUnavailable, null);
                }

                throw PrincipalExceptionFactory.Create(PrincipalExceptionType.OpenAm,
                                                       LocalizationKeys.InvalidAgentProfile, null);
            }
        }
        public async Task <List <Claim> > GetUserInfo(string token)
        {
            var camsClaimsKey    = string.Format(AuthCacheKeys.CAMSCLAIMS, token);
            var cachedCamsClaims = cacheManager.Contains <CachedObjectResponseContainer <List <Tuple <string, string> > > >(camsClaimsKey,
                                                                                                                            CacheRegion.Global);

            // Check if claims are missing first
            if (cachedCamsClaims.Exists)
            {
                var missingClaimsInCache = ClaimsInventoryHelper.CheckCamsClaims(cachedCamsClaims.CachedObj.DataObject.ToClaims());
                if (missingClaimsInCache.Any())
                {
                    // We have incomplete data. Invalidate cache
                    cachedCamsClaims.Exists = false;
                    cacheManager.Remove(camsClaimsKey, CacheRegion.Global);
                }
                else
                {
                    return(cachedCamsClaims.CachedObj.DataObject.ToClaims());
                }
            }

            Dictionary <string, object> userInfo = null;

            try
            {
                userInfo = await openAmIntegration.GetUserInfo(token);
            }
            catch (Exception)
            {
                throw PrincipalExceptionFactory.Create(PrincipalExceptionType.OpenAm,
                                                       LocalizationKeys.SystemUnavailable, null);
            }

            var camsClaims = ClaimsManager.ParseClaims(userInfo);

            var missingClaims = ClaimsInventoryHelper.CheckCamsClaims(camsClaims);

            if (missingClaims.Any())
            {
                // Throw exception
                throw PrincipalExceptionFactory.Create(PrincipalExceptionType.OpenAm,
                                                       LocalizationKeys.InvalidUserProfile, missingClaims);
            }

            var camsClaimsCacheContainer = CacheAheadHelper.PopulateCacheMetadata <List <Tuple <string, string> > >(camsClaims.ToTupleList(), CachePolicies.FourHours);

            cacheManager.Save(camsClaimsKey, camsClaimsCacheContainer, CacheRegion.Global,
                              CachePolicies.FourHours);

            return(camsClaims);
        }