Ejemplo n.º 1
0
 /// <summary>
 /// Construct a new O365Client.
 /// </summary>
 /// <param name="optionsAccessor">Provides access to the O365AuthenticationOptions configuration object.</param>
 /// <param name="adalFactory">Creates ADAL Authentication contexts.</param>
 /// <param name="backchannelFactory">Creates HttpClient backchannels</param>
 /// <param name="loggerFactory">Creates Loggers.</param>
 public O365Client(
     IOptions <O365AuthenticationOptions> optionsAccessor,
     IAdalFactory adalFactory,
     IBackchannelFactory backchannelFactory,
     ILoggerFactory loggerFactory)
 {
     _options     = optionsAccessor.Value ?? new O365AuthenticationOptions();
     _logger      = loggerFactory.CreateLogger(nameof(O365Client));
     _adalFactory = adalFactory;
     _backchannel = backchannelFactory.CreateBackchannel("https://outlook.office.com");
     _apiBaseUrl += _options.FromAddress;
 }
Ejemplo n.º 2
0
        private IO365Client CreateClient(
            IOptions <O365AuthenticationOptions> options = null,
            IAdalFactory adal = null,
            IBackchannelFactory backchannel = null,
            ILoggerFactory logger           = null
            )
        {
            if (options == null)
            {
                var optionsMock = new Mock <IOptions <O365AuthenticationOptions> >();
                optionsMock.Setup(i => i.Value)
                .Returns(
                    new O365AuthenticationOptions
                {
                    ClientId       = "LKAMFLDSNFLASDFLANDF",
                    ClientSecret   = "AKLSJFLASNDFLASNDF",
                    TenantId       = "ATAETAERADFADFGER",
                    TenantName     = "FASLALKDFNLANDF",
                    CertBytes      = File.ReadAllBytes(Directory.GetCurrentDirectory() + "/testCert.cer"),
                    CertPrivateKey = "FALSTAD",
                }
                    );
                options = optionsMock.Object;
            }

            if (adal == null)
            {
                var authenticationResult = new Mock <IAuthenticationResultAdapter>();
                authenticationResult.Setup(result => result.AccessToken)
                .Returns("Access Token");
                var authenticationContext = new Mock <IAuthenticationContextAdapter>();
                authenticationContext
                .Setup(context => context.AcquireTokenAsync(
                           It.IsAny <string>(),
                           It.IsAny <ClientCredential>()
                           ))
                .Returns(Task.FromResult(authenticationResult.Object));
                authenticationContext
                .Setup(context => context.AcquireTokenAsync(
                           It.IsAny <string>(),
                           It.IsAny <ClientAssertionCertificate>()
                           ))
                .Returns(Task.FromResult(authenticationResult.Object));
                var adalFactory = new Mock <IAdalFactory>();
                adalFactory.Setup(factory => factory.CreateAuthenticationContext(It.IsAny <string>()))
                .Returns(authenticationContext.Object);
                adal = adalFactory.Object;
            }

            if (backchannel == null)
            {
                var http = new Mock <IHttpClientAdapter>();
                http.Setup(h => h.SendAsync(It.IsAny <HttpRequestMessage>()))
                .ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.OK));
                var backchannelFactory = new Mock <IBackchannelFactory>();
                backchannelFactory.Setup(b => b.CreateBackchannel(It.IsAny <string>()))
                .Returns(http.Object);
                backchannel = backchannelFactory.Object;
            }

            if (logger == null)
            {
                logger = MockLoggerFactory();
            }

            return(new O365Client(
                       options,
                       adal,
                       backchannel,
                       logger
                       ));
        }