public async Task DoNotSaveRefreshTokenInAdalCacheForMsalB2CAuthorityTestAsync() { var appConfig = new ApplicationConfiguration() { ClientId = TestConstants.ClientId, RedirectUri = TestConstants.RedirectUri, AuthorityInfo = AuthorityInfo.FromAuthorityUri(TestConstants.B2CAuthority, false) }; var serviceBundle = ServiceBundle.Create(appConfig); ITokenCacheInternal cache = new TokenCache(serviceBundle); MsalTokenResponse response = TestConstants.CreateMsalTokenResponse(); var requestParams = CreateAuthenticationRequestParameters(serviceBundle, authority: Authority.CreateAuthority(serviceBundle, TestConstants.B2CAuthority)); requestParams.TenantUpdatedCanonicalAuthority = TestConstants.AuthorityTestTenant; AddHostToInstanceCache(serviceBundle, TestConstants.ProductionPrefNetworkEnvironment); await cache.SaveTokenResponseAsync(requestParams, response).ConfigureAwait(false); Assert.AreEqual(1, cache.Accessor.GetAllRefreshTokens().Count()); Assert.AreEqual(1, cache.Accessor.GetAllAccessTokens().Count()); IDictionary <AdalTokenCacheKey, AdalResultWrapper> dictionary = AdalCacheOperations.Deserialize(serviceBundle.DefaultLogger, cache.LegacyPersistence.LoadCache()); cache.LegacyPersistence.WriteCache(AdalCacheOperations.Serialize(serviceBundle.DefaultLogger, dictionary)); // ADAL cache is empty because B2C scenario is only for MSAL Assert.AreEqual(0, dictionary.Count); }
public void B2CLoginAuthorityEndpoints() { using (var httpManager = new MockHttpManager()) { var appConfig = new ApplicationConfiguration() { HttpManager = httpManager, AuthorityInfo = AuthorityInfo.FromAuthorityUri( "https://sometenantid.b2clogin.com/tfp/6babcaad-604b-40ac-a9d7-9fd97c0b779f/b2c_1_susi/", true) }; var serviceBundle = ServiceBundle.Create(appConfig); Authority instance = Authority.CreateAuthority( "https://sometenantid.b2clogin.com/tfp/6babcaad-604b-40ac-a9d7-9fd97c0b779f/b2c_1_susi/"); Assert.IsNotNull(instance); Assert.AreEqual(instance.AuthorityInfo.AuthorityType, AuthorityType.B2C); var resolver = new AuthorityEndpointResolutionManager(serviceBundle); var endpoints = resolver.ResolveEndpointsAsync( instance.AuthorityInfo, null, new RequestContext(serviceBundle, Guid.NewGuid())) .GetAwaiter().GetResult(); Assert.AreEqual( "https://sometenantid.b2clogin.com/tfp/6babcaad-604b-40ac-a9d7-9fd97c0b779f/b2c_1_susi/oauth2/v2.0/authorize", endpoints.AuthorizationEndpoint); Assert.AreEqual( "https://sometenantid.b2clogin.com/tfp/6babcaad-604b-40ac-a9d7-9fd97c0b779f/b2c_1_susi/oauth2/v2.0/token", endpoints.TokenEndpoint); Assert.AreEqual("https://sometenantid.b2clogin.com/tfp/6babcaad-604b-40ac-a9d7-9fd97c0b779f/b2c_1_susi/oauth2/v2.0/token", endpoints.SelfSignedJwtAudience); } }
public async Task ValidateAuthorityFalse_SkipsNetworkCall_Async() { // Arrange var validationException = new MsalServiceException(MsalError.InvalidInstance, "authority validation failed"); // Inject authority in service bundle var httpManager = new MockHttpManager(); var appConfig = new ApplicationConfiguration() { HttpManager = httpManager, AuthorityInfo = AuthorityInfo.FromAuthorityUri(Authority, false) }; var serviceBundle = ServiceBundle.Create(appConfig); RequestContext requestContext = new RequestContext(serviceBundle, Guid.NewGuid()); // network fails with invalid_instance exception _networkMetadataProvider .When(x => x.GetMetadataAsync(Arg.Any <Uri>(), requestContext)) .Do(x => throw validationException); InstanceDiscoveryMetadataEntry actualResult = await _discoveryManager.GetMetadataEntryAsync( AuthorityInfo.FromAuthorityUri("https://some_env.com/tid", true), requestContext).ConfigureAwait(false); // Since the validateAuthority is set to false, proceed without alias. ValidateSingleEntryMetadata(new Uri(Authority), actualResult); }
public static Authority CreateAuthorityFromUrl(string uri) { var httpManager = new MockHttpManager(); var appConfig = new ApplicationConfiguration() { HttpManager = httpManager, AuthorityInfo = AuthorityInfo.FromAuthorityUri(uri, false) }; var serviceBundle = ServiceBundle.Create(appConfig); Authority authority = Authority.CreateAuthority( serviceBundle, uri); return(authority); }
public void B2CLoginAuthorityCreateAuthority() { using (var httpManager = new MockHttpManager()) { var appConfig = new ApplicationConfiguration() { HttpManager = httpManager, AuthorityInfo = AuthorityInfo.FromAuthorityUri(TestConstants.B2CLoginAuthority, false) }; var serviceBundle = ServiceBundle.Create(appConfig); // add mock response for tenant endpoint discovery httpManager.AddMockHandler( new MockHttpMessageHandler { ExpectedMethod = HttpMethod.Get, ExpectedUrl = "https://sometenantid.b2clogin.com/tfp/sometenantid/policy/v2.0/.well-known/openid-configuration", ResponseMessage = MockHelpers.CreateSuccessResponseMessage( File.ReadAllText(ResourceHelper.GetTestResourceRelativePath("OpenidConfiguration-B2CLogin.json"))) }); Authority instance = Authority.CreateAuthority( serviceBundle, TestConstants.B2CLoginAuthority); Assert.IsNotNull(instance); Assert.AreEqual(instance.AuthorityInfo.AuthorityType, AuthorityType.B2C); var resolver = new AuthorityEndpointResolutionManager(serviceBundle); var endpoints = resolver.ResolveEndpointsAsync( instance.AuthorityInfo, null, new RequestContext(serviceBundle, Guid.NewGuid())) .GetAwaiter().GetResult(); Assert.AreEqual( "https://sometenantid.b2clogin.com/6babcaad-604b-40ac-a9d7-9fd97c0b779f/policy/oauth2/v2.0/authorize", endpoints.AuthorizationEndpoint); Assert.AreEqual( "https://sometenantid.b2clogin.com/6babcaad-604b-40ac-a9d7-9fd97c0b779f/policy/oauth2/v2.0/token", endpoints.TokenEndpoint); Assert.AreEqual("https://sometenantid.b2clogin.com/6babcaad-604b-40ac-a9d7-9fd97c0b779f/v2.0/", endpoints.SelfSignedJwtAudience); } }
public static IServiceBundle CreateServiceBundleWithCustomHttpManager( IHttpManager httpManager, TelemetryCallback telemetryCallback = null, LogCallback logCallback = null, string authority = ClientApplicationBase.DefaultAuthority, bool isExtendedTokenLifetimeEnabled = false, bool enablePiiLogging = false, string clientId = MsalTestConstants.ClientId) { var appConfig = new ApplicationConfiguration() { ClientId = clientId, HttpManager = httpManager, RedirectUri = PlatformProxyFactory.CreatePlatformProxy(null).GetDefaultRedirectUri(clientId), TelemetryCallback = telemetryCallback, LoggingCallback = logCallback, LogLevel = LogLevel.Verbose, EnablePiiLogging = enablePiiLogging, IsExtendedTokenLifetimeEnabled = isExtendedTokenLifetimeEnabled, AuthorityInfo = AuthorityInfo.FromAuthorityUri(authority, false) }; return(ServiceBundle.Create(appConfig)); }