public void SetAccount(AuthenticatedAccount acct)
        {
            account = acct;

            // Set the appropriate cookies
            sockets[Service.Main].SetCookie(
                new WebSocketSharp.Net.Cookie(
                    "name", account.profile.nickname));
            sockets[Service.Main].SetCookie(
                new WebSocketSharp.Net.Cookie(
                    "authtoken", account.authToken));
            sockets[Service.Main].SetCookie(
                new WebSocketSharp.Net.Cookie(
                    "auth0id", account.auth0_id));

            sockets[Service.Ops].SetCookie(
                new WebSocketSharp.Net.Cookie(
                    "name", account.profile.nickname));
            sockets[Service.Ops].SetCookie(
                new WebSocketSharp.Net.Cookie(
                    "authtoken", account.authToken));
            sockets[Service.Ops].SetCookie(
                new WebSocketSharp.Net.Cookie(
                    "auth0id", account.auth0_id));
        }
        /// <summary>
        /// Form constructor
        /// Opens a modal login window first.  When control returns it checks
        /// if there's a valid user it populates the rest of the window.
        /// </summary>
        public MainWindow()
        {
            // Last resort for any unhandled exceptions
            AppDomain.CurrentDomain.UnhandledException
                += new UnhandledExceptionEventHandler(UEHandler);

            AddProcessor(typeof(ANWI.Messaging.FullRoster),
                         ProcessRoster);
            AddProcessor(typeof(ANWI.Messaging.FullOperationsList),
                         ProcessOpsList);
            AddProcessor(typeof(ANWI.Messaging.AllCommonData),
                         ProcessCommonData);
            AddProcessor(typeof(ANWI.Messaging.ConfirmUpdate),
                         ProcessConfirmUpdate);
            AddProcessor(typeof(ANWI.Messaging.FullProfile),
                         ProcessFullProfile);
            AddProcessor(typeof(ANWI.Messaging.Ops.NewOpCreated),
                         ProcessNewOpCreated);

            MessageRouter.Instance.onOpen  += OnSocketOpen;
            MessageRouter.Instance.onClose += OnSocketClose;
            MessageRouter.Instance.onError += OnSocketError;

#if !DEBUG
            //
            // Open the splash screen which doubles as the updater
            Splash splash = new Splash();
            splash.UpdateDownloaded += OnUpdateDownloaded;
            splash.ShowDialog();
#endif

            if (updated)
            {
                Application.Current.Shutdown();
                return;
            }

            // Open a modal login window
            // When the window closes the authclient member will be either null
            // or have a structure.
            // If null, the login failed.
            Login logwin = new Client.Login();
            logwin.returnuser += value => account = value;
            logwin.ShowDialog();

            // Once the control returns check if we have a valid account
            if (account == null || account.profile == null)
            {
                // Just quit the app
                Application.Current.Shutdown();
                return;
            }

            MessageRouter.Instance.SetAccount(account);

            // Once we have the basic data initialize the window
            this.DataContext = this;
            InitializeComponent();

            wpfProfile = account.profile;

            // Open connection to the main service
            MessageRouter.Instance.Connect(MessageRouter.Service.Main);
            MessageRouter.Instance.Connect(MessageRouter.Service.Ops);

            // Note: blocking
            FetchCommonData();

            FetchRoster();
            FetchOps();
        }
Beispiel #3
0
        private async void LoginUser(ANWI.Messaging.LoginRequest cred)
        {
            // Authenticate the user with Auth0
            try {
                // Check version
                if (minimumVersion.CompareTo(cred.clientVer) > 0)
                {
                    logger.Info(
                        $"User {cred.username} has invalid version. " +
                        $"Client: {cred.clientVer} Minimum: {minimumVersion}");
                    DenyLogin(ANWI.Messaging.LoginResponse.Code.FAILED_VERSION);
                    return;
                }

                ResourceOwnerTokenRequest req
                    = new ResourceOwnerTokenRequest()
                    {
                    ClientId     = Configuration.auth0Settings.client,
                    ClientSecret = Configuration.auth0Settings.secret,
                    Realm        = Configuration.auth0Settings.connection,
                    Username     = cred.username,
                    Password     = cred.password
                    };


                AccessTokenResponse token = null;
                try {
                    token = await auth0Client.GetTokenAsync(req);
                } catch (Auth0.Core.Exceptions.ApiException e) {
                    logger.Error(
                        $"Failed to log in user {cred.username}: {e.Message}");
                    DenyLogin(
                        ANWI.Messaging.LoginResponse.Code.FAILED_CREDENTIALS);
                    return;
                }

                UserInfo user
                    = await auth0Client.GetUserInfoAsync(token.AccessToken);

                logger.Info("Successfully authenticated user.  Token: " +
                            token.AccessToken);

                ANWI.AuthenticatedAccount account = new AuthenticatedAccount();
                account.authToken = token.AccessToken;
                account.auth0_id  = user.UserId;
                account.nickname  = user.NickName;

                // Get the main user profile
                Datamodel.User dbUser = null;
                if (!Datamodel.User.FetchByAuth0(ref dbUser, account.auth0_id))
                {
                    logger.Info("Profile not found for user " +
                                account.auth0_id + ". It will be created.");

                    // Create a basic profile
                    if (!CreateDatabaseUser(user.NickName, user.UserId))
                    {
                        DenyLogin(ANWI.Messaging.LoginResponse.
                                  Code.FAILED_SERVER_ERROR);
                        return;
                    }
                }

                account.profile = Profile.FetchByAuth0(account.auth0_id);

                ANWI.Messaging.Message resp = new ANWI.Messaging.Message(
                    0,
                    new ANWI.Messaging.LoginResponse(
                        ANWI.Messaging.LoginResponse.Code.OK,
                        account)
                    );

                SendMessage(resp);
            } catch (System.Net.Http.HttpRequestException e) {
                logger.Info("HTTP error when connecting to Auth0:\n"
                            + e);
                DenyLogin(
                    ANWI.Messaging.LoginResponse.Code.FAILED_SERVER_ERROR);
                return;
            }
        }
 public LoginResponse(Code c, AuthenticatedAccount acc)
 {
     code    = c;
     account = acc;
 }
 public LoginResponse()
 {
     code    = Code.FAILED_OTHER;
     account = null;
 }