Exemplo n.º 1
0
        public async Task TestCreateConfirmKey()
        {
            string encryptme  = pso.ToJson().ToString();
            string confirmKey = EncryptorService.CreateConfirmKey(encryptme, key);

            Assert.Equal("KwB6AEgATwBtAGIAMwBzAGwAQgA4AHQAcQBFAGsAUABPAEYAaQBnAEkARgBXAHAAawBVAFYAawBsAGIATgAzAHkAWAA2AEQAWQB6AHYAaQA5AHcATQBxAG0AbQBZAHUASwB5AEsARABLAE8AOQB4AC8AcQBqAHYAegBSAE8ALwBoAE0ARgBJAEIAdwBEADQAYwBmAFIAWABkAHAAdwB6AFUAegBNAGUATQBhADgARgBoAHoAUwBqADAAdQArAHYAaABnADUAegBMAHIATwA4AFQANABwAFMAaABVAEMAawB3ADIAdQByAG0ALwBGAEkAWgB0ADcAZQBmAEkARQAwAHEANgAyAEYAeQBZAFoASwBUAFcAOQBsADUAcgBiAGgALwAwAFIATwBxAGgARABHAHMASgBRAG0AagAyADEATgBzAGIAbQBTAGsANgBvAFMAOQBOAGoAdgBhAE4ASABJAGcAbABRAGoAUgAyAHgAawBBAGsAagA1AHQAZABOAHUAcQBZAEUAdwBaAFcARwBPAEYALwBjAGcAOQAzADkAcABsAEgAdwAzAHoAYgBhAGwAawBYAC8AcgBwAHgAcQBBAGQARQBOAGcARgBCAFAAcQBpAFoAcQA4AHUANgB0AHkAagBKADgAaQBsAEoAUwBHADcAYQBkAHkANwBuAHoAOABHAHUAWABHAEIAcgBIAFEAMQBJAHkAegB6AHcANQBLAGYAcwBsAFkAMwBRAD0APQA*", confirmKey);
        }
