public override async Task <bool> OnPreRequest()
        {
            //Get token
            string tokenString = GetAuthToken();

            if (tokenString == null)
            {
                await WriteString("Not Authorized", "text/plain", 401);

                return(false);
            }

            //Authenticate this token
            token = await conn.GetTokenByTokenAsync(tokenString);

            if (token == null)
            {
                await WriteString("Not Authorized", "text/plain", 401);

                return(false);
            }

            //Get user
            user = await conn.GetUserByIdAsync(token.user_id);

            if (user == null)
            {
                await WriteString("Not Authorized", "text/plain", 401);

                return(false);
            }

            return(true);
        }
コード例 #2
0
        private async Task OnLoginRequest(JObject data)
        {
            //Check if already logged in
            if (user != null)
            {
                await SendLoginStatus(false, "Already logged in. Disconnect and reconnect first.");

                return;
            }

            //Validate
            if (!UtilValidateJObject(data, out string validateError, new JObjectValidationParameter("access_token", JTokenType.String)))
            {
                await SendLoginStatus(false, validateError);

                return;
            }

            //Get token
            token = await conn.GetTokenByTokenAsync((string)data["access_token"]);

            if (token == null)
            {
                await SendLoginStatus(false, "Token Invalid");

                return;
            }

            //Get user
            user = await conn.GetUserByIdAsync(token.user_id);

            if (user == null)
            {
                await SendLoginStatus(false, "User Invalid (bad!)");

                return;
            }

            //Issue OK
            await SendLoginStatus(true, "OK; Logged in user " + user.id);

            //Let user space code handle this
            await OnUserLoginSuccess();
        }
コード例 #3
0
        public DbToken Create(string type,
                              string value,
                              string userId,
                              int ttlSeconds)
        {
            var token = new DbToken
            {
                UserId   = userId,
                Type     = type,
                Value    = value,
                Created  = DateTime.Now,
                ExpireAt = DateTime.Now.AddSeconds(ttlSeconds)
            };
            var existing = GetByTypeAndUserId(type, userId);

            if (existing != null)
            {
                return(existing);
            }
            _tokens.InsertOne(token);
            return(token);
        }
コード例 #4
0
        public DbToken Create(string type,
                              string value,
                              string author,
                              int ttlSeconds)
        {
            var token = new DbToken
            {
                Author   = author,
                Type     = type,
                Value    = value,
                Created  = DateTime.Now,
                ExpireAt = DateTime.Now.AddSeconds(ttlSeconds)
            };
            var existing = GetByTypeAndAuthor(type, author);

            if (existing != null)
            {
                return(existing);
            }
            _tokens.InsertOne(token);
            return(token);
        }
コード例 #5
0
ファイル: TokenService.cs プロジェクト: kaban4ik1994/Gallery2
 public void UpdateToken(DbToken token)
 {
     _tokenRepository.Update(token);
     _unitOfWork.Commit();
 }
コード例 #6
0
ファイル: TokenService.cs プロジェクト: kaban4ik1994/Gallery2
 public void CreateToken(DbToken token)
 {
     _tokenRepository.Add(token);
     _unitOfWork.Commit();
 }