Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Обсуждение авторизации #2

Open
qdimka opened this issue Feb 17, 2021 · 0 comments
Open

Обсуждение авторизации #2

qdimka opened this issue Feb 17, 2021 · 0 comments
Assignees
Labels
question Further information is requested

Comments

@qdimka
Copy link
Contributor

qdimka commented Feb 17, 2021

@alkuramshina есть такое предложение по авторизации. Позволяет разделить сессии для разных сервисов (Глагол, Яндекс). Предлагаю обсудить и додумать.

       // Хранит куки
    public interface ISessionStorage
    {
        public CookieCollection Get();
        public void Save(CookieCollection cookie);
    }
    
    // Взаимодействует с ISessionStorage и апи яндекса. Подставляет куки
    public interface IYandexApiInvoker
    {
        Task<TOut> GetAsync<TIn, TOut>(Uri uri, TIn query, bool skipAuth, CancellationToken token);
        
        Task<TOut> PostAsync<TIn, TOut>(Uri uri, TIn request, bool skipAuth, CancellationToken token);
    }

    // Взаимодействует с IYandexApiInvoker и отвечает за авторизацию
    public interface IYandexSession
    {
        Task<bool> IsAuthorizedAsync();
        
        Task AuthorizeByLoginAsync(AuthByLoginRequest request);
    }

    // Расширяет функционал IYandexSession для получения токенов от Я.Музыки 
    public interface IGlagolSession
    {
        public Task<string> GetMusicTokenAsync();
    }
    
    
    public class SessionStorage : ISessionStorage
    {
        private readonly string _path;

        public SessionStorage(string path)
        {
            _path = path;
        }
        
        public CookieCollection Get()
        {
            return new CookieCollection();
        }

        public void Save(CookieCollection cookies)
        {
            throw new NotImplementedException();
        }
    }

    // Реализует IYandexApiInvoker, IYandexSession,
    // позволяет при запросах к апи яндекса рефрешить авторизацию, незаметно для пользователчя
    public class YandexApi : IYandexApiInvoker, IYandexSession
    {
        private readonly ISessionStorage _sessionStorage;
        
        private readonly HttpClientHandler _httpClientHandler;
        private readonly HttpClient _httpClient;

        public YandexApi(ISessionStorage sessionStorage)
        {
            _sessionStorage = sessionStorage;
            _httpClientHandler = new HttpClientHandler
            {
                UseCookies = true,
                CookieContainer = new CookieContainer()
            };

            _httpClient = new HttpClient(_httpClientHandler);
        }
        
        public Task<TOut> GetAsync<TIn, TOut>(Uri uri, TIn query, bool skipAuth, CancellationToken token)
        {
            throw new NotImplementedException();
        }

        public Task<TOut> PostAsync<TIn, TOut>(Uri uri, TIn request, bool skipAuth, CancellationToken token)
        {
            throw new NotImplementedException();
        }

        public Task<bool> IsAuthorizedAsync()
        {
            throw new NotImplementedException();
        }

        public Task AuthorizeByLoginAsync(AuthByLoginRequest request)
        {
            throw new NotImplementedException();
        }

        private Task AuthorizeByXToken(string xToken)
        {
            throw new NotImplementedException();
        }
    }
    
    public class GlagolSession : IGlagolSession
    {
        private readonly IYandexApiInvoker _apiInvoker;

        public GlagolSession(IYandexApiInvoker apiInvoker)
        {
            _apiInvoker = apiInvoker;
        }
        
        public Task<string> GetMusicTokenAsync()
        {
            //return _apiInvoker.GetAsync<string>(url, params);
        }
    }
@qdimka qdimka added the question Further information is requested label Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants