/// <summary> /// Initializes a new instance of the <see cref="OktaClient"/> class. /// </summary> /// <param name="apiClientConfiguration"> /// The client configuration. If <c>null</c>, the library will attempt to load /// configuration from an <c>okta.yaml</c> file or environment variables. /// </param> /// <param name="logger">The logging interface to use, if any.</param> /// <param name="serializer">The JSON serializer to use, if any. Using the <c>DefaultSerializer</c> is still strongly recommended since it has all the behavior this SDK needs to work properly. /// If a custom serializer is used, the developer is responsible to add the required logic for this SDK to continue working properly. See <see cref="DefaultSerializer"/> to check out what can be configured. /// </param> public OktaClient(OktaClientConfiguration apiClientConfiguration = null, ILogger logger = null, ISerializer serializer = null) { Configuration = GetConfigurationOrDefault(apiClientConfiguration); OktaClientConfigurationValidator.Validate(Configuration); logger = logger ?? NullLogger.Instance; serializer = serializer ?? new DefaultSerializer(); var defaultClient = DefaultHttpClient.Create( Configuration.ConnectionTimeout, Configuration.Proxy, logger); var resourceFactory = new ResourceFactory(this, logger); IOAuthTokenProvider oAuthTokenProvider = (Configuration.AuthorizationMode == AuthorizationMode.PrivateKey) ? new DefaultOAuthTokenProvider(Configuration, resourceFactory, logger: logger) : NullOAuthTokenProvider.Instance; var requestExecutor = new DefaultRequestExecutor(Configuration, defaultClient, logger, oAuthTokenProvider: oAuthTokenProvider); _dataStore = new DefaultDataStore( requestExecutor, serializer, resourceFactory, logger); PayloadHandler.TryRegister <PkixCertPayloadHandler>(); PayloadHandler.TryRegister <PemFilePayloadHandler>(); PayloadHandler.TryRegister <X509CaCertPayloadHandler>(); }
public void SetHttpMessageContentTransferEncodingFromRequestInsteadOfPayloadHandler() { // the TestPayloadHandler has a ContentType of "foo" var testPayloadHandler = new TestPayloadHandler(); // register the payload handler so that it is found when the ContentType of the request is set PayloadHandler.TryRegister(testPayloadHandler); // set the ContentType of the request to match the ContentType of the TestPayloadHandler so that the request uses the TestPayloadHandler var testHttpRequest = new HttpRequest { ContentType = "foo", ContentTransferEncoding = "testContentTransferEncoding-fromRequest", }; var testContentTransferEncoding = "testContentTransferEncoding-fromPayloadHandler"; testPayloadHandler.SetContentTransferEncoding(testContentTransferEncoding); var httpRequestMessage = new HttpRequestMessage(); testPayloadHandler.SetHttpRequestMessageContent(testHttpRequest, httpRequestMessage); httpRequestMessage.Headers.Contains("Content-Transfer-Encoding").Should().BeTrue(); // the content transfer encoding set on the request should win over the content transfer encoding set on the payload handler httpRequestMessage.Headers.ToString().Should().BeOneOf( "Content-Transfer-Encoding: testContentTransferEncoding-fromRequest\n", "Content-Transfer-Encoding: testContentTransferEncoding-fromRequest\r\n"); httpRequestMessage.Content.Should().NotBeNull(); httpRequestMessage.Content.GetType().Should().Be(typeof(StringContent)); }
public async Task SetPayloadHandlerContentTypeOnPostAsync() { var mockRequestExecutor = Substitute.For <IRequestExecutor>(); mockRequestExecutor .PostAsync(Arg.Any <HttpRequest>(), Arg.Any <CancellationToken>()) .Returns(new HttpResponse <string>() { StatusCode = 200 }); var testPayloadHandler = new TestPayloadHandler(); PayloadHandler.TryRegister(testPayloadHandler); var request = new TestHttpRequest(); request.ContentType.Should().Be("application/json"); request.GetPayloadHandler().ContentType.Should().Be("application/json"); var dataStore = new DefaultDataStore(mockRequestExecutor, new DefaultSerializer(), new ResourceFactory(null, null), NullLogger.Instance); await dataStore.PostAsync <TestResource>(request, new RequestContext { ContentType = "foo" }, CancellationToken.None); request.ContentType.Should().Be("foo"); request.GetPayloadHandler().ContentType.Should().Be("foo"); }
public void SetHttpMessageContentTransferEncodingHeader() { // the TestPayloadHandler has a ContentType of "foo" var testPayloadHandler = new TestPayloadHandler(); // register the payload handler so that it is found when the ContentType of the request is set. The OktaClient will register payload handlers as appropriate. PayloadHandler.TryRegister(testPayloadHandler); // set the ContentType of the request to match the ContentType of the TestPayloadHandler so that the request uses the TestPayloadHandler var testHttpRequest = new HttpRequest { ContentType = "foo" }; var testContentTransferEncoding = "testContentTransferEncoding-fromPayloadHandler"; testPayloadHandler.SetContentTransferEncoding(testContentTransferEncoding); var httpRequestMessage = new HttpRequestMessage(); testPayloadHandler.SetHttpRequestMessageContent(testHttpRequest, httpRequestMessage); httpRequestMessage.Headers.Contains("Content-Transfer-Encoding").Should().BeTrue(); // if there is no content transfer encoding on the request then the value from the PayloadHandler is used httpRequestMessage.Headers.ToString().Should().BeOneOf( "Content-Transfer-Encoding: testContentTransferEncoding-fromPayloadHandler\n", "Content-Transfer-Encoding: testContentTransferEncoding-fromPayloadHandler\r\n"); httpRequestMessage.Content.Should().NotBeNull(); httpRequestMessage.Content.GetType().Should().Be(typeof(StringContent)); }
public void RegisterPayloadHandlers() { var testableClient = new TestableHttpClient(); var client = new OktaClient( TestableOktaClient.DefaultFakeConfiguration, testableClient); PayloadHandler.ForContentType("application/pkix-cert").GetType().Should().Be(typeof(PkixCertPayloadHandler)); PayloadHandler.ForContentType("application/x-pem-file").GetType().Should().Be(typeof(PemFilePayloadHandler)); PayloadHandler.ForContentType("application/x-x509-ca-cert").GetType().Should().Be(typeof(X509CaCertPayloadHandler)); }
public void ProcessIncomingPayloads(PayloadHandler handler) { if (processing) { throw new InvalidOperationException("Already processing."); } payload_handler = handler; processing = true; ThreadAssist.Spawn(Process); }
public void SetPayloadHandlerWhenContentTypeIsSet() { // must register PkixCertPayloadHandler for it to be available to the HttpRequest // the OktaClient will register the PkixCertPayloadHandler on instantiation. PayloadHandler.TryRegister <PkixCertPayloadHandler>(); var request = new TestHttpRequest(); request.ContentType.Should().Be("application/json"); request.GetPayloadHandler().GetType().Should().Be(typeof(JsonPayloadHandler)); request.ContentType = "application/pkix-cert"; request.GetPayloadHandler().GetType().Should().Be(typeof(PkixCertPayloadHandler)); }
public Task WaitForReply(PayloadHandler payloadHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { var actor = this.__Actor; if (ReferenceEquals(actor, null)) { throw new Exception("Can't handle response if payload.Actor is null"); } else { actor.ReplyTo += this.HandleReplyTo; var waitTask = Task.Factory.StartNew(() => { try { if (waitTimeout > 0 && !ReferenceEquals(payloadHandler, null)) { this.TimedOutWaiting = false; var startedAt = DateTime.Now; while (!this.ReplyRecieved && !this.TimedOutWaiting && DateTime.Now < startedAt.AddSeconds(waitTimeout)) { Thread.Sleep(100); } if (!this.ReplyRecieved) { this.TimedOutWaiting = true; } var errorMessageReceived = !ReferenceEquals(this.ReplyPayload, null) && !String.IsNullOrEmpty(this.ReplyPayload.ErrorMessage); if (this.ReplyRecieved && (!errorMessageReceived || ReferenceEquals(timeoutHandler, null))) { this.ReplyPayload.__Actor = actor; payloadHandler(this.ReplyPayload, this.ReplyBDEA); } else if (!ReferenceEquals(timeoutHandler, null)) { timeoutHandler(this.ReplyPayload, default(BasicDeliverEventArgs)); } } } finally { actor.ReplyTo -= this.HandleReplyTo; } }); return(waitTask); } }
protected Task SendMessage(string routingKey, StandardPayload payload, PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int timeout = StandardPayload.DEFAULT_TIMEOUT) { if (SMQActorBase.IsDebugMode) { System.Console.WriteLine(routingKey); } IBasicProperties props = this.RMQChannel.CreateBasicProperties(); props.ReplyTo = "amq.rabbitmq.reply-to"; props.CorrelationId = payload.PayloadId.ToString(); var payloadJson = JsonConvert.SerializeObject(payload); this.RMQChannel.BasicPublish(this.Microphone, routingKey, props, Encoding.UTF8.GetBytes(payloadJson)); return(payload.WaitForReply(replyHandler, timeoutHandler, timeout)); }
public void SetHttpMessageContent() { // the TestPayloadHandler has a ContentType of "foo" var testPayloadHandler = new TestPayloadHandler(); // register the payload handler so that it is found when the ContentType of the request is set. The OktaClient will register payload handlers as appropriate. PayloadHandler.TryRegister(testPayloadHandler); // set the ContentType of the request to match the ContentType of the TestPayloadHandler so that the request uses the TestPayloadHandler var testHttpRequest = new HttpRequest { ContentType = "foo" }; var httpRequestMessage = new HttpRequestMessage(); httpRequestMessage.Content.Should().BeNull(); testPayloadHandler.SetHttpRequestMessageContent(testHttpRequest, httpRequestMessage); httpRequestMessage.Content.Should().NotBeNull(); httpRequestMessage.Content.GetType().Should().Be(typeof(StringContent)); }
public void SetPkixCertContent() { var pkixCertPayloadHandler = new PkixCertPayloadHandler(); PayloadHandler.TryRegister(pkixCertPayloadHandler); var testHttpRequest = new TestHttpRequest { ContentType = "application/pkix-cert", // this must match the ContentType of the PkixCertPayloadHandler for this test Payload = "testPayload", }; testHttpRequest.GetPayloadHandler().GetType().Should().Be(typeof(PkixCertPayloadHandler)); var httpRequestMessage = new HttpRequestMessage(); pkixCertPayloadHandler.SetHttpRequestMessageContent(testHttpRequest, httpRequestMessage); httpRequestMessage.Content.Should().NotBeNull(); httpRequestMessage.Content.GetType().Should().Be(typeof(StringContent)); httpRequestMessage.Content.Headers.Contains("Content-Type"); httpRequestMessage.Content.Headers.ContentType.ToString().Should().Be("application/pkix-cert; charset=utf-8"); }
public void SetX509CertContent() { var pkixCertPayloadHandler = new X509CaCertPayloadHandler(); PayloadHandler.TryRegister(pkixCertPayloadHandler); var testHttpRequest = new TestHttpRequest { ContentType = "application/x-x509-ca-cert", // this must match the ContentType of the X509CaCertPayloadHandler for this test Payload = Encoding.UTF8.GetBytes("testPayload"), }; testHttpRequest.GetPayloadHandler().GetType().Should().Be(typeof(X509CaCertPayloadHandler)); var httpRequestMessage = new HttpRequestMessage(); pkixCertPayloadHandler.SetHttpRequestMessageContent(testHttpRequest, httpRequestMessage); httpRequestMessage.Content.Should().NotBeNull(); httpRequestMessage.Content.GetType().Should().Be(typeof(ByteArrayContent)); httpRequestMessage.Content.Headers.Contains("Content-Type"); httpRequestMessage.Content.Headers.ContentType.ToString().Should().Be("application/x-x509-ca-cert"); }
public void CallPayloadHandlerSetHttpRequestMessageContent() { var testContentType = "testContentType"; var mockPayloadHandler = Substitute.For <IPayloadHandler>(); mockPayloadHandler.ContentType.Returns(testContentType); var testHttpMessage = new HttpRequestMessage(); // Registering the payload handler sets it as the handler for its content type; in this case "testContentType" PayloadHandler.TryRegister(mockPayloadHandler); // Setting ContentType on the request causes the internal payload handler to be set to the PayloadHandler registered for the specified content type var testHttpRequest = new TestHttpRequest { ContentType = testContentType }; testHttpRequest.GetPayloadHandler().GetType().Should().NotBe(typeof(JsonPayloadHandler)); // the actual type is a mock testHttpRequest.SetHttpRequestMessageContent(testHttpMessage); mockPayloadHandler .Received(1) .SetHttpRequestMessageContent(Arg.Is(testHttpRequest), Arg.Is(testHttpMessage)); }
protected Task SendMessage(string routingKey, StandardPayload payload, PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int timeout = StandardPayload.DEFAULT_TIMEOUT, string directMessageQueue = "") { if (SMQActorBase.IsDebugMode) { System.Console.WriteLine(routingKey); } var finalMic = String.IsNullOrEmpty(directMessageQueue) ? this.Microphone : ""; var finalRoutinKey = String.IsNullOrEmpty(directMessageQueue) ? routingKey : directMessageQueue; IBasicProperties props = this.RMQChannel.CreateBasicProperties(); props.ReplyTo = "amq.rabbitmq.reply-to"; props.CorrelationId = payload.PayloadId.ToString(); payload.RoutingKey = String.IsNullOrEmpty(directMessageQueue) ? "" : routingKey; var payloadJson = JsonConvert.SerializeObject(payload, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); this.RMQChannel.BasicPublish(finalMic, finalRoutinKey, props, Encoding.UTF8.GetBytes(payloadJson)); return(payload.WaitForReply(replyHandler, timeoutHandler, timeout)); }
/// <summary> /// ValidateTemporaryAccessToken - /// </summary> public Task ValidateTemporaryAccessToken(PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { return(this.ValidateTemporaryAccessToken(this.CreatePayload(), replyHandler, timeoutHandler, waitTimeout)); }
/// <summary> /// ValidateTemporaryAccessToken - /// </summary> public Task ValidateTemporaryAccessToken(String content, PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { var payload = this.CreatePayload(content); return(this.ValidateTemporaryAccessToken(payload, replyHandler, timeoutHandler, waitTimeout)); }
/// <summary> /// GetLicenseSKUs - /// </summary> public Task GetLicenseSKUs(StandardPayload payload, PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { return(this.SendMessage("crudcoordinator.crud.guest.getlicenseskus", payload, replyHandler, timeoutHandler, waitTimeout)); }
/// <summary> /// RequestToken - /// </summary> public Task RequestToken(StandardPayload payload, PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { return(this.SendMessage("crudcoordinator.general.guest.requesttoken", payload, replyHandler, timeoutHandler, waitTimeout)); }
/// <summary> /// GetLicenseSKUs - /// </summary> public Task GetLicenseSKUs(PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { return(this.GetLicenseSKUs(this.CreatePayload(), replyHandler, timeoutHandler, waitTimeout)); }
/// <summary> /// GetLicenseSKUs - /// </summary> public Task GetLicenseSKUs(String content, PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { var payload = this.CreatePayload(content); return(this.GetLicenseSKUs(payload, replyHandler, timeoutHandler, waitTimeout)); }
/// <summary> /// StoreTempFile - /// </summary> public Task StoreTempFile(StandardPayload payload, PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { return(this.SendMessage("crudcoordinator.utlity.guest.storetempfile", payload, replyHandler, timeoutHandler, waitTimeout)); }
/// <summary> /// SuggestAbstractionRemoval - SuggestAbstractionRemoval /// </summary> public Task SuggestAbstractionRemoval(StandardPayload payload, PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { return(this.SendMessage("armediator.general.person.suggestabstractionremoval", payload, replyHandler, timeoutHandler, waitTimeout)); }
/// <summary> /// WhoAreYou - /// </summary> public Task WhoAreYou(StandardPayload payload, PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { return(this.SendMessage("crudcoordinator.general.guest.whoareyou", payload, replyHandler, timeoutHandler, waitTimeout)); }
/// <summary> /// StoreTempFile - /// </summary> public Task StoreTempFile(String content, PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { var payload = this.CreatePayload(content); return(this.StoreTempFile(payload, replyHandler, timeoutHandler, waitTimeout)); }
public void ProcessIncomingPayloads (PayloadHandler handler) { if (processing) { throw new InvalidOperationException ("Already processing."); } payload_handler = handler; processing = true; ThreadAssist.Spawn (Process); }
/// <summary> /// SuggestAbstractionRemoval - SuggestAbstractionRemoval /// </summary> public Task SuggestAbstractionRemoval(PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { return(this.SuggestAbstractionRemoval(this.CreatePayload(), replyHandler, timeoutHandler, waitTimeout)); }
/// <summary> /// SuggestAbstractionRemoval - SuggestAbstractionRemoval /// </summary> public Task SuggestAbstractionRemoval(String content, PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { var payload = this.CreatePayload(content); return(this.SuggestAbstractionRemoval(payload, replyHandler, timeoutHandler, waitTimeout)); }
/// <summary> /// ValidateTemporaryAccessToken - /// </summary> public Task ValidateTemporaryAccessToken(StandardPayload payload, PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { return(this.SendMessage("crudcoordinator.general.guest.validatetemporaryaccesstoken", payload, replyHandler, timeoutHandler, waitTimeout)); }
/// <summary> /// GetSSioTechnologies - /// </summary> public Task GetSSioTechnologies(StandardPayload payload, PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { return(this.SendMessage("crudcoordinator.crud.guest.getssiotechnologies", payload, replyHandler, timeoutHandler, waitTimeout)); }
/// <summary> /// WhoAmI - /// </summary> public Task WhoAmI(PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { return(this.WhoAmI(this.CreatePayload(), replyHandler, timeoutHandler, waitTimeout)); }
/// <summary> /// DirectMessage - A mediated directed message (through ARMediator) /// </summary> public Task DirectMessage(StandardPayload payload, PayloadHandler replyHandler = null, PayloadHandler timeoutHandler = null, int waitTimeout = StandardPayload.DEFAULT_TIMEOUT) { return(this.SendMessage("armediator.chat.person.directmessage", payload, replyHandler, timeoutHandler, waitTimeout)); }