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 Authorize(InstanceAndAuthenticationCode instanceAndAuthenticationCode, CancellationToken cancellationToken) { var instance = new Instance(instanceAndAuthenticationCode.Instance); AccessInfo accessInfo; try { var client = await _mastodonClientRepository.FindMastodonClient(instance, cancellationToken); if (client is null) { _showMastodonAuthenticationError.ShowMastodonAuthenticationError(); return; } var authorizationInfo = new AuthorizationInfo(instance.Value, client.ClientId.Value, client.ClientSecret.Value, instanceAndAuthenticationCode.AuthenticationCode); accessInfo = await _authorizeMastodonAccountWithCode.AuthorizeMastodonAccountWithCode(authorizationInfo, cancellationToken); } catch { _showMastodonAuthenticationError.ShowMastodonAuthenticationError(); return; } AccountInfo accountInfo; try { accountInfo = await _getAccountInfo.GetAccountInfo(accessInfo, cancellationToken); } catch { _showGetMastodonAccountInfoError.ShowGetMastodonAccountInfoError(); return; } var username = new Username(accountInfo.Username); var displayName = new DisplayName(accountInfo.DisplayName); var accessToken = new MastodonAccessToken(accessInfo.Token); var iconUrl = new AccountIconUrl(accountInfo.IconUrl); var account = await _mastodonAccountRepository.FindMastodonAccount(new AccountIdentifier(username, instance), cancellationToken); if (account is null) { account = _mastodonAccountRepository.CreateMastodonAccount(username, instance, displayName, accessToken, iconUrl); } else { account = account with { AccessToken = accessToken, IconUrl = iconUrl }; } await _mastodonAccountRepository.SaveMastodonAccount(account, cancellationToken); var connection = _makeMastodonConnection.MakeConnection(account.Username.Value, account.Instance.Value, account.AccessToken.Token); _connectionRepository.AddConnection(new Connection(account.AccountIdentifier, connection)); _finishAuthorizeMastodonAccount.FinishAuthorizeMastodonAccount(); } }