Beispiel #1
0
        public static async Task<TokenStatus> CheckTokenStatus(Token token)
        {
            var status = TokenStatus.NOTACKED;
            switch (token.Status)
            {
                case "NOTACKED":
                    status = TokenStatus.NOTACKED;
                    break;
                case "EXPIRED":
                    status = TokenStatus.EXPIRED;
                    throw new NotImplementedException();
                case "REVOKED":
                    status = TokenStatus.REVOKED;
                    throw new NotImplementedException();
                case "VALID":
                    status = TokenStatus.VALID;
                    //Retrieve the user for later
                    User = await GetCurrentUserFromAPI();
                    break;
            }


            // Create or update the token.
            if (settings.Contains("Token"))
            {
                settings["Token"] = token.NTToken;
                settings["TokenStatus"] = status;
                settings.Save();
            }
            else
            {
                // Add token..
                settings.Add("Token", token.NTToken);
                settings.Add("TokenStatus", status);
                settings.Save();
            }
            
            return status;
        }
Beispiel #2
0
        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void LoadData(UserRepository user = null, 
                             IEnumerable<ForumTopic> forumTopics = null, 
                             IEnumerable<ForumsRepository> forumRepositories = null, 
                             IEnumerable<PrivateMessageRepository> privateMessages = null, 
                             ForumRepository forum = null,
                             IEnumerable<ForumTopic> latestTopics = null,
                             ForumTopic forumTopic = null, 
                             List<ForumPost> forumTopicPosts = null)
        {
            if (user != null)
            {
                User.Add(new UserViewModel()
                {
                    UserName = user.UserName,
                    Avatar = user.Avatar,
                    Title = user.Title,
                    UserClass = ReadableUserClass(user.UserClass),
                    Age = user.Age,
                    Donor = user.Donor ? "Ja" : "Nej",
                    JoinDate = DateConverter.FromUnixTime(user.JoinDate),
                    Warned = user.Warned ? "Ja" : "Nej",
                    Friends = user.Friends,
                    UserID = user.UserId,
                    Forumaxx = user.ForumAxx
                });
            }

            if (privateMessages != null)
            {
                foreach (var item in privateMessages)
                {
                    PrivateMessages.Add(new PrivateMessagesViewModel()
                    {
                        ID = item.ID,
                        Content = item.Content,
                        CanReply = item.CanReply
                    });
                }
            }

            if (latestTopics != null)
            {
                foreach (var topic in latestTopics)
                {
                    LatestTopics.Add(new LatestTopicsViewModel()
                    {
                        ID = topic.Id,
                        Subject = topic.Subject,
                        User = topic.User,
                        Added = Utilities.DateConverter.FromUnixTime(topic.Added),
                        LastPostingUser = topic.LastPostingUser,
                        LastPostTime = Utilities.DateConverter.FromUnixTime(topic.LastPostTime)
                    });
                }
            }

            if (forumTopics != null)
            {
                ForumTopics.Clear();
                foreach (var topic in forumTopics)
                {
                    ForumTopics.Add(new TopicViewModel()
                    {
                        TopicID = topic.Id,
                        Subject = topic.Subject,
                        User = topic.User,
                        LastPostingUser = topic.LastPostingUser
                    });
                }
            }

            if (forumTopic != null)
            {
                Topic.Clear();
                Topic.Add(new TopicViewModel()
                {
                    TopicID = forumTopic.Id,
                    Subject = forumTopic.Subject,
                    TopicContent = forumTopicPosts != null ? forumTopicPosts[0].PostContent.Replace('[', '<').Replace(']', '>') : "",
                    User = forumTopic.User,
                    Added = Utilities.DateConverter.FromUnixTime(forumTopic.Added)
                });
            }

            if (forum != null)
            {
                Forum.Add(new ForumViewModel
                {
                    ID = forum.ID,
                    Name = forum.Name
                });
            }

            if (forumRepositories != null)
            {
                foreach (var item in forumRepositories)
                {
                    ForumForums.Add(new ForumViewModel()
                    {
                        Name = item.Name,
                        Description = item.Description,
                        ID = item.Id,
                        PostCount = item.PostCount,
                        TopicCount = item.TopicCount,
                        MinClassCreate = item.MinClassCreate,
                        MinClassRead = item.MinClassRead,
                        MinClassWrite = item.MinClassWrite,
                        SortIndex = item.SortIndex

                    });
                }
            }

            if (forumTopicPosts != null)
            {
                TopicPosts.Clear();
                foreach (var item in forumTopicPosts.Skip(1))
                {
                    TopicPosts.Add(new PostViewModel()
                    {
                        PostID = item.PostId,
                    PostContent = item.PostContent,
                        TopicID = item.TopicId,
                        User = item.User,
                        Added = Utilities.DateConverter.FromUnixTime(item.Added),
                    });
                }
                TopicPosts.Reverse();
            }

            this.IsDataLoaded = true;
        }
