예제 #1
0
        private void SendAuthMessage(AuthChangeReason reason)
        {
            var authManager = new AuthManager();

            ServiceContainer.Register <AuthManager> (authManager);
            MessageBus.Send(new AuthChangedMessage(authManager, reason));
        }
예제 #2
0
 public AuthChangedMessage(AuthManager manager, AuthChangeReason reason) : base(manager)
 {
     this.reason = reason;
 }
예제 #3
0
        private async Task <AuthResult> AuthenticateAsync(Func <Task <UserJson> > getUser, AuthChangeReason reason, AccountCredentials credentialsType)
        {
            if (IsAuthenticated)
            {
                throw new InvalidOperationException("Cannot authenticate when old credentials still present.");
            }
            if (IsAuthenticating)
            {
                throw new InvalidOperationException("Another authentication is still in progress.");
            }

            IsAuthenticating = true;

            try {
                UserJson userJson;
                try {
                    userJson = await getUser();

                    if (userJson == null)
                    {
                        ServiceContainer.Resolve <MessageBus> ().Send(
                            new AuthFailedMessage(this, AuthResult.InvalidCredentials));
                        return(AuthResult.InvalidCredentials);
                    }
                    else if (userJson.DefaultWorkspaceId == 0)
                    {
                        ServiceContainer.Resolve <MessageBus> ().Send(
                            new AuthFailedMessage(this, AuthResult.NoDefaultWorkspace));
                        return(AuthResult.NoDefaultWorkspace);
                    }
                } catch (Exception ex) {
                    var reqEx = ex as UnsuccessfulRequestException;
                    if (reqEx != null && (reqEx.IsForbidden || reqEx.IsValidationError))
                    {
                        ServiceContainer.Resolve <MessageBus> ().Send(
                            new AuthFailedMessage(this, AuthResult.InvalidCredentials));
                        return(AuthResult.InvalidCredentials);
                    }

                    var log = ServiceContainer.Resolve <ILogger> ();
                    if (ex.IsNetworkFailure() || ex is TaskCanceledException)
                    {
                        log.Info(Tag, ex, "Failed authenticate user.");
                    }
                    else
                    {
                        log.Warning(Tag, ex, "Failed to authenticate user.");
                    }

                    ServiceContainer.Resolve <MessageBus> ().Send(
                        new AuthFailedMessage(this, AuthResult.NetworkError, ex));
                    return(AuthResult.NetworkError);
                }

                // Import the user into our database:
                UserData userData;
                try {
                    var dataStore = ServiceContainer.Resolve <IDataStore> ();
                    userData = await dataStore.ExecuteInTransactionAsync(ctx => userJson.Import(ctx));
                } catch (Exception ex) {
                    var log = ServiceContainer.Resolve <ILogger> ();
                    log.Error(Tag, ex, "Failed to import authenticated user.");

                    ServiceContainer.Resolve <MessageBus> ().Send(
                        new AuthFailedMessage(this, AuthResult.SystemError, ex));
                    return(AuthResult.SystemError);
                }

                var credStore = ServiceContainer.Resolve <ISettingsStore> ();
                credStore.UserId   = userData.Id;
                credStore.ApiToken = userJson.ApiToken;

                User            = userData;
                Token           = userJson.ApiToken;
                IsAuthenticated = true;

                ServiceContainer.Resolve <MessageBus> ().Send(
                    new AuthChangedMessage(this, reason));
            } finally {
                IsAuthenticating = false;
            }

            // Ping analytics service
            var tracker = ServiceContainer.Resolve <ITracker> ();

            switch (reason)
            {
            case AuthChangeReason.Login:
                tracker.SendAccountLoginEvent(credentialsType);
                break;

            case AuthChangeReason.Signup:
                tracker.SendAccountCreateEvent(credentialsType);
                break;
            }

            return(AuthResult.Success);
        }
예제 #4
0
 public AuthChangedMessage (AuthManager manager, AuthChangeReason reason) : base (manager)
 {
     this.reason = reason;
 }
예제 #5
0
 private void SendAuthMessage (AuthChangeReason reason)
 {
     var authManager = new AuthManager ();
     ServiceContainer.Register<AuthManager> (authManager);
     MessageBus.Send (new AuthChangedMessage (authManager, reason));
 }