Esempio n. 1
0
 private void UpdateAppMetadata(string clientId, string environment, string familyId)
 {
     if (_featureFlags.IsFociEnabled)
     {
         var metadataCacheItem = new MsalAppMetadataCacheItem(clientId, environment, familyId);
         _accessor.SaveAppMetadata(metadataCacheItem);
     }
 }
        public void TestAppMetadata_SerializeDeserialize()
        {
            var    item   = new MsalAppMetadataCacheItem(TestConstants.ClientId, "env", "1");
            string asJson = item.ToJsonString();
            var    item2  = MsalAppMetadataCacheItem.FromJsonString(asJson);

            Assert.AreEqual(item, item2);
        }
        private void ValidateIsFociMember(
            ITokenCacheInternal cache,
            AuthenticationRequestParameters requestParams,
            string metadataFamilyId,
            bool?expectedResult,
            string errMessage)
        {
            // Arrange
            var metadata = new MsalAppMetadataCacheItem(TestConstants.ClientId, TestConstants.ProductionPrefCacheEnvironment, metadataFamilyId);

            cache.Accessor.SaveAppMetadata(metadata);

            // Act
            var result = cache.IsFociMemberAsync(requestParams, "1").Result;

            // Assert
            Assert.AreEqual(expectedResult, result, errMessage);
            Assert.AreEqual(1, cache.Accessor.GetAllAppMetadata().Count());
        }
        public void TestAppMetadata_Supports_AdditionalFields()
        {
            var item = new MsalAppMetadataCacheItem(TestConstants.ClientId, "env", "1");

            // Add an unknown field into the json
            var asJObject = item.ToJObject();

            AssertContainsKeys(asJObject, s_appMetadataKeys);

            asJObject["unsupported_field_name"] = "this is a value";

            // Ensure unknown field remains in the AdditionalFieldsJson block
            var item2 = MsalAppMetadataCacheItem.FromJObject(asJObject);

            Assert.AreEqual("{\r\n  \"unsupported_field_name\": \"this is a value\"\r\n}", item2.AdditionalFieldsJson);

            // Ensure additional fields make the round trip into json
            asJObject = item2.ToJObject();
            AssertContainsKeys(asJObject, s_appMetadataKeys);
            AssertContainsKeys(asJObject, new[] { "unsupported_field_name" });
        }
Esempio n. 5
0
        public void TestMsalAppMetadataCacheItemFromJsonStringEmpty()
        {
            var item = MsalAppMetadataCacheItem.FromJsonString(null);

            Assert.IsNull(item);
        }
        internal void PopulateCache(
            ITokenCacheAccessor accessor,
            string uid               = TestConstants.Uid,
            string utid              = TestConstants.Utid,
            string clientId          = TestConstants.ClientId,
            string environment       = TestConstants.ProductionPrefCacheEnvironment,
            string displayableId     = TestConstants.DisplayableId,
            string rtSecret          = TestConstants.RTSecret,
            string overridenScopes   = null,
            bool expiredAccessTokens = false,
            bool addSecondAt         = true)
        {
            string clientInfo = MockHelpers.CreateClientInfo(uid, utid);
            string homeAccId  = ClientInfo.CreateFromJson(clientInfo).ToAccountIdentifier();

            var accessTokenExpiresOn = expiredAccessTokens ?
                                       new DateTimeOffset(DateTime.UtcNow) :
                                       new DateTimeOffset(DateTime.UtcNow + TimeSpan.FromSeconds(ValidExpiresIn));

            var extendedAccessTokenExpiresOn = expiredAccessTokens ?
                                               new DateTimeOffset(DateTime.UtcNow) :
                                               new DateTimeOffset(DateTime.UtcNow + TimeSpan.FromSeconds(ValidExtendedExpiresIn));

            MsalAccessTokenCacheItem atItem = new MsalAccessTokenCacheItem(
                environment,
                clientId,
                overridenScopes ?? TestConstants.s_scope.AsSingleString(),
                utid,
                "",
                accessTokenExpiresOn,
                extendedAccessTokenExpiresOn,
                clientInfo,
                homeAccId);

            // add access token
            accessor.SaveAccessToken(atItem);

            var idTokenCacheItem = new MsalIdTokenCacheItem(
                environment,
                clientId,
                MockHelpers.CreateIdToken(TestConstants.UniqueId + "more", displayableId),
                clientInfo,
                homeAccId,
                tenantId: utid);

            accessor.SaveIdToken(idTokenCacheItem);

            // add another access token
            if (addSecondAt)
            {
                atItem = new MsalAccessTokenCacheItem(
                    environment,
                    clientId,
                    TestConstants.s_scopeForAnotherResource.AsSingleString(),
                    utid,
                    "",
                    accessTokenExpiresOn,
                    extendedAccessTokenExpiresOn,
                    clientInfo,
                    homeAccId);

                accessor.SaveAccessToken(atItem);
            }

            var accountCacheItem = new MsalAccountCacheItem(
                environment,
                null,
                clientInfo,
                homeAccId,
                null,
                displayableId,
                utid,
                null,
                null);

            accessor.SaveAccount(accountCacheItem);

            AddRefreshTokenToCache(accessor, uid, utid, clientId, environment, rtSecret);

            var appMetadataItem = new MsalAppMetadataCacheItem(
                clientId,
                environment,
                null);

            accessor.SaveAppMetadata(appMetadataItem);
        }
