Exemple #1
0
        /// <inheritdoc/>
        public async Task <LoginResult> LoginViaOAuth(
            HostAddress hostAddress,
            IGitHubClient client,
            IOauthClient oauthClient,
            Action <Uri> openBrowser,
            CancellationToken cancel)
        {
            Guard.ArgumentNotNull(hostAddress, nameof(hostAddress));
            Guard.ArgumentNotNull(client, nameof(client));
            Guard.ArgumentNotNull(oauthClient, nameof(oauthClient));
            Guard.ArgumentNotNull(openBrowser, nameof(openBrowser));

            var state    = Guid.NewGuid().ToString();
            var loginUrl = GetLoginUrl(oauthClient, state);
            var listen   = oauthListener.Listen(state, cancel);

            openBrowser(loginUrl);

            var code = await listen.ConfigureAwait(false);

            var request = new OauthTokenRequest(clientId, clientSecret, code);
            var token   = await oauthClient.CreateAccessToken(request).ConfigureAwait(false);

            await keychain.Save("[oauth]", token.AccessToken, hostAddress).ConfigureAwait(false);

            var result = await ReadUserWithRetry(client).ConfigureAwait(false);

            await keychain.Save(result.User.Login, token.AccessToken, hostAddress).ConfigureAwait(false);

            return(result);
        }
        public Task <TToken> Authorize <TToken>(
            Uri callbackUri,
            Uri authorizationUri)
            where TToken : TokenCredentials
        {
            var taskCompletion = new TaskCompletionSource <TToken>();

            _listener.Listen(callbackUri, taskCompletion);

            _browser.Launch(authorizationUri);

            return(taskCompletion.Task);
        }