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

            Assert.Equal("KwB6AEgATwBtAGIAMwBzAGwAQgA4AHQAcQBFAGsAUABPAEYAaQBnAEkARgBXAHAAawBVAFYAawBsAGIATgAzAHkAWAA2AEQAWQB6AHYAaQA5AHcATQBxAG0AbQBZAHUASwB5AEsARABLAE8AOQB4AC8AcQBqAHYAegBSAE8ALwBoAE0ARgBJAEIAdwBEADQAYwBmAFIAWABkAHAAdwB6AFUAegBNAGUATQBhADgARgBoAHoAUwBqADAAdQArAHYAaABnADUAegBMAHIATwA4AFQANABwAFMAaABVAEMAawB3ADIAdQByAG0ALwBGAEkAWgB0ADcAZQBmAEkARQAwAHEANgAyAEYAeQBZAFoASwBUAFcAOQBsADUAcgBiAGgALwAwAFIATwBxAGgARABHAHMASgBRAG0AagAyADEATgBzAGIAbQBTAGsANgBvAFMAOQBOAGoAdgBhAE4ASABJAGcAbABRAGoAUgAyAHgAawBBAGsAagA1AHQAZABOAHUAcQBZAEUAdwBaAFcARwBPAEYALwBjAGcAOQAzADkAcABsAEgAdwAzAHoAYgBhAGwAawBYAC8AcgBwAHgAcQBBAGQARQBOAGcARgBCAFAAcQBpAFoAcQA4AHUANgB0AHkAagBKADgAaQBsAEoAUwBHADcAYQBkAHkANwBuAHoAOABHAHUAWABHAEIAcgBIAFEAMQBJAHkAegB6AHcANQBLAGYAcwBsAFkAMwBRAD0APQA*", confirmKey);
        }
Example #2
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);
        }