Пример #1
0
        public async Task <SsoResponse> Login([FromBody] LoginPost data)
        {
            try
            {
                var token = new PlexToken(data.Token);
                var(accessTier, loggedIn) = GetAccessTier();
                if (!loggedIn)
                {
                    accessTier = await _plexClient.GetAccessTier(serverIdentifier, token);
                }

                if (accessTier == AccessTier.Failure)
                {
                    var loginFailureResponse = _authValidator.ValidateAuthenticationStatus(AccessTier.NoAccess, false, GetServiceName(), GetServiceUri(), string.Empty);
                    Response.StatusCode = loginFailureResponse.Status;
                    return(loginFailureResponse);
                }

                var user = await _plexClient.GetUserInfo(token);

                var claims = new List <Claim>
                {
                    new Claim(Constants.AccessTierClaim, accessTier.ToString()),
                    new Claim(Constants.AccessTokenClaim, token.Value),
                    new Claim(Constants.ServerIdentifierClaim, serverIdentifier.Value),
                    new Claim(Constants.UsernameClaim, user.Username),
                    new Claim(Constants.EmailClaim, user.Email),
                    new Claim(Constants.ThumbnailClaim, user.Thumbnail)
                };

                var identity = new ClaimsIdentity(
                    claims,
                    CookieAuthenticationDefaults.AuthenticationScheme
                    );

                var authProperties = new AuthenticationProperties
                {
                    AllowRefresh = true,
                    IsPersistent = true
                };

                await HttpContext.SignInAsync(
                    CookieAuthenticationDefaults.AuthenticationScheme,
                    new ClaimsPrincipal(identity),
                    authProperties
                    );

                var response = _authValidator.ValidateAuthenticationStatus(accessTier, true, GetServiceName(), GetServiceUri(), user.Username);
                Response.StatusCode = response.Status;
                return(response);
            }
            catch (Exception e)
            {
                _logger.LogError("Failed to log user in", e);
                var unhandledErrorResponse = _authValidator.ValidateAuthenticationStatus(AccessTier.NoAccess, false, GetServiceName(), GetServiceUri(), string.Empty, true);
                Response.StatusCode = unhandledErrorResponse.Status;
                return(unhandledErrorResponse);
            }
        }
Пример #2
0
        public async Task <SsoResponse> Login([FromBody] LoginPost data)
        {
            try
            {
                Identity.AccessToken      = new AccessToken(data.Token);
                Identity.ServerIdentifier = _serverIdentifier;

                if (!Identity.IsAuthenticated)
                {
                    Identity.AccessTier = await _plexClient.GetAccessTier(_serverIdentifier, Identity.AccessToken);
                }

                if (Identity.AccessTier == AccessTier.Failure)
                {
                    Identity.AccessTier      = AccessTier.NoAccess;
                    Identity.IsAuthenticated = false;
                    return(GetErrorResponse());
                }

                var user = await _plexClient.GetUserInfo(Identity.AccessToken);

                Identity.Email     = user.Email;
                Identity.Username  = user.Username;
                Identity.Thumbnail = user.Thumbnail;

                Identity.IsAuthenticated = true;

                var identity = new ClaimsIdentity(
                    Identity.AsClaims(),
                    CookieAuthenticationDefaults.AuthenticationScheme
                    );

                var authProperties = new AuthenticationProperties
                {
                    AllowRefresh = true,
                    IsPersistent = true
                };

                await HttpContext.SignInAsync(
                    CookieAuthenticationDefaults.AuthenticationScheme,
                    new ClaimsPrincipal(identity),
                    authProperties
                    );

                var response = _authValidator.ValidateAuthenticationStatus(Identity, ServiceName, ServiceUri);
                Response.StatusCode = response.Status;
                return(response);
            }
            catch (Exception e)
            {
                _logger.LogError("Failed to log user in", e);
                Identity.AccessTier      = AccessTier.NoAccess;
                Identity.IsAuthenticated = false;
                return(GetErrorResponse());
            }
        }