Exemplo n.º 1
0
        protected override async Task OnLoginButtonClicked()
        {
            loginMessage1 = "";
            loginMessage2 = "Connecting to server...";
            drawLoginScreen(true);

            PlayerAccountJWTModel model = await HackySharedClientData.Instance.AuthService.TryAuthenticate(new AuthenticationRequestModel("Admin", "Test69!"));

            HackySharedClientData.Instance.AuthButtonListener.UpdatedTokenRepository(model);
            HackySharedClientData.Instance.AuthButtonListener.DispatchAuthenticationResult(model);
        }
        protected override void OnEventFired(object source, EventArgs args)
        {
            Console.WriteLine($"Auth: {UserNameField.Text}:{PasswordField.Text}");
            Task.Factory.StartNew(async() =>
            {
                PlayerAccountJWTModel jwtModel = null;

                //TODO: Validate username and password
                //We can't do error code supression with refit anymore, so we have to do this crap.
                try
                {
                    jwtModel = await AuthService.TryAuthenticate(new AuthenticationRequestModel(UserNameField.Text, PasswordField.Text))
                               .ConfigureAwait(false);
                }
                catch (ApiException e)
                {
                    jwtModel = e.GetContentAs <PlayerAccountJWTModel>();

                    if (Logger.IsErrorEnabled)
                    {
                        Logger.Error($"Encountered Auth Error: {e.Message}");
                    }
                }
                catch (Exception e)
                {
                    if (Logger.IsErrorEnabled)
                    {
                        Logger.Error($"Encountered Auth Error: {e.Message}\n\nStack: {e.StackTrace}");
                    }
                }
                finally
                {
                    if (Logger.IsDebugEnabled)
                    {
                        Logger.Debug($"Auth Response for User: {UserNameField.Text} Result: {jwtModel?.isTokenValid} OptionalError: {jwtModel?.Error} OptionalErrorDescription: {jwtModel?.ErrorDescription}");
                    }

                    if (jwtModel != null && jwtModel.isTokenValid)
                    {
                        AuthTokenRepository.Update(jwtModel.AccessToken);

                        GameQueueable.Enqueue(() =>
                        {
                            //Even if it's null, we should broadcast the event.
                            OnAuthenticationResultRecieved?.Invoke(this, new AuthenticationResultEventArgs(jwtModel));
                        });
                    }
                }
            });
        }
Exemplo n.º 3
0
        /// <summary>
        /// Attempts to authenticate with the set <see cref="AccountName"/>
        /// and <see cref="Password"/>
        /// </summary>
        /// <returns></returns>
        private bool TryAuthenticate()
        {
            //https://stackoverflow.com/questions/4926676/mono-https-webrequest-fails-with-the-authentication-or-decryption-has-failed
            ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            ServicePointManager.CheckCertificateRevocationList = false;

            //TODO: Service discovery
            IAuthenticationService authService = Refit.RestService.For <IAuthenticationService>("http://test-guardians-auth.azurewebsites.net");

            //Authentication using provided credentials
            PlayerAccountJWTModel result = authService.TryAuthenticate(new AuthenticationRequestModel(_AccountName, _Password)).Result;

            Debug.Log($"Auth Result: {result.isTokenValid} Token: {result.AccessToken} Error: {result.Error} ErrorDescription: {result.ErrorDescription}.");

            if (result.isTokenValid)
            {
                AuthenticationModelSingleton.Instance.SetAuthenticationState(true);
                AuthenticationModelSingleton.Instance.SetAuthenticationToken(result.AccessToken);
            }

            return(result.isTokenValid);
        }
 public void DispatchAuthenticationResult(PlayerAccountJWTModel model)
 {
     OnAuthenticationResultRecieved?.Invoke(this, new AuthenticationResultEventArgs(model));
 }
 public void UpdatedTokenRepository(PlayerAccountJWTModel jwtModel)
 {
     AuthTokenRepository.Update(jwtModel.AccessToken);
 }