Exemplo n.º 2
0
        public void TestOneWayHash()
        {
            string x    = "the quick brown fox jumped over the lazy dog";
            string hash = EncryptorService.OneWayHash(x);

            Assert.Equal(" ��-��efeX(�g�\u0016���;ž@Ibt�͵�&5�", hash);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds users email to the database with their chosen match objects.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <APIGatewayProxyResponse> ConfirmSubscribe(APIGatewayProxyRequest request, ILambdaContext context)
        {
            var logger = new Esk8LambdaLogger(context.Logger);

            logger.Log("Confirm Subscribe endpoint reached");

            if (request.QueryStringParameters.ContainsKey("confirmkey"))
            {
                string encryptionkey = Environment.GetEnvironmentVariable("ESK8BST_ENCRYPTION_KEY");
                string b64payload    = request.QueryStringParameters["confirmkey"];
                logger.Log("Received this as the confirm key: " + request.QueryStringParameters["confirmkey"]);
                string decrypted          = EncryptorService.DecryptConfirmKey(b64payload, encryptionkey);
                PostedSubscribeObject pso = null;
                try {
                    JObject jobj = JObject.Parse(decrypted);
                    pso = PostedSubscribeObject.FromJson(jobj);
                } catch (Exception e) {
                    logger.Log($"Tried to parse malformed json and failed at Confirm Subscribe: {e.Message}");
                    return(new APIGatewayProxyResponse()
                    {
                        StatusCode = (int)HttpStatusCode.InternalServerError,
                        Body = "Failed to parse json properly",
                        Headers = new Dictionary <string, string> {
                            { "Content-Type", "text/plain" }
                        },
                    });
                }

                if (pso != null && pso.Email.Contains("@") && pso.Matches.Count > 0)
                {
                    FirestoreService FSS = new FirestoreService(logger);
                    await FSS.UpsertSubscriber(pso);

                    await FSS.InsertPreconfirmed(pso.Email);

                    return(new APIGatewayProxyResponse()
                    {
                        StatusCode = (int)HttpStatusCode.Created,
                        Body = "Alright! You've been confirmed as interested in receiving updates from https://esk8bst.com",
                        Headers = new Dictionary <string, string> {
                            { "Content-Type", "text/plain" }
                        },
                    });
                }
            }

            return(new APIGatewayProxyResponse()
            {
                StatusCode = (int)HttpStatusCode.BadRequest,
                Body = "Failed to properly parse the confirm link.",
                Headers = new Dictionary <string, string> {
                    { "Content-Type", "text/plain" }
                },
            });
        }
Exemplo n.º 4
0
        public async Task TestDecryptConfirmKey()
        {
            string  b64       = "KwB6AEgATwBtAGIAMwBzAGwAQgA4AHQAcQBFAGsAUABPAEYAaQBnAEkARgBXAHAAawBVAFYAawBsAGIATgAzAHkAWAA2AEQAWQB6AHYAaQA5AHcATQBxAG0AbQBZAHUASwB5AEsARABLAE8AOQB4AC8AcQBqAHYAegBSAE8ALwBoAE0ARgBJAEIAdwBEADQAYwBmAFIAWABkAHAAdwB6AFUAegBNAGUATQBhADgARgBoAHoAUwBqADAAdQArAHYAaABnADUAegBMAHIATwA4AFQANABwAFMAaABVAEMAawB3ADIAdQByAG0ALwBGAEkAWgB0ADcAZQBmAEkARQAwAHEANgAyAEYAeQBZAFoASwBUAFcAOQBsADUAcgBiAGgALwAwAFIATwBxAGgARABHAHMASgBRAG0AagAyADEATgBzAGIAbQBTAGsANgBvAFMAOQBOAGoAdgBhAE4ASABJAGcAbABRAGoAUgAyAHgAawBBAGsAagA1AHQAZABOAHUAcQBZAEUAdwBaAFcARwBPAEYALwBjAGcAOQAzADkAcABsAEgAdwAzAHoAYgBhAGwAawBYAC8AcgBwAHgAcQBBAGQARQBOAGcARgBCAFAAcQBpAFoAcQA4AHUANgB0AHkAagBKADgAaQBsAEoAUwBHADcAYQBkAHkANwBuAHoAOABHAHUAWABHAEIAcgBIAFEAMQBJAHkAegB6AHcANQBLAGYAcwBsAFkAMwBRAD0APQA*";
            string  decrypted = EncryptorService.DecryptConfirmKey(b64, key);
            JObject jobj      = JObject.Parse(decrypted);
            PostedSubscribeObject decryptedPso = PostedSubscribeObject.FromJson(jobj);

            Assert.Equal(decryptedPso.Email, pso.Email);
            PostedMatchObject postedMatch = decryptedPso.Matches.FirstOrDefault();
            PostedMatchObject staticMatch = pso.Matches.FirstOrDefault();

            Assert.Equal(postedMatch.BST, staticMatch.BST);
            Assert.Equal(postedMatch.Currency, staticMatch.Currency);
            Assert.Equal(postedMatch.Price, staticMatch.Price);
            Assert.Equal(postedMatch.Companies.Count, staticMatch.Companies.Count);
        }
Exemplo n.º 5
0
        public async Task TestUnsubscribeMethod()
        {
            TestLambdaContext       context;
            APIGatewayProxyRequest  request;
            APIGatewayProxyResponse response;

            Functions functions = new Functions();

            request = new APIGatewayProxyRequest {
                HttpMethod            = HttpMethod.Get.Method,
                QueryStringParameters = new Dictionary <string, string>()
            };
            string key     = Environment.GetEnvironmentVariable("ESK8BST_ENCRYPTION_KEY");
            string payload = EncryptorService.Base64Encode("*****@*****.**"); //AESThenHMAC.SimpleEncryptWithPassword("*****@*****.**", key);

            request.QueryStringParameters.Add("confirmkey", payload);
            context  = new TestLambdaContext();
            response = await functions.Unsubscribe(request, context);

            Assert.Equal((int)HttpStatusCode.OK, response.StatusCode);
        }
Exemplo n.º 6
0
        /// <summary>
        /// The endpoint hit when a user submits their email to esk8bst
        /// This schedules a Mailgun Email that will include a Confirm Subscribe Link
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <APIGatewayProxyResponse> Subscribe(APIGatewayProxyRequest request, ILambdaContext context)
        {
            var logger = new Esk8LambdaLogger(context.Logger);

            logger.Log("Subscribe endpoint reached");


            if (request.HttpMethod != HttpMethod.Post.Method)
            {
                return(new APIGatewayProxyResponse()
                {
                    StatusCode = (int)HttpStatusCode.MethodNotAllowed
                });
            }
            ;

            string postbody           = request.Body;
            PostedSubscribeObject pso = null;
            string confirmkey         = "";

            try {
                JObject jobj = JObject.Parse(postbody);
                pso = PostedSubscribeObject.FromJson(jobj);

                if (pso.Email.Contains("@") && pso.Matches.Count > 0)   // we can proceed

                {
                    FirestoreService FSS = new FirestoreService(logger);
                    if (await FSS.CheckIsPreconfirmed(pso.Email))
                    {
                        // Immediately subscribe the user, they've already been here.
                        await FSS.UpsertSubscriber(pso);

                        return(new APIGatewayProxyResponse()
                        {
                            StatusCode = (int)HttpStatusCode.Created,
                            Headers = new Dictionary <string, string> {
                                { "Content-Type", "text/plain" }
                            },
                            Body = "Alright! You've been confirmed as interested in receiving updates from https://esk8bst.com",
                        });
                    }
                    else
                    {
                        // Not pre-confirmed, send an opt-in email.
                        string encryptionKey = Environment.GetEnvironmentVariable("ESK8BST_ENCRYPTION_KEY");
                        confirmkey = EncryptorService.CreateConfirmKey(pso.ToJson().ToString(), encryptionKey);
                    }
                }
            } catch (Exception e) {
                logger.Log($"Tried to parse a malformed subscriber json: {e.Message}");
                return(new APIGatewayProxyResponse()
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError,
                    Headers = new Dictionary <string, string> {
                        { "Content-Type", "text/plain" }
                    },
                    Body = "Failed to parse json properly",
                });
            }

            if (String.IsNullOrWhiteSpace(confirmkey))
            {
                return(new APIGatewayProxyResponse()
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError,
                    Body = "Failed to parse json properly - no email found",
                    Headers = new Dictionary <string, string> {
                        { "Content-Type", "text/plain" }
                    },
                });
            }

            MailgunService MSS = new MailgunService(logger);
            MailgunEmail   m   = new MailgunEmail()
            {
                To = new List <string> {
                    pso.Email
                },
                From    = MailgunService.POSTMASTER,
                Subject = "Esk8Bst Notification Opt In Request",
                Body    = "" +
                          "Someone has registered you as being interested in receiving notifications about new electric skateboard postings from https://esk8bst.com.\n\n" +
                          "If this was you, please click the link below to confirm your email. If this was not you, or you no longer wish to receive emails from us, then ignore this message.\n\n" +
                          $"https://1lol87xzbj.execute-api.us-east-2.amazonaws.com/Prod/confirm?confirmkey={confirmkey}",
            };

            bool success = await MSS.Send(m);

            if (!success)
            {
                return(new APIGatewayProxyResponse()
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError,
                    Body = "Failed to send email to recipent",
                    Headers = new Dictionary <string, string> {
                        { "Content-Type", "text/plain" }
                    },
                });
            }

            //An email has been sent to the address specified confirming your subscription
            var response = new APIGatewayProxyResponse {
                StatusCode = (int)HttpStatusCode.OK,
                Body       = "An email has been sent to the address specified confirming your subscription",
                Headers    = new Dictionary <string, string> {
                    { "Content-Type", "text/plain" }
                },
            };

            return(response);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Removes users email from the db
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <APIGatewayProxyResponse> Unsubscribe(APIGatewayProxyRequest request, ILambdaContext context)
        {
            var logger = new Esk8LambdaLogger(context.Logger);

            logger.Log("Unsubscribe endpoint reached");

            if (request.HttpMethod != HttpMethod.Get.Method)
            {
                return(new APIGatewayProxyResponse()
                {
                    StatusCode = (int)HttpStatusCode.MethodNotAllowed,
                    Body = "This endpoint only responds to GET requests",
                    Headers = new Dictionary <string, string> {
                        { "Content-Type", "text/plain" }
                    },
                });
            }

            string key  = Environment.GetEnvironmentVariable("ESK8BST_ENCRYPTION_KEY");
            string decr = "";

            if (request.QueryStringParameters.ContainsKey("confirmkey"))
            {
                string payload = request.QueryStringParameters["confirmkey"];
                if (!String.IsNullOrWhiteSpace(payload))
                {
                    decr = EncryptorService.Base64Decode(payload);
                    //decr = AESThenHMAC.SimpleDecryptWithPassword(payload, key);
                    if (String.IsNullOrWhiteSpace(decr) || !decr.Contains("@"))
                    {
                        return(new APIGatewayProxyResponse()
                        {
                            StatusCode = (int)HttpStatusCode.BadRequest,
                            Body = "An email was not found in the confirmkey parameter",
                            Headers = new Dictionary <string, string> {
                                { "Content-Type", "text/plain" }
                            },
                        });
                    }
                }
                else
                {
                    return(new APIGatewayProxyResponse()
                    {
                        StatusCode = (int)HttpStatusCode.BadRequest,
                        Body = "Missing value for parameter `confirmkey`",
                        Headers = new Dictionary <string, string> {
                            { "Content-Type", "text/plain" }
                        },
                    });
                }
            }
            else
            {
                return(new APIGatewayProxyResponse()
                {
                    StatusCode = (int)HttpStatusCode.BadRequest,
                    Body = "Missing parameter `confirmkey`",
                    Headers = new Dictionary <string, string> {
                        { "Content-Type", "text/plain" }
                    },
                });
            }
            FirestoreService FSS = new FirestoreService(logger);
            await FSS.DeleteSubscriber(decr.ToLowerInvariant());

            var response = new APIGatewayProxyResponse {
                StatusCode = (int)HttpStatusCode.OK,
                Body       = "Ok! You've been unsubscribed and will no longer receive updates. If you change your mind, you can always sign up again at https://esk8bst.com",
                Headers    = new Dictionary <string, string> {
                    { "Content-Type", "text/plain" }
                },
            };

            return(response);
        }