public async Task Login(LoginInfo loginInfo, CancellationToken cancellationToken) { var instance = new Instance(loginInfo.Instance); var mastodonClient = await _clientRepository.FindMastodonClient(instance, cancellationToken); if (mastodonClient is null) { MastodonClientId clientId; MastodonClientSecret clientSecret; try { var(id, secret) = await _registerClient.RegisterClient(loginInfo, cancellationToken); clientId = new MastodonClientId(id); clientSecret = new MastodonClientSecret(secret); } catch { _showRegisterClientError.ShowRegisterClientError(); return; } mastodonClient = _clientRepository.CreateMastodonClient(instance, clientId, clientSecret); await _clientRepository.SaveMastodonClient(mastodonClient, cancellationToken); } var authorizeUrl = await mastodonClient.GetAuthorizeUri(); _showAuthUrl.ShowAuthUrl(new AuthorizationUrl(authorizeUrl)); }
public async Task Login(string host, CancellationToken cancellationToken) { string secret; try { secret = await _registerClient.RegisterClient(host, cancellationToken); } catch { _showRegisterClientError.ShowRegisterClientError(); throw; } var(sessionToken, authorizeUrl) = await _getAuthorizeUrl.GetAuthorizeUri(host, secret, cancellationToken); _showAuthUrl.ShowAuthUrl(authorizeUrl); while (true) { try { await _waitAuthorize.WaitAuthorize(cancellationToken); break; } catch { _showAuthorizeError.ShowAuthorizeError(); } } var(accessTokenData, user) = await _getAccessToken.GetAccessToken(host, secret, sessionToken, cancellationToken); var username = new Username(user.Username); var instance = new Instance(host); var displayName = new DisplayName(user.DisplayName); var identifier = new AccountIdentifier(username, instance); var accessToken = new MisskeyAccessToken(accessTokenData); var iconUrl = new AccountIconUrl(user.IconUrl); var account = await _accountRepository.FindMisskeyAccount(identifier, cancellationToken); if (account is not null) { account = account with { AccessToken = accessToken, IconUrl = iconUrl }; } else { account = new MisskeyAccount(username, instance, displayName, accessToken, iconUrl, new IsReadingPostsFromThisAccount(true)); } await _accountRepository.SaveMisskeyAccount(account, cancellationToken); var connection = _makeMisskeyConnection.MakeConnection(account.Username.Value, account.Instance.Value, account.AccessToken.Token); _connectionRepository.AddConnection(new Connection(account.AccountIdentifier, connection)); }