Beispiel #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));
                }
            });
        }
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #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));
        }
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #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));
        }
Beispiel #8
0
 /// <inheritdoc />
 public AuthenticationResultEventArgs(JWTModel tokenResult)
 {
     TokenResult = tokenResult;
 }