protected override void InternalProcessRecord() { ADUser aduser = null; string empty = string.Empty; ResultType type = ResultType.Success; if (this.Mailbox != null) { aduser = (ADUser)base.GetDataObject(this.Mailbox); if (aduser == null && !this.AppOnly) { base.ThrowTerminatingError(new MailboxUserNotFoundException(this.Mailbox.ToString()), ErrorCategory.ObjectNotFound, null); } } if (this.AppOnly) { if (this.Mailbox == null && string.IsNullOrEmpty(this.OrganizationDomain)) { base.ThrowTerminatingError(new NoUserOrOrganiztionProvidedException(), ErrorCategory.ObjectNotFound, null); } if (this.Service == ModServiceType.EWS) { base.ThrowTerminatingError(new EwsNotSupportedException(), ErrorCategory.NotEnabled, null); } } else if (this.Mailbox == null) { base.ThrowTerminatingError(new MailboxParameterMissingException(), ErrorCategory.ObjectNotFound, null); } switch (this.Service) { case ModServiceType.EWS: type = TestOAuthConnectivityHelper.SendExchangeOAuthRequest(aduser, this.OrganizationDomain, this.TargetUri, out empty, this.AppOnly, this.UseCachedToken, this.ReloadConfig); break; case ModServiceType.AutoD: type = TestOAuthConnectivityHelper.SendAutodiscoverOAuthRequest(aduser, this.OrganizationDomain, this.TargetUri, out empty, this.AppOnly, this.UseCachedToken, this.ReloadConfig); break; case ModServiceType.Generic: type = TestOAuthConnectivityHelper.SendGenericOAuthRequest(aduser, this.OrganizationDomain, this.TargetUri, out empty, this.AppOnly, this.UseCachedToken, this.ReloadConfig); break; } ValidationResultNode sendToPipeline = new ValidationResultNode(Strings.TestApiCallUnderOauthTask(this.Service.ToString()), new LocalizedString(empty), type); base.WriteObject(sendToPipeline); }
public static ResultType SendSPOAuthRequest(ADUser user, Uri targetUri, out string diagnosticMessage, bool appOnly = false, bool useCachedToken = false, bool reloadConfig = false) { int startIndex = user.GetFederatedIdentity().Identity.IndexOf('@') + 1; string domain = user.GetFederatedIdentity().Identity.Substring(startIndex); ICredentials icredentials = TestOAuthConnectivityHelper.GetICredentials(appOnly, user, domain); OAuthCredentials oauthCredentials = icredentials as OAuthCredentials; if (icredentials == null) { diagnosticMessage = Strings.NullUserError; return(ResultType.Error); } Guid value = Guid.NewGuid(); oauthCredentials.ClientRequestId = new Guid?(value); ValidationResultCollector resultCollector = new ValidationResultCollector(); LocalConfiguration localConfiguration = LocalConfiguration.Load(resultCollector); oauthCredentials.Tracer = new TestOAuthConnectivityHelper.TaskOauthOutboundTracer(); oauthCredentials.LocalConfiguration = localConfiguration; string text = targetUri.Scheme + "://" + targetUri.Host + "/_vti_bin/listdata.svc"; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(text); httpWebRequest.Method = "GET"; httpWebRequest.ContentType = "text/xml"; httpWebRequest.Headers.Add("X-ExCompId", "OauthPartnerProbe:"); httpWebRequest.Headers.Add("client-request-id", value.ToString()); httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Bearer"); httpWebRequest.UserAgent = "AMProbe/OAUTH/Sharepoint"; httpWebRequest.Credentials = icredentials; httpWebRequest.PreAuthenticate = true; string value2 = string.Empty; StringBuilder stringBuilder = new StringBuilder(1024); stringBuilder.AppendLine(Strings.ClientRequestId(value.ToString())); string value3 = TestOAuthConnectivityHelper.CheckReloadConfig(reloadConfig); string value4 = TestOAuthConnectivityHelper.CheckUseCachedToken(useCachedToken); stringBuilder.AppendLine(value3); stringBuilder.AppendLine(value4); HttpWebResponse httpWebResponse = null; ResultType result = ResultType.Success; string text2 = string.Empty; try { stringBuilder.AppendLine(Strings.OAuthRequestEndPoint(text)); httpWebResponse = (httpWebRequest.GetResponse() as HttpWebResponse); if (httpWebResponse != null && httpWebResponse.StatusCode != HttpStatusCode.OK) { throw new Exception(Strings.HttpWebRequestFailure(httpWebResponse.StatusCode.ToString())); } using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream())) { value2 = streamReader.ReadToEnd(); } } catch (WebException ex) { text2 = ex.ToString(); result = ResultType.Error; } finally { if (httpWebResponse != null) { TestOAuthConnectivityHelper.LogHttpResponseHeaders(httpWebResponse.Headers, ref stringBuilder); } if (httpWebResponse != null) { httpWebResponse.Close(); } } stringBuilder.AppendLine(Strings.TestOutboundOauthLog); stringBuilder.AppendLine(Strings.TestOAuthResponseDetails("SharePoint")); stringBuilder.AppendLine(oauthCredentials.Tracer.ToString()); stringBuilder.AppendLine(Strings.ResponseHeader); stringBuilder.AppendLine(value2); if (text2 != string.Empty) { stringBuilder.AppendLine(Strings.ExceptionHeader); stringBuilder.AppendLine(text2); } diagnosticMessage = stringBuilder.ToString(); return(result); }
public static ResultType SendLyncOAuthRequest(ADUser user, Uri targetUri, out string diagnosticMessage, bool appOnly = false, bool useCachedToken = false, bool reloadConfig = false) { int startIndex = user.GetFederatedIdentity().Identity.IndexOf('@') + 1; string domain = user.GetFederatedIdentity().Identity.Substring(startIndex); StringBuilder stringBuilder = new StringBuilder(); ICredentials icredentials = TestOAuthConnectivityHelper.GetICredentials(appOnly, user, domain); OAuthCredentials oauthCredentials = icredentials as OAuthCredentials; string text = string.Empty; foreach (ProxyAddress proxyAddress in user.EmailAddresses) { if (proxyAddress.ToString().Contains("sip:")) { text = TestOAuthConnectivityHelper.FromSipFormat(proxyAddress.ToString()); } } if (string.IsNullOrEmpty(text)) { text = user.PrimarySmtpAddress.ToString(); } if (string.IsNullOrEmpty(text)) { diagnosticMessage = Strings.EMailAddressNotFound(user.Alias); return(ResultType.Error); } Guid value = Guid.NewGuid(); oauthCredentials.ClientRequestId = new Guid?(value); stringBuilder.AppendLine(Strings.ClientRequestId(value.ToString())); string value2 = TestOAuthConnectivityHelper.CheckReloadConfig(reloadConfig); string value3 = TestOAuthConnectivityHelper.CheckUseCachedToken(useCachedToken); stringBuilder.AppendLine(value2); stringBuilder.AppendLine(value3); ValidationResultCollector resultCollector = new ValidationResultCollector(); LocalConfiguration localConfiguration = LocalConfiguration.Load(resultCollector); oauthCredentials.Tracer = new TestOAuthConnectivityHelper.TaskOauthOutboundTracer(); oauthCredentials.LocalConfiguration = localConfiguration; LyncAnonymousAutodiscoverResult lyncAnonymousAutodiscoverResult = null; try { lyncAnonymousAutodiscoverResult = LyncAutodiscoverWorker.GetAuthenticatedAutodiscoverEndpoint(text, domain); } catch (WebException ex) { stringBuilder.AppendLine(Strings.DiagnosticsHeader); stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo); stringBuilder.AppendLine(Strings.AutodiscoverFailure); stringBuilder.AppendLine(Strings.TestOutboundOauthLog); stringBuilder.AppendLine(oauthCredentials.Tracer.ToString()); stringBuilder.AppendLine(ex.ToString()); diagnosticMessage = stringBuilder.ToString(); return(ResultType.Error); } catch (Exception ex2) { stringBuilder.AppendLine(Strings.DiagnosticsHeader); stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo); stringBuilder.AppendLine(Strings.AutodiscoverFailure); stringBuilder.AppendLine(Strings.TestOutboundOauthLog); stringBuilder.AppendLine(oauthCredentials.Tracer.ToString()); stringBuilder.AppendLine(ex2.ToString()); diagnosticMessage = stringBuilder.ToString(); return(ResultType.Error); } if (string.IsNullOrEmpty(lyncAnonymousAutodiscoverResult.AuthenticatedServerUri)) { stringBuilder.AppendLine(Strings.DiagnosticsHeader); stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo); stringBuilder.AppendLine(Strings.TestOutboundOauthLog); stringBuilder.AppendLine(Strings.NoAuthenticatedServerUri); stringBuilder.AppendLine(oauthCredentials.Tracer.ToString()); diagnosticMessage = stringBuilder.ToString(); return(ResultType.Error); } stringBuilder.AppendLine(Strings.DiagnosticsHeader); stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo); LyncAutodiscoverResult lyncAutodiscoverResult = null; try { lyncAutodiscoverResult = LyncAutodiscoverWorker.GetUcwaUrl(lyncAnonymousAutodiscoverResult.AuthenticatedServerUri, icredentials); } catch (WebException ex3) { stringBuilder.AppendLine(Strings.DiagnosticsHeader); stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo); stringBuilder.AppendLine(Strings.UCWADiscoveryUrlException); stringBuilder.AppendLine(lyncAutodiscoverResult.Response); stringBuilder.AppendLine(Strings.TestOutboundOauthLog); stringBuilder.AppendLine(oauthCredentials.Tracer.ToString()); stringBuilder.AppendLine(ex3.ToString()); diagnosticMessage = stringBuilder.ToString(); return(ResultType.Error); } catch (Exception ex4) { stringBuilder.AppendLine(Strings.DiagnosticsHeader); stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo); stringBuilder.AppendLine(Strings.UCWADiscoveryUrlException); stringBuilder.AppendLine(lyncAutodiscoverResult.Response); stringBuilder.AppendLine(Strings.TestOutboundOauthLog); stringBuilder.AppendLine(oauthCredentials.Tracer.ToString()); stringBuilder.AppendLine(ex4.ToString()); diagnosticMessage = stringBuilder.ToString(); return(ResultType.Error); } if (string.IsNullOrEmpty(lyncAutodiscoverResult.UcwaDiscoveryUrl)) { stringBuilder.AppendLine(Strings.DiagnosticsHeader); stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo); stringBuilder.AppendLine(Strings.TestOutboundOauthLog); stringBuilder.AppendLine(Strings.UCWADiscoveryUrlEmpty); stringBuilder.AppendLine(lyncAutodiscoverResult.Response); stringBuilder.AppendLine(oauthCredentials.Tracer.ToString()); diagnosticMessage = stringBuilder.ToString(); return(ResultType.Error); } stringBuilder.AppendLine(Strings.DiagnosticsHeader); stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo); stringBuilder.AppendLine(Strings.TestOutboundOauthLog); stringBuilder.AppendLine(oauthCredentials.Tracer.ToString()); diagnosticMessage = stringBuilder.ToString(); return(ResultType.Success); }
public static ResultType SendGenericOAuthRequest(ADUser user, string orgDomain, Uri targetUri, out string diagnosticMessage, bool appOnly = false, bool useCachedToken = false, bool reloadConfig = false) { string domain = TestOAuthConnectivityHelper.GetDomain(user, orgDomain); if (domain == null) { diagnosticMessage = Strings.NullUserError; return(ResultType.Error); } ICredentials icredentials = TestOAuthConnectivityHelper.GetICredentials(appOnly, user, domain); OAuthCredentials oauthCredentials = icredentials as OAuthCredentials; if (icredentials == null) { diagnosticMessage = Strings.NullUserError; return(ResultType.Error); } ValidationResultCollector resultCollector = new ValidationResultCollector(); LocalConfiguration localConfiguration = LocalConfiguration.Load(resultCollector); oauthCredentials.Tracer = new TestOAuthConnectivityHelper.TaskOauthOutboundTracer(); oauthCredentials.LocalConfiguration = localConfiguration; Guid value = Guid.NewGuid(); oauthCredentials.ClientRequestId = new Guid?(value); HttpWebResponse httpWebResponse = null; ResultType result = ResultType.Success; string text = string.Empty; string s = string.Empty; StringBuilder stringBuilder = new StringBuilder(); string value2 = TestOAuthConnectivityHelper.CheckReloadConfig(reloadConfig); string value3 = TestOAuthConnectivityHelper.CheckUseCachedToken(useCachedToken); stringBuilder.AppendLine(value2); stringBuilder.AppendLine(value3); try { HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(targetUri.ToString())); httpWebRequest.Method = "GET"; httpWebRequest.ContentType = "text/xml"; httpWebRequest.Headers.Add("X-ExCompId", "OauthPartnerProbe:"); httpWebRequest.Headers.Add("client-request-id", value.ToString()); httpWebRequest.Headers.Add("request-id", value.ToString()); httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Bearer"); httpWebRequest.UserAgent = "AMProbe/OAUTH/Exchange"; httpWebRequest.Credentials = icredentials; httpWebResponse = (httpWebRequest.GetResponse() as HttpWebResponse); if (httpWebResponse != null && httpWebResponse.StatusCode != HttpStatusCode.OK) { throw new Exception(Strings.ResponseMessage(httpWebResponse.StatusCode.ToString())); } using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream())) { s = streamReader.ReadToEnd(); } } catch (Exception ex) { text = ex.ToString(); result = ResultType.Error; } finally { if (httpWebResponse != null) { TestOAuthConnectivityHelper.LogHttpResponseHeaders(httpWebResponse.Headers, ref stringBuilder); httpWebResponse.Close(); } } stringBuilder.AppendLine(Strings.TestOutboundOauthLog); stringBuilder.AppendLine(Strings.ClientRequestId(value.ToString())); stringBuilder.AppendLine(oauthCredentials.Tracer.ToString()); stringBuilder.AppendLine(Strings.TestOAuthResponseDetails("Generic")); stringBuilder.AppendLine(Strings.ResponseMessage(s)); if (text != string.Empty) { stringBuilder.AppendLine(Strings.ExceptionHeader); stringBuilder.AppendLine(text); } diagnosticMessage = stringBuilder.ToString(); return(result); }
public static ResultType SendExchangeOAuthRequest(ADUser user, string orgDomain, Uri targetUri, out string diagnosticMessage, bool appOnly = false, bool useCachedToken = false, bool reloadConfig = false) { string domain = TestOAuthConnectivityHelper.GetDomain(user, orgDomain); if (domain == null) { diagnosticMessage = Strings.NullUserError; return(ResultType.Error); } ICredentials icredentials = TestOAuthConnectivityHelper.GetICredentials(false, user, domain); OAuthCredentials oauthCredentials = icredentials as OAuthCredentials; if (icredentials == null) { diagnosticMessage = Strings.NullUserError; return(ResultType.Error); } string value = "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">\r\n <soap:Header>\r\n <t:RequestServerVersion Version=\"Exchange2012\"/>\r\n </soap:Header>\r\n <soap:Body>\r\n <GetFolder xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\">\r\n <FolderShape>\r\n <t:BaseShape>IdOnly</t:BaseShape>\r\n </FolderShape>\r\n <FolderIds>\r\n <t:DistinguishedFolderId Id=\"inbox\"/>\r\n </FolderIds>\r\n </GetFolder>\r\n </soap:Body>\r\n </soap:Envelope>"; ValidationResultCollector resultCollector = new ValidationResultCollector(); LocalConfiguration localConfiguration = LocalConfiguration.Load(resultCollector); oauthCredentials.Tracer = new TestOAuthConnectivityHelper.TaskOauthOutboundTracer(); oauthCredentials.LocalConfiguration = localConfiguration; Guid value2 = Guid.NewGuid(); oauthCredentials.ClientRequestId = new Guid?(value2); HttpWebResponse httpWebResponse = null; ResultType result = ResultType.Success; string text = string.Empty; string s = string.Empty; StringBuilder stringBuilder = new StringBuilder(); string value3 = TestOAuthConnectivityHelper.CheckReloadConfig(reloadConfig); string value4 = TestOAuthConnectivityHelper.CheckUseCachedToken(useCachedToken); stringBuilder.AppendLine(value3); stringBuilder.AppendLine(value4); try { HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(targetUri.Scheme + "://" + targetUri.Host + "/ews/Exchange.asmx")); httpWebRequest.Method = "POST"; httpWebRequest.ContentType = "text/xml"; httpWebRequest.Headers.Add("X-ExCompId", "OauthPartnerProbe:"); httpWebRequest.Headers.Add("client-request-id", value2.ToString()); httpWebRequest.Headers.Add("request-id", value2.ToString()); httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Bearer"); httpWebRequest.UserAgent = "AMProbe/OAUTH/Exchange"; httpWebRequest.Credentials = icredentials; StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()); streamWriter.Write(value); streamWriter.Close(); httpWebResponse = (httpWebRequest.GetResponse() as HttpWebResponse); if (httpWebResponse != null && httpWebResponse.StatusCode != HttpStatusCode.OK) { throw new Exception(Strings.HttpWebRequestFailure(httpWebResponse.StatusCode.ToString())); } using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream())) { s = streamReader.ReadToEnd(); } } catch (Exception ex) { text = ex.ToString(); result = ResultType.Error; } finally { if (httpWebResponse != null) { TestOAuthConnectivityHelper.LogHttpResponseHeaders(httpWebResponse.Headers, ref stringBuilder); httpWebResponse.Close(); } } stringBuilder.AppendLine(Strings.TestOutboundOauthLog); stringBuilder.AppendLine(Strings.ClientRequestId(value2.ToString())); stringBuilder.AppendLine(oauthCredentials.Tracer.ToString()); stringBuilder.AppendLine(Strings.TestOAuthResponseDetails("Exchange")); stringBuilder.AppendLine(Strings.ResponseMessage(s)); if (text != string.Empty) { stringBuilder.AppendLine(Strings.ExceptionHeader); stringBuilder.AppendLine(text); } diagnosticMessage = stringBuilder.ToString(); return(result); }
public static ResultType SendAutodiscoverOAuthRequest(ADUser user, string orgDomain, Uri targetUri, out string diagnosticMessage, bool appOnly = false, bool useCachedToken = false, bool reloadConfig = false) { string domain = TestOAuthConnectivityHelper.GetDomain(user, orgDomain); if (domain == null) { diagnosticMessage = Strings.NullUserError; return(ResultType.Error); } ICredentials icredentials = TestOAuthConnectivityHelper.GetICredentials(appOnly, user, domain); OAuthCredentials oauthCredentials = icredentials as OAuthCredentials; if (icredentials == null) { diagnosticMessage = Strings.NullUserError; return(ResultType.Error); } StringBuilder stringBuilder = new StringBuilder(); ValidationResultCollector resultCollector = new ValidationResultCollector(); LocalConfiguration localConfiguration = LocalConfiguration.Load(resultCollector); oauthCredentials.Tracer = new TestOAuthConnectivityHelper.TaskOauthOutboundTracer(); oauthCredentials.LocalConfiguration = localConfiguration; Guid value = Guid.NewGuid(); oauthCredentials.ClientRequestId = new Guid?(value); string value2 = TestOAuthConnectivityHelper.CheckReloadConfig(reloadConfig); string value3 = TestOAuthConnectivityHelper.CheckUseCachedToken(useCachedToken); stringBuilder.AppendLine(value2); stringBuilder.AppendLine(value3); AutodiscoverService autodiscoverService = new AutodiscoverService(4); autodiscoverService.Url = new Uri(targetUri.Scheme + "://" + targetUri.Host + "/autodiscover/autodiscover.svc"); autodiscoverService.TraceEnabled = true; autodiscoverService.Credentials = new OAuthCredentials(oauthCredentials); ResultType result = ResultType.Success; try { string text = (user == null) ? ("@" + domain) : user.PrimarySmtpAddress.ToString(); GetUserSettingsResponse userSettings = autodiscoverService.GetUserSettings(text, new UserSettingName[] { 58, 75 }); if (userSettings.ErrorCode != null && (userSettings.ErrorCode != 3 || !(text == "@" + domain))) { result = ResultType.Error; } } catch (Exception ex) { stringBuilder.AppendLine(ex.ToString()); result = ResultType.Error; } stringBuilder.AppendLine(Strings.TestOutboundOauthLog); stringBuilder.AppendLine(Strings.ClientRequestId(value.ToString())); stringBuilder.AppendLine(oauthCredentials.Tracer.ToString()); stringBuilder.AppendLine(Strings.TestOAuthResponseDetails("Exchange")); stringBuilder.AppendLine(Strings.TestOutboundOauthLog); stringBuilder.AppendLine(Strings.TestOAuthResponseDetails("Autodiscover")); stringBuilder.AppendLine(oauthCredentials.Tracer.ToString()); diagnosticMessage = stringBuilder.ToString(); return(result); }