Exemple #1
0
        public static LuisAccount GetLuisAccount(string email)
        {
            if (Logger == null)
            {
                throw new Exception("I need a logger");
            }
            var         dbConnectionString = Environment.GetEnvironmentVariable("DB_CONNECTIONSTRING");
            LuisAccount la = new LuisAccount();

            using (SqlConnection connection = new SqlConnection(dbConnectionString))
            {
                connection.Open();
                SqlCommand command = connection.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = $"select Id, Email, LuisAccountNumber, LuisFavoriteColor, EnableSelfRegistration from LuisRegistrations  where Email = '{email}'";
                command.Connection  = connection;


                SqlDataReader reader = command.ExecuteReader();
                try
                {
                    while (reader.Read())
                    {
                        la.Id                     = Convert.ToInt32(reader["Id"]);
                        la.Email                  = Convert.ToString(reader["Email"]);
                        la.LuisAccountNumber      = Convert.ToString(reader["LuisAccountNumber"]);
                        la.LuisFavoriteColor      = Convert.ToString(reader["LuisFavoriteColor"]);
                        la.EnableSelfRegistration = Convert.ToBoolean(reader["EnableSelfRegistration"]);
                    }
                }
                catch (Exception e)
                {
                    Logger.LogError($"Exception while reading from database for email {email}, {e}");
                    return(null);
                }
                finally
                {
                    reader.Close();
                }
            }
            return(la);
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            Database.Logger   = log;
            Authenticator.log = log;

            /* {
             *  "email": "User email address",
             * } */

            req.Headers.TryGetValue("Authorization", out StringValues authHeaders);
            var authHeader = authHeaders.FirstOrDefault();
            var base64part = authHeader.Split(' ')[1];

            if (!Authenticator.Authenticate(base64part))
            {
                return((ActionResult) new UnauthorizedResult());
            }

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            // Check request body
            if (String.IsNullOrEmpty(requestBody))
            {
                return((ActionResult) new BadRequestObjectResult(new ResponseError()
                {
                    userMessage = "Request content is empty."
                }));
            }

            // Print out the request body
            log.LogInformation("Request body: " + requestBody);

            // Convert the request body into dynamic JSON data object
            dynamic data = JsonConvert.DeserializeObject(requestBody);


            //Check whether the email element is presented
            if (data.email == null || data.email.ToString() == "")
            {
                return((ActionResult) new BadRequestObjectResult(new ResponseError()
                {
                    userMessage = "Missing required `email` element"
                }));
            }

            LuisAccount luisAccount = Database.GetLuisAccount(data.email.ToString());

            if (luisAccount == null)
            {
                return((ActionResult) new BadRequestObjectResult(new ResponseError()
                {
                    userMessage = $"email {data.email} address has not been pre-registered with the LUIS service"
                }));
            }

            if (string.IsNullOrEmpty(luisAccount.LuisAccountNumber))
            {
                return((ActionResult) new BadRequestObjectResult(new ResponseError()
                {
                    userMessage = $"email {data.email} address has not been pre-registered with the LUIS service"
                }));
            }
            if (!luisAccount.EnableSelfRegistration)
            {
                return((ActionResult) new BadRequestObjectResult(new ResponseError()
                {
                    userMessage = $"pre-registration of LUIS account for email {data.email} is currently in progress please try again later"
                }));
            }

            return((ActionResult) new OkObjectResult(luisAccount));
        }
Exemple #3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            Database.Logger   = log;
            Authenticator.log = log;
            req.Headers.TryGetValue("Authorization", out StringValues authHeaders);
            var authHeader = authHeaders.FirstOrDefault();
            var base64part = authHeader.Split(' ')[1];

            if (!Authenticator.Authenticate(base64part))
            {
                return((ActionResult) new UnauthorizedResult());
            }

            /* {
             *  "email": "User email address",
             * } */

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            // Check request body
            if (String.IsNullOrEmpty(requestBody))
            {
                return((ActionResult) new BadRequestObjectResult(new ResponseError()
                {
                    userMessage = "Request content is empty."
                }));
            }

            // Print out the request body
            log.LogInformation("Request body: " + requestBody);

            // Convert the request body into dynamic JSON data object
            dynamic data = JsonConvert.DeserializeObject(requestBody);

            //Check whether the email element is presented
            if ((data.email == null || data.email.ToString() == "") && (data.signInName == null || data.signInName.ToString() == ""))
            {
                return((ActionResult) new BadRequestObjectResult(new ResponseError()
                {
                    userMessage = "either email or signInName must be supplied int he request body"
                }));
            }

            string email = "";

            if (data.email != null)
            {
                email = (string)data.email;
            }
            else if (data.signInName != null)
            {
                email = (string)data.signInName;
            }

            LuisAccount luisAccount = Database.GetLuisAccount(email);

            if (luisAccount == null)
            {
                return((ActionResult) new BadRequestObjectResult(new ResponseError()
                {
                    userMessage = $"email {email} address has not been pre-registered with the LUIS service"
                }));
            }

            var luisState = "";

            if (data.luisState != null)
            {
                luisState = (string)data.luisState;
            }
            log.LogInformation($"Incoming luisSate={luisState}");
            var luisStateResponse = $"luisState={luisState}";

            if (string.IsNullOrEmpty(luisAccount.LuisFavoriteColor))
            {
                luisAccount.LuisFavoriteColor = "None";
            }
            log.LogDebug($"email={email}, luisFavoritecolor={luisAccount.LuisFavoriteColor}, luisStateResponse={luisStateResponse}");
            return((ActionResult) new OkObjectResult(new LuisClaimsResponseContent()
            {
                LuisFavoriteColor = luisAccount.LuisFavoriteColor, LuisAccountNumber = luisAccount.LuisAccountNumber, LuisStateResponse = luisStateResponse
            }));
        }