private async Task <ReferralTokenReceipt> SendSignalRAuthReply(ReferralTokenReceipt referralToken, AuthReplyMethod methodEnum) { string methodName = null; switch (methodEnum) { case AuthReplyMethod.InitialRegistration: methodName = SignalrMethodNames.InitialRegistration; break; case AuthReplyMethod.Reauthenticated: methodName = SignalrMethodNames.Reauthenticated; break; } try { var replyPayload = new TokenPayload() { DiscordUserId = referralToken.ReferralUserId }; await _botHub.Clients.User(SignalrUsernames.BotUsername).SendAsync(methodName, replyPayload); referralToken.ReplySent = true; } catch (Exception ex) { _logger.Log(LogLevel.Error, "Failed sending SignalR auth response", ex); referralToken.ReplyError = true; } return(referralToken); }
public async Task <IActionResult> OnGetAsync(string provider, string returnUrl = null, string referralToken = null) { // Validate Referrer Token var key = Encoding.ASCII.GetBytes(_config["synthbot.token.sharedsecret"]); var handler = new JwtSecurityTokenHandler(); var validations = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = true, ValidateAudience = true, ValidAudiences = new[] { "Synthbot.WebApp" }, ValidIssuers = new[] { "Synthbot.DiscordBot" } }; ClaimsPrincipal referralTokenPrincipal; try { referralTokenPrincipal = handler.ValidateToken(referralToken, validations, out SecurityToken tokenSecure); } catch (Exception ex) { _logger.Log(LogLevel.Error, "Referral Token validation failed", ex); return(new BadRequestResult()); } // Add referral token var token = new ReferralTokenReceipt() { ReferralUserId = referralTokenPrincipal.GetDiscordUserId(), ReceivedTS = DateTime.UtcNow, ReferrerSignalrUser = referralTokenPrincipal.GetHubReplyUserId() }; // TODO - Move to a service layer await _db.ReferralTokenReceipts.AddAsync(token); await _db.SaveChangesAsync(); // Request a redirect to the external login provider. var redirectUrl = Url.Page("./ExternalLogin", pageHandler: "Callback", values: new { returnUrl }); var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl); // Add referral token to spotify auth properties properties.Items.Add("state", token.Id.ToString()); return(new ChallengeResult(provider, properties)); }