Esempio n. 7
0
        internal void PopulateCache(
            ITokenCacheAccessor accessor,
            string uid      = MsalTestConstants.Uid,
            string utid     = MsalTestConstants.Utid,
            string clientId = MsalTestConstants.ClientId)
        {
            MsalAccessTokenCacheItem atItem = new MsalAccessTokenCacheItem(
                MsalTestConstants.ProductionPrefCacheEnvironment,
                clientId,
                MsalTestConstants.Scope.AsSingleString(),
                utid,
                "",
                new DateTimeOffset(DateTime.UtcNow + TimeSpan.FromSeconds(ValidExpiresIn)),
                new DateTimeOffset(DateTime.UtcNow + TimeSpan.FromSeconds(ValidExtendedExpiresIn)),
                MockHelpers.CreateClientInfo(uid, utid));

            // add access token
            accessor.SaveAccessToken(atItem);

            var idTokenCacheItem = new MsalIdTokenCacheItem(
                MsalTestConstants.ProductionPrefCacheEnvironment,
                clientId,
                MockHelpers.CreateIdToken(MsalTestConstants.UniqueId + "more", MsalTestConstants.DisplayableId),
                MockHelpers.CreateClientInfo(uid, utid),
                utid);

            accessor.SaveIdToken(idTokenCacheItem);

            var accountCacheItem = new MsalAccountCacheItem(
                MsalTestConstants.ProductionPrefCacheEnvironment,
                null,
                MockHelpers.CreateClientInfo(uid, utid),
                null,
                MsalTestConstants.DisplayableId,
                utid,
                null,
                null);

            accessor.SaveAccount(accountCacheItem);

            atItem = new MsalAccessTokenCacheItem(
                MsalTestConstants.ProductionPrefCacheEnvironment,
                clientId,
                MsalTestConstants.ScopeForAnotherResource.AsSingleString(),
                utid,
                "",
                new DateTimeOffset(DateTime.UtcNow + TimeSpan.FromSeconds(ValidExpiresIn)),
                new DateTimeOffset(DateTime.UtcNow + TimeSpan.FromSeconds(ValidExtendedExpiresIn)),
                MockHelpers.CreateClientInfo(uid, utid));

            // add another access token
            accessor.SaveAccessToken(atItem);

            AddRefreshTokenToCache(accessor, uid, utid, clientId);

            var appMetadataItem = new MsalAppMetadataCacheItem(
                clientId,
                MsalTestConstants.ProductionPrefCacheEnvironment,
                null);

            accessor.SaveAppMetadata(appMetadataItem);
        }
Esempio n. 8
0
 public void WriteAppMetadata(MsalAppMetadataCacheItem appMetadata)
 {
     throw new NotImplementedException();
 }
