private AuthenticationTicket GetAuthenticationTicketFromJsonElement(JsonElement element, int version)
        {
            switch (version)
            {
            case Version:
                var repositoryItem = this.ToObject <AuthenticationTicketRepositoryItem>(element);

                if (repositoryItem == null)
                {
                    throw new SpotifyMalformedAuthenticationTicketException("Unable to get authentication ticket.");
                }

                if (string.IsNullOrEmpty(repositoryItem.RefreshToken) ||
                    string.IsNullOrEmpty(repositoryItem.AccessToken) ||
                    repositoryItem.ExpiresAt == null)
                {
                    throw new SpotifyMalformedAuthenticationTicketException("Unable to get authentication tokens.");
                }

                if (repositoryItem.UserClaims?.Any(kvp => string.IsNullOrEmpty(kvp.Key) || string.IsNullOrEmpty(kvp.Value)) == true)
                {
                    throw new SpotifyMalformedAuthenticationTicketException("Unable to get user claims.");
                }

                var userClaims = new UserClaims(repositoryItem.UserClaims?.Select(kvp => new UserClaim(kvp.Key, kvp.Value)));
                if (!userClaims.HasClaim(UserClaimTypes.Id))
                {
                    throw new SpotifyMalformedAuthenticationTicketException("Unable to get User ID.");
                }

                return(new AuthenticationTicket(
                           repositoryItem.RefreshToken,
                           new AccessToken(repositoryItem.AccessToken, repositoryItem.ExpiresAt.Value),
                           userClaims));

            default:
                throw new SpotifyMalformedAuthenticationTicketException($"Stored authentication ticket has version {version} that is not supported.");
            }
        }
예제 #2
0
 public User(UserClaims userClaims)
 {
     this.Id = userClaims[UserClaimTypes.Id].Value;
 }