Exemplo n.º 1
0
        public async Task <TokenResponse> ValidateMagicNumber(
            ITurnContext turnContext,
            string text,
            CancellationToken cancellationToken)
        {
            string channelUserId = await _userStateData.GetUserIdFromStateToken(text);

            if (channelUserId == RingoBotHelper.ChannelUserId(turnContext))
            {
                await turnContext.SendActivityAsync(
                    $"Magic Number OK. Ringo is authorized to play Spotify. Ready to rock! 😎",
                    cancellationToken : cancellationToken);

                await _userData.SetTokenValidated(channelUserId, text);

                return(await GetAccessToken(channelUserId));
            }

            _logger.LogWarning($"Invalid Magic Number \"{text}\" for channelUserId {RingoBotHelper.ChannelUserId(turnContext)}");
            await turnContext.SendActivityAsync(
                $"Magic Number is invalid or has expired. Please try again 🤔",
                cancellationToken : cancellationToken);

            return(null);
        }
Exemplo n.º 2
0
        public async Task <ContentResult> Authorize(
            [FromQuery(Name = "state")] string state,
            [FromQuery(Name = "code")] string code   = null,
            [FromQuery(Name = "error")] string error = null)
        {
            //string userId = GetUserId();

            // if Spotify returned an error, throw it
            if (error != null)
            {
                throw new SpotifyApiErrorException(error);
            }

            // validate state
            if (string.IsNullOrEmpty(state))
            {
                throw new ArgumentException("Invalid State Argument", nameof(state));
            }
            if (!AuthService.RingoBotStateRegex.IsMatch(state))
            {
                throw new ArgumentException("Invalid State Argument", nameof(state));
            }

            // get the userId from state
            string channelUserId = await _userStateData.GetUserIdFromStateToken(state);

            if (channelUserId == null)
            {
                return(new ContentResult
                {
                    ContentType = "text/html",
                    StatusCode = (int)HttpStatusCode.OK,
                    Content = $"<html><body><p>This authorization request has expired or is invalid. Please try again.</p></body></html>"
                });
            }

            // Use the code to request a token
            BearerAccessRefreshToken token = await _userAccounts.RequestAccessRefreshToken(code);

            await _userData.SaveUserAccessToken(channelUserId, MapToBearerAccessToken(token));

            // return an HTML result with the state token to authorise the bot
            return(new ContentResult
            {
                ContentType = "text/html",
                StatusCode = (int)HttpStatusCode.OK,
                Content = $"<html><body style='font-family:Consolas'><p>Copy this code into the chat window:<br/><input style='width:300px' value='{RingoBotCommands.AuthCommand[0]} {state}'/></p></body></html>"
            });
        }