Ejemplo n.º 1
0
        public async Task <CharacterMedia> GetCharacterMedia(string realm, string characterName) //TODO change to profile object
        {
            //Gonna need to split these up (for every case of this), as we might be trying to access account info with a client cred (non-user)
            if (!await IsAuthenticationToken() || IsTokenExpired())
            {
                //TODO implement getting/refreshing auth token
            }
            //replace realm name space with - char
            string path = $"profile/wow/character/{realm.ToLower().Replace(' ', '-')}/{characterName.ToLower()}/character-media";
            AuthenticationHeaderValue             authHeader = new AuthenticationHeaderValue("Bearer", token.access_token);
            List <KeyValuePair <string, string> > query      = new List <KeyValuePair <string, string> >();

            query.Add(new KeyValuePair <string, string>("region", "eu"));
            query.Add(new KeyValuePair <string, string>("namespace", "profile-eu"));
            query.Add(new KeyValuePair <string, string>("locale", "en_GB"));

            try
            {
                string response = await SendGet(false, path, query, authHeader);

                CharacterMedia charMedia = JsonConvert.DeserializeObject <CharacterMedia>(response);

                return(charMedia); //TODO implement profile object
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return(null); //TODO implement profile object
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Authorize(string code, string state)
        {
            if (User.Identity.IsAuthenticated) //TODO: check state
            {
                string      id = User.Identity.GetUserId();
                BearerToken b  = await APIHelper.GetInstance().GetTokenWithAuthenticationCode(code);


                //probably going to need something like:
                //>authenticate and set identity+bearertoken with middleware
                //
                //>get identity
                //  >get bearertoken from identity
                //      >do API call(bearertoken)

                //One way of storing the access and/or openid token, but probably inefficient and not well maintainable?
                //var claims = new List<Claim>();
                //claims.Add(new Claim("BlizzardAuthClientCredentialsAccessToken", b.access_token));
                //claims.Add(new Claim("BlizzardAuthClientCredentialsTokenType", b.token_type));
                //claims.Add(new Claim("BlizzardAuthClientCredentialsExpiresIn", b.expires_in.ToString()));
                //if(b.scope != null)
                //    claims.Add(new Claim("BlizzardAuthClientCredentialsScope", b.scope));
                //if (b.id_token != null)
                //    claims.Add(new Claim("BlizzardAuthClientCredentialsIdToken", b.id_token));
                //claims.Add(new Claim("BlizzardAuthClientCredentialsSub", b.sub));
                //claims.Add(new Claim("BlizzardAuthClientCredentialsExpirationDate", b.expirationDate.ToString()));
                //foreach(Claim c in claims)
                //{
                //    await UserManager.AddClaimAsync(id, c);
                //}

                //Cookie: The worst option for storing an openid token, OK for storing the short-lived access token

                //TODO: there's no way of distinguishing region right now, and if different regions have same realm names errors will occur here.
                //CODE FOR ADDING (only max level) CHARACTERS TO ACCOUNT
                int            processed = 0;
                AccountProfile account   = await APIHelper.GetInstance().GetAccountProfile();

                int accountsCount = account.wow_accounts.Count();
                Tuple <Character, CharacterMedia>[] responseArr =
                    new Tuple <Character, CharacterMedia> [account.wow_accounts.Select(x => x.characters.Where(y => y.level >= 60).Count()).Sum()];

                for (int i = 0; i < accountsCount; i++)
                {
                    Character[]         cArr   = account.wow_accounts[i].characters.Where(x => x.level >= 60).ToArray();
                    Characters[]        csArr  = cArr.Where(x => x.level >= 60).Select(y => Characters.CharacterToAerithiumDBCharactersConverter(y)).ToArray();
                    People_Characters[] pCsArr = new People_Characters[csArr.Count()];
                    for (int j = 0; j < csArr.Count(); j++)
                    {
                        //Use Characters to reference the object, since there is no id yet - EF will take care of it :)
                        pCsArr[j] = new People_Characters()
                        {
                            Characters = csArr[j], person_id = id
                        };
                        CharacterMedia cM = null;
                        cM = await APIHelper.GetInstance().GetCharacterMedia(cArr[j].realm.slug, cArr[j].name);

                        responseArr[processed] = new Tuple <Character, CharacterMedia>(cArr[j], cM);
                        processed++;
                    }
                    db.Characters.AddRange(csArr);
                    db.People_Characters.AddRange(pCsArr);
                    //db.SaveChanges();
                }
                ViewData["Response"] = responseArr;


                //ViewBag.Battletag = await APIHelper.GetInstance().GetUserInfo();
                //ViewBag.Guild = await APIHelper.GetInstance().GetGuild("silvermoon", "aerithium");
                //ViewBag.CharacterProfile = await APIHelper.GetInstance().GetCharacterProfile("silvermoon", "aerithium");
                //ViewBag.Mounts = await APIHelper.GetInstance().GetAccountMounts();
                //ViewBag.Pets = await APIHelper.GetInstance().GetAccountPets();

                return(View());
            }

            //someone's in between D:
            return(new HttpUnauthorizedResult());
        }