Esempio n. 1
0
        private async Task <bool> CreateClientAsync(OAuthAccessTokenRepository tokenRepo)
        {
            Auth auth;

            if (!string.IsNullOrEmpty(this.AuthorizationCode))
            {
                auth = await this.preClient.ConnectWithCode(this.AuthorizationCode);

                await tokenRepo.Save(this.InstanceUri, auth.AccessToken);
            }
            else
            {
                auth = new Auth
                {
                    AccessToken = this.AccessToken
                };
            }

            this.Client = new MastodonClient(this.appRegistration, auth);

            var user = await this.Client.GetCurrentUser();

            // 自分のアカウント名が取得できてなかったら失敗とする
            if (string.IsNullOrEmpty(user?.AccountName))
            {
                return(false);
            }

            this.CurrentUser = user.ToMastodonAccount();

            this.PublicStreamingFunctionCounter = new ConnectionFunctionCounter <MastodonStatus>(new PublicTimelineFunction
            {
                Client = this.Client,
                StreamingInstanceUri = this.StreamingUri,
            });

            var homeFunc = new HomeTimelineFunction
            {
                Client = this.Client,
                StreamingInstanceUri = this.StreamingUri,
            };

            this.HomeStreamingFunctionCounter = new ConnectionFunctionCounter <MastodonStatus>(homeFunc);

            this.NotificationStreamingFunctionCounter = new ConnectionFunctionCompositionCounter <MastodonNotification, MastodonStatus>(
                new NotificationFunction(this.Client, homeFunc), this.HomeStreamingFunctionCounter
                );

            return(true);
        }
Esempio n. 2
0
        private async void InitializeAsync(OAuthAccessTokenRepository tokenRepo)
        {
            this.Auth = await MastodonAuthenticationHouse.Get(this.InstanceUri, tokenRepo);

            while (!this.Auth.HasAuthenticated)
            {
                await this.Auth.DoAuth(tokenRepo);

                // TODO AccessToken が無効になってた場合にのみここに来るはず。
                // AccessToken をクリアして、WebView で OAuth 認証からやり直す必要あり。
            }

            this.ImportAuthenticationData();
            await this.StartFunctionAsync();
        }
Esempio n. 3
0
        public Task DoAuth(OAuthAccessTokenRepository tokenRepo)
        {
            if (string.IsNullOrEmpty(this.AccessToken))
            {
                var comp = new TaskCompletionSource <bool>();

                // ヘルパがビヘイビアにアタッチされた時
                this.OAuthHelper.Attached += (sender, e) =>
                {
                    // すぐさまログインを開始
                    this.StartOAuthLogin();
                };

                // ブラウザのURIが変わった時
                this.OAuthHelper.UriNavigated += async(sender, e) =>
                {
                    try
                    {
                        if (e.Uri.Contains("/oauth/authorize/"))
                        {
                            var paths = e.Uri.Split('/');
                            this.AuthorizationCode = paths.Last();

                            await this.CreateClientAsync(tokenRepo);

                            this.OAuthHelper.Hide();
                            this.HasAuthenticated = true;
                            comp.SetResult(true);
                        }
                    }
                    catch
                    {
                        this.HasAuthenticated = false;
                        comp.SetResult(false);
                    }
                };

                return(comp.Task);
            }
            else
            {
                return(this.CreateClientAsync(tokenRepo).ContinueWith(t =>
                {
                    this.HasAuthenticated = t.Result;
                }));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// マストドンの認証情報を取得。
        /// 取得した情報にはHasAuthenticatedがfalse(未認証状態)の場合があるので
        /// その場合は呼び出し側が認証すること
        /// </summary>
        /// <param name="instanceUri">インスタンスURI</param>
        /// <returns>認証情報</returns>
        public async static Task <MastodonAuthentication> Get(string instanceUri, OAuthAccessTokenRepository tokenRepo)
        {
            var auth = Authes.SingleOrDefault(a => a.InstanceUri == instanceUri);

            if (auth != null)
            {
                return(auth);
            }
            else
            {
                var accessToken = await tokenRepo.Load(instanceUri);

                var newAuth = new MastodonAuthentication(instanceUri, accessToken);
                _authes.Add(newAuth);
                return(newAuth);
            }
        }
Esempio n. 5
0
 internal MastodonConnection(string instanceUri, IFunctionContainer functionContainer, OAuthAccessTokenRepository tokenRepo)
 {
     this.InstanceUri = instanceUri;
     this.container   = functionContainer;
     InitializeAsync(tokenRepo);
 }