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); }
public static async Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e) { //Grab the token from the URL if (!e.Request.Query.ContainsKey("token")) { throw new Exception("Missing token!"); } string rpws_token = e.Request.Query["token"]; //Look the token up in the database and see if we already have it. var collec = Program.GetVoiceTokensCollection(); var tokens = collec.Find(x => x.rpws_token == rpws_token).ToArray(); //If we already have a token for this, return the token if (tokens.Length == 1) { await Program.QuickWriteToDoc(e, tokens[0]._id, "text/plain"); return; } //Generate a new token. Request RPWS data RpwsMe me = await RpwsAuth.RpwsRequests.AuthenticateUser(rpws_token); if (me == null) { throw new Exception("Failed to authenticate with RPWS."); } //Create voice token string voiceToken = GenerateVoiceToken(collec); VoiceToken vt = new VoiceToken { last_cache_refresh_time = DateTime.UtcNow.Ticks, rpws_email = me.email, rpws_name = me.name, rpws_token = rpws_token, rpws_uid = me.uuid, _id = voiceToken }; //Insert collec.Insert(vt); //Log Console.WriteLine($"Generated voice token for RPWS user {me.uuid} ({me.email}), {voiceToken}"); //Write the token await Program.QuickWriteToDoc(e, voiceToken, "text/plain"); }