コード例 #1
0
        public async Task <SkillResponse> ProcessRequest(Intent request, string alexaUserId)
        {
            try
            {
                string computedOtp = null;
                int    numAttempts = 0;

                do
                {
                    computedOtp = GenerateOtp();

                    AlexaUser queriedAuthUser = await DynamoService.Instance.LoadItem <AlexaUser>(computedOtp);

                    if (queriedAuthUser != null)
                    {
                        Logger.Log($"OTP value '{computedOtp}' already exists. Attempting to generate a new one.");
                        numAttempts++;
                        continue;
                    }

                    AlexaUser newAlexaAuthUser = new AlexaUser();
                    newAlexaAuthUser.OneTimePasscode = computedOtp;
                    newAlexaAuthUser.AlexaUserId     = alexaUserId;
                    newAlexaAuthUser.TimeToLive      = DateTimeOffset.UtcNow.AddMinutes(5).ToUnixTimeSeconds();

                    bool didSaveSucceed = await DynamoService.Instance.SaveItem(newAlexaAuthUser);

                    if (didSaveSucceed)
                    {
                        break;
                    }
                    else
                    {
                        Logger.Log($"Error saving OTP value '{computedOtp}'. Attempting to generate a new one.");
                    }

                    numAttempts++;
                } while (numAttempts < 10);

                SsmlOutputSpeech responseMessage = BuildSsmlResponseMessage(computedOtp);

                if (computedOtp != null)
                {
                    return(ResponseBuilder.Tell(responseMessage));
                }
                else
                {
                    return(ResponseBuilder.Tell($"I'm sorry, I was unable to process your authentication."));
                }
            }
            catch (Exception ex)
            {
                Logger.Log($"Exception handling the authorization intent: {ex}");
                return(ResponseBuilder.Tell($"I'm sorry, I was unable to process your authentication. {ex.Message}."));
            }
        }
コード例 #2
0
        public void TestUserIdProperty(string userId)
        {
            string requestJson = GetUserIdTestJson(userId);

            try
            {
                AlexaUser alexaUser = JsonConvert.DeserializeObject <AlexaUser>(requestJson);
                Assert.Equal(ValidUserId, userId);
                Assert.Equal(ValidUserId, alexaUser.UserId);
            }
            catch (JsonSerializationException)
            {
                Assert.NotEqual(ValidUserId, userId);
            }
        }
コード例 #3
0
        public async Task <ActionResult> CreateNewDevice([FromBody] AuthData authData)
        {
            try
            {
                if (authData == null || !authData.IsModelValid())
                {
                    return(BadRequest($"Error in device create: {nameof(AuthData)} body is malformed: {authData}"));
                }

                // Find the user created when interacting with Alexa
                AlexaUser alexaUser = await context.LoadAsync <AlexaUser>(authData.OneTimePasscode);

                if (alexaUser == null || alexaUser.TimeToLive <= DateTimeOffset.UtcNow.ToUnixTimeSeconds())
                {
                    return(NotFound("Could not locate user."));
                }

                Device newDevice = new Device
                {
                    AlexaUserId   = alexaUser.AlexaUserId,
                    DeviceId      = Guid.NewGuid().ToString(),
                    FirebaseToken = authData.FirebaseToken,
                    DeviceName    = authData.DeviceName,
                    ModifiedDate  = DateTime.UtcNow,
                    CreatedDate   = DateTime.UtcNow
                };

                // Save the new device, and delete the old AlexaUser entry
                Task newDeviceSaveResult   = context.SaveAsync(newDevice);
                Task deleteAlexaAuthResult = context.DeleteAsync <AlexaUser>(alexaUser.AlexaUserId);

                await Task.WhenAll(newDeviceSaveResult, deleteAlexaAuthResult);

                return(CreatedAtAction(nameof(CreateNewDevice), newDevice));
            }
            catch (Exception ex)
            {
                return(BadRequest($"{authData}\n{ex}"));
            }
        }
コード例 #4
0
 public AlexaSession()
 {
     Application = new AlexaApplication();
     Attributes  = new AlexaAttributes();
     User        = new AlexaUser();
 }
コード例 #5
0
 public AlexaSystem()
 {
     Application = new AlexaApplication();
     User        = new AlexaUser();
     Device      = new AlexaDevice();
 }