Example #1
0
        //TODO: Simplified async event firing/handling
        /// <inheritdoc />
        protected override void OnEventFired(object source, EventArgs args)
        {
            //We should not do async OnEventFired because we will get silent failures.
            UnityExtended.UnityMainThreadContext.PostAsync(async() =>
            {
                JWTModel 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(BuildAuthRequestModel())
                               .ConfigureAwait(false);
                }
                catch (ApiException e)
                {
                    jwtModel = e.GetContentAs <JWTModel>();

                    if (Logger.IsErrorEnabled)
                    {
                        Logger.Error($"Encountered Auth Error: {e.Message}");
                    }
                }
                finally
                {
                    if (Logger.IsDebugEnabled)
                    {
                        Logger.Debug($"Auth Response for User: {UsernameText.Text} Result: {jwtModel?.isTokenValid} OptionalError: {jwtModel?.Error} OptionalErrorDescription: {jwtModel?.ErrorDescription}");
                    }

                    //Even if it's null, we should broadcast the event.
                    OnAuthenticationResultRecieved?.Invoke(this, new AuthenticationResultEventArgs(jwtModel));
                }
            });
        }
Example #2
0
        public static void Test_JWTModel_Indicates_IsValid_When_Error_Is_Present(string error, string errorDescription)
        {
            //arrange
            JWTModel model = new JWTModel(error, errorDescription);

            //act
            JWTModel deserializedModel = JsonConvert.DeserializeObject <JWTModel>(JsonConvert.SerializeObject(model));

            //assert
            Assert.IsFalse(deserializedModel.isTokenValid);
        }
Example #3
0
        public static void Test_JWTModel_Indicates_IsValid_When_AccessToken_IsPresent(string accessToken)
        {
            //arrange
            JWTModel model = new JWTModel(accessToken);

            //act
            JWTModel deserializedModel = JsonConvert.DeserializeObject <JWTModel>(JsonConvert.SerializeObject(model));

            //assert
            Assert.IsTrue(deserializedModel.isTokenValid);
        }
Example #4
0
        public static void Test_Can_JSON_Serialize_To_NonNull_Non_Whitespace_ErrorArgs(string error, string errorDefinition)
        {
            //arrange
            JWTModel model = new JWTModel(error, errorDefinition);

            //act
            string serializedModel = JsonConvert.SerializeObject(model);

            //assert
            Assert.NotNull(serializedModel);
            Assert.IsNotEmpty(serializedModel);
            Assert.True(serializedModel.Contains(error));
            Assert.True(serializedModel.Contains(errorDefinition));
        }
Example #5
0
        public static void Test_Can_JSON_Serialize_Then_Deserialize_AccessToken(string accessToken)
        {
            //arrange
            JWTModel model = new JWTModel(accessToken);

            //act
            JWTModel deserializedModel = JsonConvert.DeserializeObject <JWTModel>(JsonConvert.SerializeObject(model));

            //assert
            Assert.NotNull(deserializedModel);
            Assert.NotNull(deserializedModel.AccessToken);
            Assert.IsNotEmpty(deserializedModel.AccessToken);
            Assert.AreEqual(accessToken, deserializedModel.AccessToken);
        }
Example #6
0
        public static void Test_Can_JSON_Serialize_Then_Deserialize_ErrorArgs(string error, string errorDefinition)
        {
            //arrange
            JWTModel model = new JWTModel(error, errorDefinition);

            //act
            JWTModel deserializedModel = JsonConvert.DeserializeObject <JWTModel>(JsonConvert.SerializeObject(model));

            //assert
            Assert.NotNull(deserializedModel);
            Assert.NotNull(deserializedModel.Error);
            Assert.NotNull(deserializedModel.ErrorDescription);
            Assert.AreEqual(error, deserializedModel.Error);
            Assert.AreEqual(errorDefinition, deserializedModel.ErrorDescription);
        }
Example #7
0
        public static void Test_Can_JSON_Serialize_To_NonNull_Non_Whitespace_AccessToken(string accessToken)
        {
            //arrange
            JWTModel model = new JWTModel(accessToken);

            //act
            string serializedModel = JsonConvert.SerializeObject(model);

            //assert
            Assert.NotNull(serializedModel);
            Assert.True(!serializedModel.Contains(nameof(model.isTokenValid)), $"JSON modle contains what should be unlisted field {nameof(model.isTokenValid)}. JSON: {serializedModel}");
            Assert.True(!serializedModel.Contains("_isTokenValid"), $"JSON modle contains what should be unlisted field _isTokenValid. JSON: {serializedModel}");
            Assert.IsNotEmpty(serializedModel);
            Assert.True(serializedModel.Contains(accessToken));
        }
Example #8
0
 /// <inheritdoc />
 public AuthenticationResultEventArgs(JWTModel tokenResult)
 {
     TokenResult = tokenResult;
 }