Esempio n. 9
0
 public void SaveAppMetadata(MsalAppMetadataCacheItem item)
 {
     throw new NotImplementedException();
 }
 public void WriteAppMetadata(MsalAppMetadataCacheItem appMetadata)
 {
     //Save(appMetadata.GetKey(), appMetadata.ToJsonString());
     throw new NotImplementedException();
 }
        public void SaveAppMetadata(MsalAppMetadataCacheItem item)
        {
            string key = item.GetKey().ToString();

            AppMetadataDictionary[key] = item;
        }
        public void SaveAppMetadata(MsalAppMetadataCacheItem item)
        {
            string key = item.GetKey().ToString();

            _appMetadataDictionary.AddOrUpdate(key, item, (k, oldValue) => item);
        }
Esempio n. 13
0
 public void SaveAppMetadata(MsalAppMetadataCacheItem item)
 {
     _appMetadataDictionary[item.GetKey().ToString()] = item;
 }
        internal static void PopulateCache(
            ITokenCacheAccessor accessor,
            string uid               = TestConstants.Uid,
            string utid              = TestConstants.Utid,
            string clientId          = TestConstants.ClientId,
            string environment       = TestConstants.ProductionPrefCacheEnvironment,
            string displayableId     = TestConstants.DisplayableId,
            string rtSecret          = TestConstants.RTSecret,
            string overridenScopes   = null,
            string userAssertion     = null,
            bool expiredAccessTokens = false,
            bool addSecondAt         = true)
        {
            bool addAccessTokenOnly = accessor is InMemoryPartitionedAppTokenCacheAccessor;

            string clientInfo = MockHelpers.CreateClientInfo(uid, utid);
            string homeAccId  = ClientInfo.CreateFromJson(clientInfo).ToAccountIdentifier();

            var accessTokenExpiresOn = expiredAccessTokens ?
                                       DateTimeOffset.UtcNow :
                                       DateTimeOffset.UtcNow + TimeSpan.FromSeconds(ValidExpiresIn);

            var extendedAccessTokenExpiresOn = expiredAccessTokens ?
                                               DateTimeOffset.UtcNow :
                                               DateTimeOffset.UtcNow + TimeSpan.FromSeconds(ValidExtendedExpiresIn);

            string userAssertionHash = null;

            if (userAssertion != null)
            {
                var crypto = PlatformProxyFactory.CreatePlatformProxy(null).CryptographyManager;
                userAssertionHash = crypto.CreateBase64UrlEncodedSha256Hash(userAssertion);
            }

            MsalAccessTokenCacheItem atItem = new MsalAccessTokenCacheItem(
                environment,
                clientId,
                overridenScopes ?? TestConstants.s_scope.AsSingleString(),
                utid,
                "",
                DateTimeOffset.UtcNow,
                accessTokenExpiresOn,
                extendedAccessTokenExpiresOn,
                clientInfo,
                homeAccId,
                oboCacheKey: userAssertionHash);

            // add access token
            accessor.SaveAccessToken(atItem);

            // add another access token
            if (addSecondAt)
            {
                atItem = new MsalAccessTokenCacheItem(
                    environment,
                    clientId,
                    TestConstants.s_scopeForAnotherResource.AsSingleString(),
                    utid,
                    "",
                    DateTimeOffset.UtcNow,
                    accessTokenExpiresOn,
                    extendedAccessTokenExpiresOn,
                    clientInfo,
                    homeAccId);

                accessor.SaveAccessToken(atItem);
            }

            if (!addAccessTokenOnly)
            {
                var idTokenCacheItem = new MsalIdTokenCacheItem(
                    environment,
                    clientId,
                    MockHelpers.CreateIdToken(TestConstants.UniqueId + "more", displayableId),
                    clientInfo,
                    homeAccId,
                    tenantId: utid);

                accessor.SaveIdToken(idTokenCacheItem);

                var accountCacheItem = new MsalAccountCacheItem(
                    environment,
                    null,
                    clientInfo,
                    homeAccId,
                    null,
                    displayableId,
                    utid,
                    null,
                    null,
                    null);

                accessor.SaveAccount(accountCacheItem);

                AddRefreshTokenToCache(accessor, uid, utid, clientId, environment, rtSecret);

                var appMetadataItem = new MsalAppMetadataCacheItem(
                    clientId,
                    environment,
                    null);

                accessor.SaveAppMetadata(appMetadataItem);
            }
        }