public async Task AuthenticateAsync_AuthenticateWithRefreshToken_WithClientCredential() { string refreshToken = "refresh"; this.authenticationProvider.CurrentAccountSession = new AccountSession { RefreshToken = refreshToken }; this.adalServiceInfo.ClientSecret = "clientSecret"; var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenByRefreshTokenAsync( It.Is <string>(token => token.Equals(refreshToken)), It.Is <ClientCredential>(credential => credential.ClientId.Equals(this.adalServiceInfo.AppId)), It.Is <string>(resource => resource.Equals(this.adalServiceInfo.ServiceResource)))).Returns(Task.FromResult(mockAuthenticationResult.Object)); await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task AuthenticateAsync_AuthenticateWithRefreshToken_WithClientCertificate() { string refreshToken = "refresh"; this.authenticationProvider.CurrentAccountSession = new AccountSession { RefreshToken = refreshToken }; this.adalServiceInfo.ClientCertificate = new X509Certificate2(@"Certs\testwebapplication.pfx", "password"); var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenByRefreshTokenAsync( It.Is <string>(token => token.Equals(refreshToken)), It.Is <ClientAssertionCertificate>(certificate => certificate.ClientId.Equals(this.adalServiceInfo.AppId) && certificate.Certificate == this.adalServiceInfo.ClientCertificate), It.Is <string>(resource => resource.Equals(serviceResourceId)))).Returns(Task.FromResult(mockAuthenticationResult.Object)); await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task AuthenticateAsync_AuthenticateSilentlyWithClientCredential() { this.serviceInfo.ServiceResource = serviceResourceId; this.serviceInfo.BaseUrl = "https://localhost"; this.serviceInfo.ClientSecret = "clientSecret"; var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is <string>(resource => resource.Equals(serviceResourceId)), It.Is <string>(clientId => clientId.Equals(this.serviceInfo.AppId)))).Throws(new Exception()); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is <string>(resource => resource.Equals(serviceResourceId)), It.Is <ClientCredential>(credential => credential.ClientId.Equals(this.serviceInfo.AppId)), UserIdentifier.AnyUser)).Returns(Task.FromResult(mockAuthenticationResult.Object)); await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task AuthenticateAsync_AuthenticateSilentlyWithClientCertificate() { this.adalServiceInfo.ServiceResource = serviceResourceId; this.adalServiceInfo.BaseUrl = "https://localhost"; this.adalServiceInfo.ClientCertificate = new X509Certificate2(@"Certs\testwebapplication.pfx", "password"); var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is <string>(resource => resource.Equals(serviceResourceId)), It.Is <string>(clientId => clientId.Equals(this.adalServiceInfo.AppId)))).Throws(new Exception()); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is <string>(resource => resource.Equals(serviceResourceId)), It.Is <ClientAssertionCertificate>(certificate => certificate.Certificate == this.adalServiceInfo.ClientCertificate && certificate.ClientId == this.adalServiceInfo.AppId), UserIdentifier.AnyUser)).Returns(Task.FromResult(mockAuthenticationResult.Object)); await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task AuthenticateAsync_AuthenticateWithDiscoveryService() { const string serviceResourceId = "https://localhost/resource/"; var authenticationResult = new MockAuthenticationResult { AccessToken = "token", AccessTokenType = "type", ExpiresOn = DateTimeOffset.UtcNow, Status = AuthenticationStatus.Success, }; await this.AuthenticateAsync_AuthenticateWithDiscoveryService( authenticationResult, (string resource, string clientId, Uri redirectUri) => { switch (resource) { case (serviceResourceId): return(authenticationResult); case (Constants.Authentication.ActiveDirectoryDiscoveryResource): return(new MockAuthenticationResult { AccessToken = "discoveryServiceToken" }); default: return(null); } }, (string resource, string clientId) => { throw new Exception(); }); }
public async Task AuthenticateAsync_AuthenticateSilentlyWithoutDiscoveryService_Base(bool setUserSignInName) { if (setUserSignInName) { this.serviceInfo.UserId = "email"; } var silentAuthenticationResult = new MockAuthenticationResult { AccessToken = "token", AccessTokenType = "type", ExpiresOn = DateTimeOffset.UtcNow, Status = AuthenticationStatus.Success, }; await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( silentAuthenticationResult, null, (string resource, string clientId, UserIdentifier userIdentifier) => { if (setUserSignInName) { Assert.AreEqual(this.serviceInfo.UserId, userIdentifier.Id, "Unexpected user identifier."); } else { Assert.AreEqual(UserIdentifier.AnyUser, userIdentifier, "Unexpected user identifier."); } return(silentAuthenticationResult); }); }
public async Task AuthenticateResourceAsync_NullAuthenticationResult() { this.adalServiceInfo.ClientSecret = "clientSecret"; var resource = "https://resource.sharepoint.com/"; var expectedAuthenticationResult = new MockAuthenticationResult(); var innerException = new Exception(); this.authenticationContextWrapper .Setup(wrapper => wrapper.AcquireTokenByAuthorizationCodeAsync( It.IsAny <string>(), It.IsAny <Uri>(), It.IsAny <ClientCredential>(), It.IsAny <string>())) .Returns(Task.FromResult <IAuthenticationResult>(null)); try { var authenticationResult = await this.authenticationProvider.AuthenticateResourceAsyncWrapper(resource); } catch (OneDriveException exception) { Assert.AreEqual(OneDriveErrorCode.AuthenticationFailure.ToString(), exception.Error.Code, "Unexpected error thrown."); Assert.AreEqual("An error occurred during Azure Active Directory authentication.", exception.Error.Message, "Unexpected error thrown."); Assert.IsNull(exception.InnerException, "Unexpected inner exception."); throw; } }
public async Task AuthenticateResourceAsync_HandleException() { var resource = "https://resource.sharepoint.com/"; var expectedAuthenticationResult = new MockAuthenticationResult(); var innerException = new Exception(); this.authenticationContextWrapper .Setup(wrapper => wrapper.AcquireTokenAsync( It.IsAny <string>(), It.IsAny <ClientAssertionCertificate>())) .Throws(innerException); try { var authenticationResult = await this.authenticationProvider.AuthenticateResourceAsyncWrapper(resource); } catch (OneDriveException exception) { Assert.AreEqual(OneDriveErrorCode.AuthenticationFailure.ToString(), exception.Error.Code, "Unexpected error thrown."); Assert.AreEqual("An error occurred during Azure Active Directory authentication.", exception.Error.Message, "Unexpected error thrown."); Assert.AreEqual(innerException, exception.InnerException, "Unexpected inner exception."); throw; } }
public async Task AuthenticateAsync_CachedCurrentAccountSessionExpiring() { var cachedAccountSession = new AccountSession { AccessToken = "expiredToken", ExpiresOnUtc = DateTimeOffset.UtcNow, }; this.authenticationProvider.CurrentAccountSession = cachedAccountSession; var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow.AddHours(1)); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is <string>(resource => resource.Equals(serviceResourceId)), It.Is <string>(clientId => clientId.Equals(this.serviceInfo.AppId)))) .Returns(Task.FromResult(mockAuthenticationResult.Object)); await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task AuthenticateAsync_AuthenticateWithoutDiscoveryService() { this.serviceInfo.ServiceResource = serviceResourceId; this.serviceInfo.BaseUrl = "https://localhost"; var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is <string>(resource => resource.Equals(serviceResourceId)), It.Is <string>(clientId => clientId.Equals(this.serviceInfo.AppId)))).Throws(new Exception()); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireToken( It.Is <string>(resource => resource.Equals(serviceResourceId)), It.Is <string>(clientId => clientId.Equals(this.serviceInfo.AppId)), It.Is <Uri>(returnUri => returnUri.ToString().Equals(this.serviceInfo.ReturnUrl)), PromptBehavior.Always)).Returns(mockAuthenticationResult.Object); await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task <AccountSession> AuthenticateWithDiscoveryService( MockAuthenticationContextWrapper mockAuthenticationContextWrapper, DiscoveryServiceResponse discoveryServiceResponse = null) { var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("discoveryResource"); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is <string>(resource => resource.Equals(Constants.Authentication.ActiveDirectoryDiscoveryResource)), It.Is <string>(clientId => clientId.Equals(this.serviceInfo.AppId)))).Throws(new Exception()); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireToken( It.Is <string>(resource => resource.Equals(Constants.Authentication.ActiveDirectoryDiscoveryResource)), It.Is <string>(clientId => clientId.Equals(this.serviceInfo.AppId)), It.Is <Uri>(returnUri => returnUri.ToString().Equals(this.serviceInfo.ReturnUrl)), PromptBehavior.Auto, UserIdentifier.AnyUser)).Returns(mockAuthenticationResult.Object); if (discoveryServiceResponse == null) { discoveryServiceResponse = new DiscoveryServiceResponse { Value = new List <DiscoveryService> { new DiscoveryService { Capability = Constants.Authentication.MyFilesCapability, ServiceApiVersion = this.serviceInfo.OneDriveServiceEndpointVersion, ServiceEndpointUri = serviceEndpointUri, ServiceResourceId = serviceResourceId, } } }; } var requestBodyString = this.serializer.SerializeObject(discoveryServiceResponse); AccountSession accountSession; using (var stringContent = new StringContent(requestBodyString)) { this.httpResponseMessage.Content = stringContent; this.authenticationProvider.authenticationContextWrapper = mockAuthenticationContextWrapper.Object; accountSession = await this.authenticationProvider.AuthenticateAsync(); } return(accountSession); }
public async Task AuthenticateAsync_AuthenticateWithDiscoveryService_Base(bool setUserSignInName) { const string serviceResourceId = "https://localhost/resource/"; if (setUserSignInName) { this.serviceInfo.UserId = "email"; } var authenticationResult = new MockAuthenticationResult { AccessToken = "token", AccessTokenType = "type", ExpiresOn = DateTimeOffset.UtcNow, Status = AuthenticationStatus.Success, }; await this.AuthenticateAsync_AuthenticateWithDiscoveryService( authenticationResult, (string resource, string clientId, Uri redirectUri, UserIdentifier userIdentifier) => { if (setUserSignInName) { Assert.AreEqual(this.serviceInfo.UserId, userIdentifier.Id, "Unexpected user identifier."); } else { Assert.AreEqual(UserIdentifier.AnyUser, userIdentifier, "Unexpected user identifier."); } switch (resource) { case (serviceResourceId): return(authenticationResult); case (Constants.Authentication.ActiveDirectoryDiscoveryResource): return(new MockAuthenticationResult { AccessToken = "discoveryServiceToken" }); default: return(null); } }, (string resource, string clientId, UserIdentifier userIdentifier) => { throw new Exception(); }); }
public async Task AuthenticateResourceAsync() { var resource = "https://resource.sharepoint.com/"; var expectedAuthenticationResult = new MockAuthenticationResult(); this.authenticationContextWrapper .Setup(wrapper => wrapper.AcquireTokenAsync( It.Is<string>(resourceValue => resource.Equals(resourceValue)), It.Is<ClientAssertionCertificate>(certificate => certificate.Certificate == this.clientCertificate && this.adalServiceInfo.AppId.Equals(certificate.ClientId)))) .Returns(Task.FromResult(expectedAuthenticationResult.Object)); var authenticationResult = await this.authenticationProvider.AuthenticateResourceAsyncWrapper(resource); Assert.AreEqual(expectedAuthenticationResult.Object, authenticationResult, "Unexpected authentication result returned."); }
public async Task AuthenticateResourceAsync() { var resource = "https://resource.sharepoint.com/"; var expectedAuthenticationResult = new MockAuthenticationResult(); this.authenticationContextWrapper .Setup(wrapper => wrapper.AcquireTokenAsync( It.Is <string>(resourceValue => resource.Equals(resourceValue)), It.Is <ClientAssertionCertificate>(certificate => certificate.Certificate == this.clientCertificate && this.adalServiceInfo.AppId.Equals(certificate.ClientId)))) .Returns(Task.FromResult(expectedAuthenticationResult.Object)); var authenticationResult = await this.authenticationProvider.AuthenticateResourceAsyncWrapper(resource); Assert.AreEqual(expectedAuthenticationResult.Object, authenticationResult, "Unexpected authentication result returned."); }
public async Task AuthenticateAsync_AuthenticateSilentlyWithDiscoveryService() { var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is <string>(resource => resource.Equals(serviceResourceId)), It.Is <string>(clientId => clientId.Equals(this.serviceInfo.AppId)))) .Returns(Task.FromResult(mockAuthenticationResult.Object)); await this.AuthenticateAsync_AuthenticateWithDiscoveryService(mockAuthenticationContextWrapper, mockAuthenticationResult.Object); }
public async Task AuthenticateAsync_AuthenticateSilentlyWithoutDiscoveryService() { var silentAuthenticationResult = new MockAuthenticationResult { AccessToken = "token", AccessTokenType = "type", ExpiresOn = DateTimeOffset.UtcNow, Status = AuthenticationStatus.Success, }; await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( silentAuthenticationResult, null, (string resource, string clientId) => { return(silentAuthenticationResult); }); }
public async Task AuthenticateAsync_AuthenticateWithClientCertificate() { this.adalServiceInfo.ServiceResource = serviceResourceId; this.adalServiceInfo.BaseUrl = "https://localhost"; this.adalServiceInfo.ClientCertificate = new X509Certificate2(@"Certs\testwebapplication.pfx", "password"); var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is <string>(resource => resource.Equals(serviceResourceId)), It.Is <string>(clientId => clientId.Equals(this.adalServiceInfo.AppId)))).Throws(new Exception()); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenByAuthorizationCodeAsync( It.Is <string>(code => code.Equals(Constants.Authentication.CodeKeyName)), It.Is <Uri>(returnUri => returnUri.ToString().Equals(this.adalServiceInfo.ReturnUrl)), It.Is <ClientAssertionCertificate>(certificate => certificate.Certificate == this.adalServiceInfo.ClientCertificate && certificate.ClientId == this.adalServiceInfo.AppId), It.Is <string>(resource => resource.Equals(serviceResourceId)))) .Returns(Task.FromResult(mockAuthenticationResult.Object)); var webAuthenticationUi = new MockWebAuthenticationUi( new Dictionary <string, string> { { Constants.Authentication.CodeKeyName, Constants.Authentication.CodeKeyName } }); this.adalServiceInfo.WebAuthenticationUi = webAuthenticationUi.Object; await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task AuthenticateResourceAsync_ClientSecret() { this.authenticationProvider.ServiceInfo = this.serviceInfo; this.authenticationProvider.authenticationContextWrapper = this.authenticationContextWrapper.Object; this.serviceInfo.ClientSecret = "clientSecret"; var resource = "https://resource.sharepoint.com/"; var expectedAuthenticationResult = new MockAuthenticationResult(); this.authenticationContextWrapper .Setup(wrapper => wrapper.AcquireTokenByAuthorizationCodeAsync( It.Is <string>(code => authenticationCode.Equals(code)), It.Is <Uri>(returnUri => this.serviceInfo.ReturnUrl.Equals(returnUri.ToString())), It.Is <ClientCredential>(credential => this.serviceInfo.AppId.Equals(credential.ClientId)), It.Is <string>(resourceValue => resource.Equals(resourceValue)))) .Returns(Task.FromResult(expectedAuthenticationResult.Object)); var authenticationResult = await this.authenticationProvider.AuthenticateResourceAsyncWrapper(resource); Assert.AreEqual(expectedAuthenticationResult.Object, authenticationResult, "Unexpected authentication result returned."); }
public async Task AuthenticateAsync_AuthenticationError() { var authenticationResult = new MockAuthenticationResult { Error = "error", ErrorDescription = "errorDescription", Status = AuthenticationStatus.ServiceError, }; bool oneDriveExceptionThrown = false; try { await this.AuthenticateWithDiscoveryService( (string resource, string clientId, Uri redirectUri) => { return(authenticationResult); }, (string resource, string clientId) => { throw new Exception(); }); } catch (OneDriveException exception) { Assert.IsNotNull(exception.Error, "Error not set in exception."); Assert.AreEqual(OneDriveErrorCode.AuthenticationFailure.ToString(), exception.Error.Code, "Unexpected error code returned."); Assert.AreEqual( string.Format("An error occurred during active directory authentication. Error: {0}. Description: {1}", authenticationResult.Error, authenticationResult.ErrorDescription), exception.Error.Message, "Unexpected error message returned."); oneDriveExceptionThrown = true; } Assert.IsTrue(oneDriveExceptionThrown, "OneDriveException not thrown."); }
public async Task AuthenticateResourceAsync_ClientCertificate() { var resource = "https://resource.sharepoint.com/"; var expectedAuthenticationResult = new MockAuthenticationResult(); var clientCertificate = new X509Certificate2(@"Certs\testwebapplication.pfx", "password"); this.adalServiceInfo.ClientCertificate = clientCertificate; this.authenticationContextWrapper .Setup(wrapper => wrapper.AcquireTokenByAuthorizationCodeAsync( It.Is<string>(code => authenticationCode.Equals(code)), It.Is<Uri>(returnUri => this.adalServiceInfo.ReturnUrl.Equals(returnUri.ToString())), It.Is<ClientAssertionCertificate>(certificate => certificate.Certificate == clientCertificate && this.adalServiceInfo.AppId.Equals(certificate.ClientId)), It.Is<string>(resourceValue => resource.Equals(resourceValue)))) .Returns(Task.FromResult(expectedAuthenticationResult.Object)); var authenticationResult = await this.authenticationProvider.AuthenticateResourceAsyncWrapper(resource); Assert.AreEqual(expectedAuthenticationResult.Object, authenticationResult, "Unexpected authentication result returned."); }
private async Task AuthenticateAsync_AuthenticateWithoutDiscoveryService_Base(bool setUserSignInName) { this.serviceInfo.ServiceResource = "https://resource/"; this.serviceInfo.BaseUrl = "https://localhost"; if (setUserSignInName) { this.serviceInfo.UserId = "email"; } var authenticationResult = new MockAuthenticationResult { AccessToken = "token", AccessTokenType = "type", ExpiresOn = DateTimeOffset.UtcNow, Status = AuthenticationStatus.Success, }; await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( authenticationResult, (string resource, string clientId, Uri redirectUri, UserIdentifier userIdentifier) => { if (setUserSignInName) { Assert.AreEqual(this.serviceInfo.UserId, userIdentifier.Id, "Unexpected user identifier."); } else { Assert.AreEqual(UserIdentifier.AnyUser, userIdentifier, "Unexpected user identifier."); } return(authenticationResult); }, (string resource, string clientId, UserIdentifier userIdentifier) => { throw new Exception(); }); }
public async Task AuthenticateResourceAsync_ClientCertificate() { var resource = "https://resource.sharepoint.com/"; var expectedAuthenticationResult = new MockAuthenticationResult(); var clientCertificate = new X509Certificate2(@"Certs\testwebapplication.pfx", "password"); this.adalServiceInfo.ClientCertificate = clientCertificate; this.authenticationContextWrapper .Setup(wrapper => wrapper.AcquireTokenByAuthorizationCodeAsync( It.Is <string>(code => authenticationCode.Equals(code)), It.Is <Uri>(returnUri => this.adalServiceInfo.ReturnUrl.Equals(returnUri.ToString())), It.Is <ClientAssertionCertificate>(certificate => certificate.Certificate == clientCertificate && this.adalServiceInfo.AppId.Equals(certificate.ClientId)), It.Is <string>(resourceValue => resource.Equals(resourceValue)))) .Returns(Task.FromResult(expectedAuthenticationResult.Object)); var authenticationResult = await this.authenticationProvider.AuthenticateResourceAsyncWrapper(resource); Assert.AreEqual(expectedAuthenticationResult.Object, authenticationResult, "Unexpected authentication result returned."); }
public async Task AuthenticateAsync_AuthenticateWithoutDiscoveryService() { this.serviceInfo.ServiceResource = "https://resource/"; this.serviceInfo.BaseUrl = "https://localhost"; var authenticationResult = new MockAuthenticationResult { AccessToken = "token", AccessTokenType = "type", ExpiresOn = DateTimeOffset.UtcNow, Status = AuthenticationStatus.Success, }; await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( authenticationResult, (string resource, string clientId, Uri redirectUri) => { return(authenticationResult); }, (string resource, string clientId) => { throw new Exception(); }); }
public async Task AuthenticateAsync_AuthenticateWithDiscoveryService_Base(bool setUserSignInName) { const string serviceResourceId = "https://localhost/resource/"; if (setUserSignInName) { this.serviceInfo.UserId = "email"; } var authenticationResult = new MockAuthenticationResult { AccessToken = "token", AccessTokenType = "type", ExpiresOn = DateTimeOffset.UtcNow, Status = AuthenticationStatus.Success, }; await this.AuthenticateAsync_AuthenticateWithDiscoveryService( authenticationResult, (string resource, string clientId, Uri redirectUri, UserIdentifier userIdentifier) => { if (setUserSignInName) { Assert.AreEqual(this.serviceInfo.UserId, userIdentifier.Id, "Unexpected user identifier."); } else { Assert.AreEqual(UserIdentifier.AnyUser, userIdentifier, "Unexpected user identifier."); } switch (resource) { case (serviceResourceId): return authenticationResult; case (Constants.Authentication.ActiveDirectoryDiscoveryResource): return new MockAuthenticationResult { AccessToken = "discoveryServiceToken" }; default: return null; } }, (string resource, string clientId, UserIdentifier userIdentifier) => { throw new Exception(); }); }
public async Task AuthenticateAsync_AuthenticateSilentlyWithoutDiscoveryService_Base(bool setUserSignInName) { if (setUserSignInName) { this.serviceInfo.UserId = "email"; } var silentAuthenticationResult = new MockAuthenticationResult { AccessToken = "token", AccessTokenType = "type", ExpiresOn = DateTimeOffset.UtcNow, Status = AuthenticationStatus.Success, }; await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( silentAuthenticationResult, null, (string resource, string clientId, UserIdentifier userIdentifier) => { if (setUserSignInName) { Assert.AreEqual(this.serviceInfo.UserId, userIdentifier.Id, "Unexpected user identifier."); } else { Assert.AreEqual(UserIdentifier.AnyUser, userIdentifier, "Unexpected user identifier."); } return silentAuthenticationResult; }, null); }
public async Task AuthenticateAsync_AuthenticationError() { var authenticationResult = new MockAuthenticationResult { Error = "error", ErrorDescription = "errorDescription", Status = AuthenticationStatus.ServiceError, }; bool oneDriveExceptionThrown = false; try { await this.AuthenticateWithDiscoveryService( (string resource, string clientId, Uri redirectUri, UserIdentifier userIdentifier) => { return authenticationResult; }, (string resource, string clientId, UserIdentifier userIdentifier) => { throw new Exception(); }); } catch (OneDriveException exception) { Assert.IsNotNull(exception.Error, "Error not set in exception."); Assert.AreEqual(OneDriveErrorCode.AuthenticationFailure.ToString(), exception.Error.Code, "Unexpected error code returned."); Assert.AreEqual( string.Format("An error occurred during Azure Active Directory authentication. Error: {0}. Description: {1}", authenticationResult.Error, authenticationResult.ErrorDescription), exception.Error.Message, "Unexpected error message returned."); oneDriveExceptionThrown = true; } Assert.IsTrue(oneDriveExceptionThrown, "OneDriveException not thrown."); }
public async Task AuthenticateResourceAsync_HandleException() { var resource = "https://resource.sharepoint.com/"; var expectedAuthenticationResult = new MockAuthenticationResult(); var innerException = new Exception(); this.authenticationContextWrapper .Setup(wrapper => wrapper.AcquireTokenAsync( It.IsAny<string>(), It.IsAny<ClientAssertionCertificate>())) .Throws(innerException); try { var authenticationResult = await this.authenticationProvider.AuthenticateResourceAsyncWrapper(resource); } catch (OneDriveException exception) { Assert.AreEqual(OneDriveErrorCode.AuthenticationFailure.ToString(), exception.Error.Code, "Unexpected error thrown."); Assert.AreEqual("An error occurred during Azure Active Directory authentication.", exception.Error.Message, "Unexpected error thrown."); Assert.AreEqual(innerException, exception.InnerException, "Unexpected inner exception."); throw; } }
public async Task AuthenticateAsync_AuthenticateSilentlyWithDiscoveryService() { const string serviceResourceId = "https://localhost/resource/"; var authenticationResult = new MockAuthenticationResult { AccessToken = "token", AccessTokenType = "type", ExpiresOn = DateTimeOffset.UtcNow, Status = AuthenticationStatus.Success, }; await this.AuthenticateAsync_AuthenticateWithDiscoveryService( authenticationResult, (string resource, string clientId, Uri redirectUri) => { switch (resource) { case (Constants.Authentication.ActiveDirectoryDiscoveryResource): return new MockAuthenticationResult { AccessToken = "discoveryServiceToken" }; default: return null; } }, (string resource, string clientId) => { switch (resource) { case (serviceResourceId): return authenticationResult; case (Constants.Authentication.ActiveDirectoryDiscoveryResource): throw new Exception(); default: return null; } }); }
public async Task<AccountSession> AuthenticateWithDiscoveryService( MockAuthenticationContextWrapper mockAuthenticationContextWrapper, DiscoveryServiceResponse discoveryServiceResponse = null) { var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("discoveryResource"); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is<string>(resource => resource.Equals(Constants.Authentication.ActiveDirectoryDiscoveryResource)), It.Is<string>(clientId => clientId.Equals(this.serviceInfo.AppId)))).Throws(new Exception()); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireToken( It.Is<string>(resource => resource.Equals(Constants.Authentication.ActiveDirectoryDiscoveryResource)), It.Is<string>(clientId => clientId.Equals(this.serviceInfo.AppId)), It.Is<Uri>(returnUri => returnUri.ToString().Equals(this.serviceInfo.ReturnUrl)), PromptBehavior.Always)).Returns(mockAuthenticationResult.Object); if (discoveryServiceResponse == null) { discoveryServiceResponse = new DiscoveryServiceResponse { Value = new List<DiscoveryService> { new DiscoveryService { Capability = Constants.Authentication.MyFilesCapability, ServiceApiVersion = this.serviceInfo.OneDriveServiceEndpointVersion, ServiceEndpointUri = serviceEndpointUri, ServiceResourceId = serviceResourceId, } } }; } var requestBodyString = this.serializer.SerializeObject(discoveryServiceResponse); AccountSession accountSession; using (var stringContent = new StringContent(requestBodyString)) { this.httpResponseMessage.Content = stringContent; this.authenticationProvider.authenticationContextWrapper = mockAuthenticationContextWrapper.Object; accountSession = await this.authenticationProvider.AuthenticateAsync(); } return accountSession; }
public async Task AuthenticateAsync_AuthenticateWithoutDiscoveryService() { this.serviceInfo.ServiceResource = serviceResourceId; this.serviceInfo.BaseUrl = "https://localhost"; var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is<string>(resource => resource.Equals(serviceResourceId)), It.Is<string>(clientId => clientId.Equals(this.serviceInfo.AppId)))).Throws(new Exception()); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireToken( It.Is<string>(resource => resource.Equals(serviceResourceId)), It.Is<string>(clientId => clientId.Equals(this.serviceInfo.AppId)), It.Is<Uri>(returnUri => returnUri.ToString().Equals(this.serviceInfo.ReturnUrl)), PromptBehavior.Always)).Returns(mockAuthenticationResult.Object); await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task AuthenticateAsync_AuthenticateWithClientCredential() { this.adalServiceInfo.ServiceResource = serviceResourceId; this.adalServiceInfo.BaseUrl = "https://localhost"; this.adalServiceInfo.ClientSecret = "clientSecret"; var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is<string>(resource => resource.Equals(serviceResourceId)), It.Is<string>(clientId => clientId.Equals(this.adalServiceInfo.AppId)))).Throws(new Exception()); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenByAuthorizationCodeAsync( It.Is<string>(code => code.Equals(Constants.Authentication.CodeKeyName)), It.Is<Uri>(returnUri => returnUri.ToString().Equals(this.adalServiceInfo.ReturnUrl)), It.Is<ClientCredential>(credential => credential.ClientId.Equals(this.adalServiceInfo.AppId)), It.Is<string>(resource => resource.Equals(serviceResourceId)))) .Returns(Task.FromResult(mockAuthenticationResult.Object)); var webAuthenticationUi = new MockWebAuthenticationUi( new Dictionary<string, string> { { Constants.Authentication.CodeKeyName, Constants.Authentication.CodeKeyName } }); this.adalServiceInfo.WebAuthenticationUi = webAuthenticationUi.Object; await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task AuthenticateResourceAsync_ClientSecret() { this.authenticationProvider.ServiceInfo = this.serviceInfo; this.authenticationProvider.authenticationContextWrapper = this.authenticationContextWrapper.Object; this.serviceInfo.ClientSecret = "clientSecret"; var resource = "https://resource.sharepoint.com/"; var expectedAuthenticationResult = new MockAuthenticationResult(); this.authenticationContextWrapper .Setup(wrapper => wrapper.AcquireTokenByAuthorizationCodeAsync( It.Is<string>(code => authenticationCode.Equals(code)), It.Is<Uri>(returnUri => this.serviceInfo.ReturnUrl.Equals(returnUri.ToString())), It.Is<ClientCredential>(credential => this.serviceInfo.AppId.Equals(credential.ClientId)), It.Is<string>(resourceValue => resource.Equals(resourceValue)))) .Returns(Task.FromResult(expectedAuthenticationResult.Object)); var authenticationResult = await this.authenticationProvider.AuthenticateResourceAsyncWrapper(resource); Assert.AreEqual(expectedAuthenticationResult.Object, authenticationResult, "Unexpected authentication result returned."); }
public async Task AuthenticateAsync_AuthenticateWithoutDiscoveryService() { this.serviceInfo.ServiceResource = "https://resource/"; this.serviceInfo.BaseUrl = "https://localhost"; var authenticationResult = new MockAuthenticationResult { AccessToken = "token", AccessTokenType = "type", ExpiresOn = DateTimeOffset.UtcNow, Status = AuthenticationStatus.Success, }; await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( authenticationResult, (string resource, string clientId, Uri redirectUri) => { return authenticationResult; }, (string resource, string clientId) => { throw new Exception(); }); }
private async Task AuthenticateAsync_AuthenticateWithoutDiscoveryService_Base(bool setUserSignInName) { this.serviceInfo.ServiceResource = "https://resource/"; this.serviceInfo.BaseUrl = "https://localhost"; if (setUserSignInName) { this.serviceInfo.UserId = "email"; } var authenticationResult = new MockAuthenticationResult { AccessToken = "token", AccessTokenType = "type", ExpiresOn = DateTimeOffset.UtcNow, Status = AuthenticationStatus.Success, }; await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( authenticationResult, (string resource, string clientId, Uri redirectUri, UserIdentifier userIdentifier) => { if (setUserSignInName) { Assert.AreEqual(this.serviceInfo.UserId, userIdentifier.Id, "Unexpected user identifier."); } else { Assert.AreEqual(UserIdentifier.AnyUser, userIdentifier, "Unexpected user identifier."); } return authenticationResult; }, (string resource, string clientId, UserIdentifier userIdentifier) => { throw new Exception(); }); }
private async Task AuthenticateAsync_AuthenticateWithoutDiscoveryService_Base( bool setUserSignInName, bool useRefreshToken) { this.serviceInfo.ServiceResource = "https://resource/"; this.serviceInfo.BaseUrl = "https://localhost"; string refreshToken = "refresh"; if (useRefreshToken) { this.authenticationProvider.CurrentAccountSession = new AccountSession { RefreshToken = refreshToken }; } if (setUserSignInName) { this.serviceInfo.UserId = "email"; } var authenticationResult = new MockAuthenticationResult { AccessToken = "token", AccessTokenType = "type", ExpiresOn = DateTimeOffset.UtcNow, Status = AuthenticationStatus.Success, }; await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( authenticationResult, (string resource, string clientId, Uri redirectUri, UserIdentifier userIdentifier) => { if (setUserSignInName) { Assert.AreEqual(this.serviceInfo.UserId, userIdentifier.Id, "Unexpected user identifier."); } else { Assert.AreEqual(UserIdentifier.AnyUser, userIdentifier, "Unexpected user identifier."); } return authenticationResult; }, (string resource, string clientId, UserIdentifier userIdentifier) => { throw new Exception(); }, (string token, string clientId, string resource) => { if (useRefreshToken) { Assert.AreEqual(refreshToken, token, "Unexpected refresh token."); Assert.AreEqual(this.serviceInfo.AppId, clientId, "Unexpected app ID."); Assert.AreEqual(this.serviceInfo.ServiceResource, resource, "Unexpected service resource."); return authenticationResult; } return null; }); }
public async Task AuthenticateAsync_AuthenticateSilentlyWithoutDiscoveryService() { var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is<string>(resource => resource.Equals(serviceResourceId)), It.Is<string>(clientId => clientId.Equals(this.serviceInfo.AppId)))) .Returns(Task.FromResult(mockAuthenticationResult.Object)); await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task AuthenticateAsync_AuthenticateWithRefreshToken_WithClientCertificate() { string refreshToken = "refresh"; this.authenticationProvider.CurrentAccountSession = new AccountSession { RefreshToken = refreshToken }; this.adalServiceInfo.ClientCertificate = new X509Certificate2(@"Certs\testwebapplication.pfx", "password"); var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenByRefreshTokenAsync( It.Is<string>(token => token.Equals(refreshToken)), It.Is<ClientAssertionCertificate>(certificate => certificate.ClientId.Equals(this.adalServiceInfo.AppId) && certificate.Certificate == this.adalServiceInfo.ClientCertificate), It.Is<string>(resource => resource.Equals(serviceResourceId)))).Returns(Task.FromResult(mockAuthenticationResult.Object)); await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task AuthenticateAsync_CachedCurrentAccountSessionExpiring() { var cachedAccountSession = new AccountSession { AccessToken = "expiredToken", ExpiresOnUtc = DateTimeOffset.UtcNow, }; this.authenticationProvider.CurrentAccountSession = cachedAccountSession; var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow.AddHours(1)); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is<string>(resource => resource.Equals(serviceResourceId)), It.Is<string>(clientId => clientId.Equals(this.serviceInfo.AppId)))) .Returns(Task.FromResult(mockAuthenticationResult.Object)); await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task AuthenticateAsync_AuthenticateWithRefreshToken_WithClientCredential() { string refreshToken = "refresh"; this.authenticationProvider.CurrentAccountSession = new AccountSession { RefreshToken = refreshToken }; this.adalServiceInfo.ClientSecret = "clientSecret"; var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenByRefreshTokenAsync( It.Is<string>(token => token.Equals(refreshToken)), It.Is<ClientCredential>(credential => credential.ClientId.Equals(this.adalServiceInfo.AppId)), It.Is<string>(resource => resource.Equals(this.adalServiceInfo.ServiceResource)))).Returns(Task.FromResult(mockAuthenticationResult.Object)); await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task AuthenticateAsync_AuthenticateSilentlyWithClientCredential() { this.serviceInfo.ServiceResource = serviceResourceId; this.serviceInfo.BaseUrl = "https://localhost"; this.serviceInfo.ClientSecret = "clientSecret"; var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is<string>(resource => resource.Equals(serviceResourceId)), It.Is<string>(clientId => clientId.Equals(this.serviceInfo.AppId)))).Throws(new Exception()); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is<string>(resource => resource.Equals(serviceResourceId)), It.Is<ClientCredential>(credential => credential.ClientId.Equals(this.serviceInfo.AppId)), UserIdentifier.AnyUser)).Returns(Task.FromResult(mockAuthenticationResult.Object)); await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task AuthenticateAsync_AuthenticateSilentlyWithClientCertificate() { this.adalServiceInfo.ServiceResource = serviceResourceId; this.adalServiceInfo.BaseUrl = "https://localhost"; this.adalServiceInfo.ClientCertificate = new X509Certificate2(@"Certs\testwebapplication.pfx", "password"); var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns("type"); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is<string>(resource => resource.Equals(serviceResourceId)), It.Is<string>(clientId => clientId.Equals(this.adalServiceInfo.AppId)))).Throws(new Exception()); mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is<string>(resource => resource.Equals(serviceResourceId)), It.Is<ClientAssertionCertificate>(certificate => certificate.Certificate == this.adalServiceInfo.ClientCertificate && certificate.ClientId == this.adalServiceInfo.AppId), UserIdentifier.AnyUser)).Returns(Task.FromResult(mockAuthenticationResult.Object)); await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( mockAuthenticationContextWrapper.Object, mockAuthenticationResult.Object); }
public async Task <BusinessServiceInformation> AuthenticateWithDiscoveryServiceAsync( DiscoveryServiceResponse discoveryServiceResponse = null, string refreshToken = null) { bool refresh = refreshToken != null; var mockAuthenticationResult = new MockAuthenticationResult(); mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token"); mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns((string)null); mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow.AddHours(1)); var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper(); if (refresh) { mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenByRefreshTokenAsync( It.Is <string>(token => token.Equals(refreshToken)), It.Is <string>(clientId => clientId.Equals(AuthenticationTestBase.ClientId)), It.Is <string>(resource => resource.Equals(OAuthConstants.ActiveDirectoryDiscoveryResource)))) .Returns(Task.FromResult(mockAuthenticationResult.Object)); } else { mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync( It.Is <string>(resource => resource.Equals(OAuthConstants.ActiveDirectoryDiscoveryResource)), It.Is <string>(clientId => clientId.Equals(AuthenticationTestBase.ClientId)), UserIdentifier.AnyUser)) .Returns(Task.FromResult(mockAuthenticationResult.Object)); } var authenticationProvider = new AdalAuthenticationProvider( AuthenticationTestBase.ClientId, AuthenticationTestBase.ReturnUrl, mockAuthenticationContextWrapper.Object); var discoveryServiceHelper = new DiscoveryServiceHelper(authenticationProvider); if (discoveryServiceResponse == null) { discoveryServiceResponse = new DiscoveryServiceResponse { Value = new List <DiscoveryService> { new DiscoveryService { Capability = "MyFiles", ServiceApiVersion = "v2.0", ServiceEndpointUri = AuthenticationTestBase.ServiceEndpointUrl, ServiceResourceId = AuthenticationTestBase.ServiceResourceId, } } }; } var requestBodyString = this.serializer.SerializeObject(discoveryServiceResponse); BusinessServiceInformation businessServiceInformation = null; using (var stringContent = new StringContent(requestBodyString)) { this.httpResponseMessage.Content = stringContent; if (refresh) { businessServiceInformation = await discoveryServiceHelper.DiscoverFilesEndpointInformationForUserWithRefreshTokenAsync( refreshToken, httpProvider : this.httpProvider.Object); } else { businessServiceInformation = await discoveryServiceHelper.DiscoverFilesEndpointInformationForUserAsync(httpProvider : this.httpProvider.Object); } } return(businessServiceInformation); }
private async Task AuthenticateAsync_AuthenticateWithoutDiscoveryService_Base( bool setUserSignInName, bool useRefreshToken) { this.serviceInfo.ServiceResource = "https://resource/"; this.serviceInfo.BaseUrl = "https://localhost"; string refreshToken = "refresh"; if (useRefreshToken) { this.authenticationProvider.CurrentAccountSession = new AccountSession { RefreshToken = refreshToken }; } if (setUserSignInName) { this.serviceInfo.UserId = "email"; } var authenticationResult = new MockAuthenticationResult { AccessToken = "token", AccessTokenType = "type", ExpiresOn = DateTimeOffset.UtcNow, Status = AuthenticationStatus.Success, }; await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( authenticationResult, (string resource, string clientId, Uri redirectUri, UserIdentifier userIdentifier) => { if (setUserSignInName) { Assert.AreEqual(this.serviceInfo.UserId, userIdentifier.Id, "Unexpected user identifier."); } else { Assert.AreEqual(UserIdentifier.AnyUser, userIdentifier, "Unexpected user identifier."); } return(authenticationResult); }, (string resource, string clientId, UserIdentifier userIdentifier) => { throw new Exception(); }, (string token, string clientId, string resource) => { if (useRefreshToken) { Assert.AreEqual(refreshToken, token, "Unexpected refresh token."); Assert.AreEqual(this.serviceInfo.AppId, clientId, "Unexpected app ID."); Assert.AreEqual(this.serviceInfo.ServiceResource, resource, "Unexpected service resource."); return(authenticationResult); } return(null); }); }
public async Task AuthenticateResourceAsync_NullAuthenticationResult() { this.adalServiceInfo.ClientSecret = "clientSecret"; var resource = "https://resource.sharepoint.com/"; var expectedAuthenticationResult = new MockAuthenticationResult(); var innerException = new Exception(); this.authenticationContextWrapper .Setup(wrapper => wrapper.AcquireTokenByAuthorizationCodeAsync( It.IsAny<string>(), It.IsAny<Uri>(), It.IsAny<ClientCredential>(), It.IsAny<string>())) .Returns(Task.FromResult<IAuthenticationResult>(null)); try { var authenticationResult = await this.authenticationProvider.AuthenticateResourceAsyncWrapper(resource); } catch (OneDriveException exception) { Assert.AreEqual(OneDriveErrorCode.AuthenticationFailure.ToString(), exception.Error.Code, "Unexpected error thrown."); Assert.AreEqual("An error occurred during Azure Active Directory authentication.", exception.Error.Message, "Unexpected error thrown."); Assert.IsNull(exception.InnerException, "Unexpected inner exception."); throw; } }
public async Task AuthenticateAsync_AuthenticateSilentlyWithoutDiscoveryService() { var silentAuthenticationResult = new MockAuthenticationResult { AccessToken = "token", AccessTokenType = "type", ExpiresOn = DateTimeOffset.UtcNow, Status = AuthenticationStatus.Success, }; await this.AuthenticateAsync_AuthenticateWithoutDiscoveryService( silentAuthenticationResult, null, (string resource, string clientId) => { return silentAuthenticationResult; }); }