Beispiel #3
0
        /// <summary>
        /// Async Acquire Token by UserName
        /// </summary>
        /// <param name="userName"></param>
        /// <returns>The token status</returns>
        public static async Task<TokenStatus> AcquireToken(string userName)
        {
            var received = string.Empty;
            var tokenStatus = new TokenStatus();

            User = new UserRepository
            {
                UserName = userName
            };

            // Post data to authorize with
            var postData = "username="******"&appId=" + appId + "&signed=" + CreateSignedHex();

            // Get User from local storage
            if (settings.Contains("Token"))
            {
                var token = settings["Token"].ToString();

                postData += "&token=" + token;

                received = await Post("authorize?", postData);

                var jsonData = JsonConvert.DeserializeObject<dynamic>(received);

                if (jsonData["result"].Value == true)
                {
                    // Get token from json data
                    Token receivedToken = JsonConvert.DeserializeObject<Token>(jsonData["data"].ToString());

                    tokenStatus = await CheckTokenStatus(receivedToken);
                }
                else
                {
                    tokenStatus = TokenStatus.ERROR;
                    // Result is wrong, throw exception..
                    throw new InvalidDataException(jsonData.errorMsg.ToString());
                }
            }
            else
            {
                // Get received JSON
                received = await Post("authorize?", postData);

                // Deserialize json code.
                var jsonData = JsonConvert.DeserializeObject<dynamic>(received);

                //MessageBox.Show(jsonData.result.ToString());

                //if (jsonData.result == true)
                if (jsonData["result"].Value == true)
                {
                    // Get token from json data
                    //Token receivedToken = JsonConvert.DeserializeObject<Token>(jsonData.data.ToString());
                    Token receivedToken = JsonConvert.DeserializeObject<Token>(jsonData["data"].ToString());

                    tokenStatus = await CheckTokenStatus(receivedToken);
                }
                else
                {
                    tokenStatus = TokenStatus.ERROR;
                    // Result is wrong, throw exception..
                    throw new InvalidDataException(jsonData.errorMsg.ToString());
                }
            }

            return tokenStatus;
        }
Beispiel #4
0
        /// <summary>
        /// Async Acquire Token by UserName
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static async Task<TokenStatus> AcquireNewToken(string userName)
        {
            User = new UserRepository
            {
                UserName = userName
            };

            string postData = "username="******"&appId=" + appId + "&signed=" + CreateSignedHex();

            //Get User from local storage
            if (settings.Contains("Token"))
            {
                string token = settings["Token"].ToString();
                postData += "&token=" + token;

            }
            string received = await Post("authorize?", postData);


            //Deserialize json code.
            var jsonData = JsonConvert.DeserializeObject<dynamic>(received);


            if (jsonData["result"].Value == true)
            {
                //Get token from json data
                Token RecivedToken = JsonConvert.DeserializeObject<Token>(jsonData["data"].ToString());

                //Create or update the token.
                if (settings.Contains("Token"))
                {
                    settings["Token"] = RecivedToken.NTToken;
                    settings.Save();
                }
                else
                {
                    //Add token..
                    settings.Add("Token", RecivedToken.NTToken);
                    settings.Save();
                }

                TokenStatus s = TokenStatus.NOTACKED;
                switch (RecivedToken.Status)
                {
                    case "NOTACKED":
                        s = TokenStatus.NOTACKED;
                        break;
                    case "EXPIRED":
                        s = TokenStatus.EXPIRED;
                        throw new NotImplementedException();
                    case "REVOKED":
                        s = TokenStatus.REVOKED;
                        throw new NotImplementedException();
                    case "VALID":
                        s = TokenStatus.VALID;
                        //Retrieve the user for later
                        User = await GetCurrentUserFromAPI();
                        break;
                }
                return s;
            }
            else
            {
                //Result is wrong, throw exception..
                throw new InvalidDataException(jsonData.errorMsg.ToString());
            }



        }