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); }
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(); }
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); }
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); }
public void UpdateToken(DbToken token) { _tokenRepository.Update(token); _unitOfWork.Commit(); }
public void CreateToken(DbToken token) { _tokenRepository.Add(token); _unitOfWork.Commit(); }