public async Task <bool> HandleEvent(WebServer server, HttpListenerContext context) { string eventKey = context.Request.Headers.GetValues("X-Event-Key")?.FirstOrDefault(); if (eventKey == null) { return(context.JsonExceptionResponse(new InvalidOperationException("Invalid event key."), HttpStatusCode.BadRequest)); } var eventParts = eventKey.Split(':'); if (eventParts.Length != 2) { return(context.JsonExceptionResponse(new InvalidOperationException("Invalid event key."), HttpStatusCode.BadRequest)); } string eventCategory = eventParts[0]; string eventName = eventParts[1]; var configReader = Program.Container.GetInstance <ConfigReader>(); var eventConfig = configReader.LoadSection <BitbucketConfig>().Events .FirstOrDefault(i => i.Category.Equals(eventCategory, StringComparison.InvariantCultureIgnoreCase) && i.Name.Equals(eventName, StringComparison.InvariantCultureIgnoreCase)); if (eventConfig != null && eventConfig.Enabled) { var handlerData = GetBitbucketEventHandler(eventCategory, eventName); if (!handlerData.IsValid) { return(context.JsonExceptionResponse( new InvalidOperationException($"No event handler found for {eventKey}."), HttpStatusCode.BadRequest)); } var handlerInstance = Activator.CreateInstance(handlerData.HandlerClass); string eventPayload; using (var streamReader = new StreamReader(context.Request.InputStream)) { eventPayload = streamReader.ReadToEnd(); } // This assumes the signature of the method. Probably should check the signature before getting to this point. var messages = (IEnumerable <Message>)handlerData.HandlerMethod.Invoke(handlerInstance, new object[] { eventPayload }); var discordConfig = configReader.LoadSection <DiscordConfig>(); var webhooks = discordConfig.GetWebhooks(eventConfig.Webhooks)?.ToList(); if (messages != null && webhooks != null) { await SendMessages(messages, webhooks); } } context.Response.StatusCode = (int)HttpStatusCode.NoContent; context.Response.OutputStream.Close(); return(true); }
public bool PostValues(WebServer server, HttpListenerContext context) { Dictionary <string, object> formParams = context.RequestFormDataDictionary(); string state = (string)formParams["state"]; AuthorizationCodeAuth.Instances.TryGetValue(state, out SpotifyAuthServer <AuthorizationCode> authServer); AuthorizationCodeAuth auth = (AuthorizationCodeAuth)authServer; auth.ClientId = (string)formParams["clientId"]; auth.SecretId = (string)formParams["secretId"]; string uri = auth.GetUri(); return(context.Redirect(uri, false)); }
public Task <bool> GetEmpty(WebServer server, HttpListenerContext context) { string state = context.Request.QueryString["state"]; AuthorizationCodeAuth.Instances.TryGetValue(state, out SpotifyAuthServer <AuthorizationCode> auth); string code = null; string error = context.Request.QueryString["error"]; if (error == null) { code = context.Request.QueryString["code"]; } Task.Factory.StartNew(() => auth?.TriggerAuth(new AuthorizationCode { Code = code, Error = error })); return(context.StringResponseAsync("OK - This window can be closed now")); }
public Task <bool> GetAuth(WebServer server, HttpListenerContext context) { string state = context.Request.QueryString["state"]; SpotifyAuthServer <Token> auth = ImplictGrantAuth.GetByState(state); if (auth == null) { return(context.StringResponseAsync( $"Failed - Unable to find auth request with state \"{state}\" - Please retry")); } Token token; string error = context.Request.QueryString["error"]; if (error == null) { string accessToken = context.Request.QueryString["access_token"]; string tokenType = context.Request.QueryString["token_type"]; string expiresIn = context.Request.QueryString["expires_in"]; token = new Token { AccessToken = accessToken, ExpiresIn = double.Parse(expiresIn), TokenType = tokenType }; } else { token = new Token() { Error = error }; } Task.Factory.StartNew(() => auth.TriggerAuth(token)); return(context.StringResponseAsync("OK - This window can be closed now")); }