Esempio n. 1
0
        public static async Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e)
        {
            //Try and authenticate the user.
            VoiceAccount va = null;

            if (e.Request.Query.ContainsKey("key"))
            {
                //This is the voice token key
                VoiceToken vc = Program.AuthenticateVoiceToken(e.Request.Query["key"]);
                if (vc == null)
                {
                    throw new Exception("RPWS voice token not valid.");
                }
                va = RpwsQuota.RpwsVoiceAuth.GetAccount(vc.rpws_uid);
            }
            if (e.Request.Query.ContainsKey("token"))
            {
                //This is the RPWS token
                RpwsMe me = await RpwsAuth.RpwsRequests.AuthenticateUser(e.Request.Query["token"]);

                if (me == null)
                {
                    throw new Exception("RPWS token is not valid.");
                }
                va = RpwsQuota.RpwsVoiceAuth.GetAccount(me.uuid);

                //That might've failed. If it did, create a "fake" account
                if (va == null)
                {
                    va = new VoiceAccount
                    {
                        b_has_sent_welcome_email = false,
                        credits = Program.config.defaultCredits,
                        last_token_awarded_time = DateTime.UtcNow.Ticks,
                        total_failed_requests   = 0,
                        total_ok_requests       = 0,
                        _id = me.uuid
                    };
                }
            }

            //If it failed, stop
            if (va == null)
            {
                throw new Exception("Not authenticated!");
            }

            //Award new tokens for an updated result
            RpwsQuota.RpwsVoiceAuth.AwardNewCredits(va);

            //Write
            await Program.QuickWriteJsonToDoc(e, va);
        }
Esempio n. 2
0
        public static void AwardNewCredits(VoiceAccount account)
        {
            //Reward daily credits
            DateTime lastTimeCreditsAwarded = new DateTime(account.last_token_awarded_time);
            DateTime nowTime             = DateTime.UtcNow;
            double   daysSinceLastUpdate = (nowTime - lastTimeCreditsAwarded).TotalDays;

            account.credits += (float)daysSinceLastUpdate * Program.config.dailyCreditsGiven;
            account.last_token_awarded_time = nowTime.Ticks;

            //Cap the daily credits
            if (account.credits > Program.config.maxDailyRefillCredits)
            {
                account.credits = Program.config.maxDailyRefillCredits;
            }
        }
Esempio n. 3
0
        public static VoiceAccount GetAccount(string userId)
        {
            var collec = Program.GetAccountsCollection();
            var acc    = collec.FindOne(x => x._id == userId);

            if (acc == null)
            {
                acc = new VoiceAccount
                {
                    _id     = userId,
                    credits = Program.config.defaultCredits,
                    total_failed_requests    = 0,
                    total_ok_requests        = 0,
                    last_token_awarded_time  = DateTime.UtcNow.Ticks,
                    b_has_sent_welcome_email = false
                };
                collec.Insert(acc);
            }
            return(acc);
        }
Esempio n. 4
0
 public static void SaveAccount(VoiceAccount acc)
 {
     Program.GetAccountsCollection().Update(acc);
 }
Esempio n. 5
0
        public static async Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e)
        {
            //Authenticate this user using the hostname used. We must use the hostname because that's the most I can change from the Pebble configuration.
            UrlParams urlParams = RpwsAuth.DecodeUrlParams.DecodeUrl(e.Request.Host.Host);

            //Check the voice token against our database
            VoiceToken me = Program.AuthenticateVoiceToken(urlParams.accessToken);

            if (me == null)
            {
                throw new Exception("Invalid access token!");
            }

            //Grab their user account
            VoiceAccount account = RpwsQuota.RpwsVoiceAuth.GetAccount(me.rpws_uid);

            //Award new tokens
            RpwsQuota.RpwsVoiceAuth.AwardNewCredits(account);

            //Send the welcome email if we haven't

            /*if(!account.b_has_sent_welcome_email)
             *  RpwsAuth.EmailService.SendWelcomeEmail(me);
             * account.b_has_sent_welcome_email = true;*/

            //Save account and log
            Console.WriteLine($"Voice request by {account._id}, credits remaining: {account.credits}");
            RpwsQuota.RpwsVoiceAuth.SaveAccount(account);

            //Stop request if needed
            if (account.credits < 1)
            {
                throw new Exception("Quota reached.");
            }

            //Pass this into the HTTPDecoder
            Tuple <RequestData, List <Stream> > payload = VoiceService.HttpDecoder.DecodeHttpData(e).Result;
            RequestData requestConfig = payload.Item1;

            //Now, convert this to base64 request
            string audioData = VoiceService.SpeexWithHeaderByteConverter.CreateBase64Payload(payload.Item2);

            //Now, form a Google request and send it.
            GoogleReply textData = await VoiceService.GoogleRequester.DoRequest(audioData, urlParams.languageRegion, Program.config.googleApiKey);

            //Now, convert to a fake Nuance response
            List <GoogleReply_Result> results = new List <GoogleReply_Result>();
            bool ok = true;

            if (textData != null)
            {
                results = textData.results;
                ok      = false;
            }
            else
            {
                ok = textData.results.Count != 0;
            }

            //Save
            account.credits -= 0.5f;
            if (ok)
            {
                account.credits -= 0.5f;
            }
            RpwsQuota.RpwsVoiceAuth.SaveAccount(account);

            //Now, form a reply that the Pebble will be happy with.
            NuanceResponse reply = VoiceService.FakeNuanceResponse.ConvertResponse(results, requestConfig);

            //Write this out
            await VoiceService.NuanceResponder.RespondWithData(e, JsonConvert.SerializeObject(reply));
        }