Exemplo n.º 1
0
        public async Task <IActionResult> SetRole(string token, int anidbuserid, int role)
        {
            try
            {
                SessionInfoWithError s = await VerifyTokenAsync(token);

                if (s.Error != null)
                {
                    return(s.Error);
                }
                if ((s.Role & WebCache_RoleType.Admin) == 0)
                {
                    return(StatusCode(403, "Admin Only"));
                }
                WebCache_User us = await _db.Users.FirstOrDefaultAsync(a => a.AniDBUserId == anidbuserid);

                if (us == null)
                {
                    return(StatusCode(404, "User not found"));
                }
                WebCache_RoleType rt = (WebCache_RoleType)role;
                SetRole(anidbuserid, rt);
                return(Ok());
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"SETROLE with Token={token} Userid={anidbuserid} Role={(WebCache_RoleType)role}");
                return(StatusCode(500));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetCollisions(string token)
        {
            try
            {
                SessionInfoWithError s = await VerifyTokenAsync(token);

                if (s.Error != null)
                {
                    return(s.Error);
                }
                if ((s.Role & WebCache_RoleType.Admin) == 0)
                {
                    return(StatusCode(403, "Admin Only"));
                }
                Dictionary <int, string>                users      = new Dictionary <int, string>();
                List <WebCache_FileHash_Collision>      collisions = _db.WebCache_FileHash_Collisions.OrderBy(a => a.WebCache_FileHash_Collision_Unique).ToList();
                List <WebCache_FileHash_Collision_Info> rets       = new List <WebCache_FileHash_Collision_Info>();
                foreach (WebCache_FileHash_Collision c in collisions)
                {
                    string uname = null;
                    if (users.ContainsKey(c.AniDBUserId))
                    {
                        uname = users[c.AniDBUserId];
                    }
                    else
                    {
                        WebCache_User k = await _db.Users.FirstOrDefaultAsync(a => a.AniDBUserId == c.AniDBUserId);

                        if (k != null)
                        {
                            users.Add(c.AniDBUserId, k.AniDBUserName);
                            uname = k.AniDBUserName;
                        }
                    }

                    if (uname != null)
                    {
                        rets.Add(c.ToCollisionInfo(uname));
                    }
                }

                return(new JsonResult(rets));
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"GETCOLLISIONS with Token={token}");
                return(StatusCode(500));
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Verify(WebCache_AniDBLoggedInfo data)
        {
            try
            {
                CookieContainer cookieContainer = new CookieContainer();
                using (var handler = new HttpClientHandler {
                    CookieContainer = cookieContainer
                })
                    using (var client = new HttpClient(handler))
                    {
                        string curi  = GetAniDBUserVerificationUri();
                        string regex = GetAniDBUserVerificationRegEx();
                        client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", User_Agent);
                        Uri   uri = new Uri(curi);
                        Regex rn  = new Regex(regex, RegexOptions.Singleline);
                        foreach (string k in data.Cookies.Keys)
                        {
                            cookieContainer.Add(new Cookie(k, data.Cookies[k], "/", uri.Host));
                        }
                        HttpRequestMessage  request  = new HttpRequestMessage(HttpMethod.Get, uri);
                        HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead);

                        if (response.IsSuccessStatusCode)
                        {
                            string str = await response.Content.ReadAsStringAsync();

                            response.Dispose();
                            Match m = rn.Match(str);
                            if (m.Success)
                            {
                                if (m.Groups.Count > 1)
                                {
                                    string val = m.Groups["username"]?.Value;
                                    string id  = m.Groups["id"]?.Value;
                                    int    aniid;
                                    if (val != null && id != null && int.TryParse(id, out aniid))
                                    {
                                        if (string.Compare(val, data.UserName, StringComparison.InvariantCultureIgnoreCase) == 0)
                                        {
                                            uri = new Uri(GetAniDBLogoutUri());
                                            try
                                            {
                                                request  = new HttpRequestMessage(HttpMethod.Get, uri);
                                                response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

                                                response.Dispose();
                                            }
                                            catch (Exception)
                                            {
                                                //ignore
                                            }
                                            WebCache_User u = await _db.Users.FirstOrDefaultAsync(a => a.AniDBUserId == aniid);

                                            if (u == null)
                                            {
                                                u               = new WebCache_User();
                                                u.AniDBUserId   = aniid;
                                                u.AniDBUserName = val;
                                                _db.Add(u);
                                            }
                                            else if (u.AniDBUserName != val)
                                            {
                                                u.AniDBUserName = val;
                                            }
                                            WebCache_Session s = new WebCache_Session();
                                            s.Token         = Guid.NewGuid().ToString().Replace("-", string.Empty);
                                            s.Expiration    = DateTime.UtcNow.AddHours(GetTokenExpirationInHours());
                                            s.AniDBUserName = val;
                                            s.AniDBUserId   = aniid;
                                            _db.Add(s);
                                            await _db.SaveChangesAsync();

                                            SessionInfoWithError si = new SessionInfoWithError {
                                                AniDBUserId = s.AniDBUserId, AniDBUserName = s.AniDBUserName, Expiration = s.Expiration, Token = s.Token
                                            };
                                            si.Role  = GetRole(s.AniDBUserId);
                                            si.Error = null;
                                            return(new JsonResult(s));
                                        }
                                    }
                                }
                            }
                        }
                    }

                return(StatusCode(403, "Invalid credentials"));
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"VERIFY with UserName={data.UserName}");
                return(StatusCode(500));
            }
        }