Ejemplo n.º 1
0
        /// <summary>
        /// Handles the OAuth 2.0 process of the Blizzard login using ArgentPonyWarcraftClient and Flurl.
        /// </summary>
        /// <param name="authCode">The Authentication Code from Blizzard's OAuth 2.0</param>
        /// <param name="configuration">Allows us to call the appsettings.json configurations.</param>
        /// <returns></returns>
        public async Task <AuthResponseType> LoginAsync(string authCode, IConfiguration configuration, ICookieManager cookieManager, IWarcraftClient warcraftClient)
        {
            if (authCode == null)
            {
                return(IsCookiePresent(cookieManager));
            }
            else
            {
                // Get token || No idea what happens here tbh.
                var values = new Dictionary <string, string>
                {
                    { "region", configuration["BattleNet:Region"] },
                    { "grant_type", "authorization_code" },
                    { "code", authCode },
                    { "redirect_uri", "https://*****:*****@"https://eu.battle.net/oauth/token"
                               .WithBasicAuth(configuration["BattleNet:ClientId"], configuration["BattleNet:ClientSecret"])
                               .PostUrlEncodedAsync(values)
                               .ReceiveString();

                var token = JsonConvert.DeserializeObject <Token>(tokenReq);

                if (token.scope == null)
                {
                    // OAuth failed on Blizzard's side.
                    return(AuthResponseType.Declined);
                }

                var userInfo = await @"https://eu.battle.net/oauth/userinfo"
                               .WithOAuthBearerToken(token.access_token).GetJsonAsync <UserInfo>();

                //Create cookie
                OAuthCookie cookie = cookieManager.GetOrSet("BNetProfile", () =>
                {
                    return(new OAuthCookie()
                    {
                        Id = Guid.NewGuid().ToString(),
                        AccessToken = token.access_token,
                        Battletag = userInfo.BattleTag,
                        Rank = AssignGuildMemberRank(warcraftClient, token).Result
                    });
                }, new CookieOptions()
                {
                    HttpOnly = true, Expires = DateTime.Now.AddDays(1), SameSite = SameSiteMode.Strict, Secure = true
                });

                // All is well, return the user to the LoginSuccesful View.
                return(AuthResponseType.Success);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Check if the Cookie is present for our user.
        /// </summary>
        /// <param name="_cookieManager">The ICookieManager</param>
        /// <returns>A Response Type from the Blizzard API.</returns>
        public AuthResponseType IsCookiePresent(ICookieManager _cookieManager)
        {
            // Check if the token is present in the cookie.
            OAuthCookie cookie = _cookieManager.Get <OAuthCookie>("BNetProfile");

            if (cookie != null)
            {
                if (cookie.AccessToken != null && cookie.AccessToken != "")
                {
                    return(AuthResponseType.Success);
                }
            }

            // If it does not exist, send the user to the Require Authentication page from Blizzard.
            return(AuthResponseType.RequiresAuth);
        }
Ejemplo n.º 3
0
        public IActionResult LoginSuccesful()
        {
            OAuthCookie cookie = _cookieManager.Get <OAuthCookie>("BNetProfile");

            return(View("../Instinct/LoginSucceeded", cookie));
        }