/// <summary> /// get the access-token by username and password /// </summary> /// <param name="parameters"></param> /// <returns></returns> private ResponseData GenerateJwt(Parameters parameters) { var refresh_token = Guid.NewGuid().ToString().Replace("-", ""); var rToken = new RToken { ClientName = parameters.username, RefreshToken = refresh_token, Id = Guid.NewGuid().ToString(), IsStop = 0 }; //store the refresh_token if (_tokenRepository.AddToken(rToken)) { return(new ResponseData { Code = "999", Message = "Ok", Data = GetJwt(parameters.username.Trim(), refresh_token) }); } return(new ResponseData { Code = "909", Message = "can not add token to database", Data = null }); }
//scenario 1 : get the access-token by username and password private ResponseData DoPassword(Parameters parameters) { //validate the client_id/client_secret/username/password var isValidated = UserInfo.GetAllUsers().Any(x => x.ClientId == parameters.client_id && x.ClientSecret == parameters.client_secret && x.UserName == parameters.username && x.Password == parameters.password); if (!isValidated) { return(new ResponseData { Code = "902", Message = "invalid user infomation", Data = null }); } var refresh_token = Guid.NewGuid().ToString().Replace("-", ""); var rToken = new RToken { ClientId = parameters.client_id, RefreshToken = refresh_token, Id = Guid.NewGuid().ToString(), IsStop = 0 }; //store the refresh_token if (_repo.AddToken(rToken)) { return(new ResponseData { Code = "999", Message = "OK", Data = GetJwt(parameters.client_id, refresh_token) }); } else { return(new ResponseData { Code = "909", Message = "can not add token to database", Data = null }); } }
/// <summary>Get the access-token by username and password</summary> /// <param name="parameters"></param> /// <param name="_repo"></param> /// <param name="_settings"></param> public ResponseData DoPassword(Parameters parameters, IRTokenRepository _repo, IOptions <Audience> _settings) { var refresh_token = Guid.NewGuid().ToString().Replace("-", ""); var rToken = new RToken { ClientId = parameters.username, RefreshToken = refresh_token, Id = Guid.NewGuid().ToString(), IsStop = 0 }; if (_repo.AddToken(rToken).Result) { dynamic UserInfo = new System.Dynamic.ExpandoObject(); UserInfo.FirstName = parameters.fullname; UserInfo.UserName = parameters.username; return(new ResponseData { Code = "999", Message = "OK", Content = UserInfo, Data = GetJwt(parameters.username, refresh_token, _settings) }); } else { return(new ResponseData { Code = "909", Message = "can not add token to database", Data = null }); } }
/// <summary>Get the access_token by refresh_token</summary> /// <param name="parameters"></param> /// <param name="_repo"></param> /// <param name="_settings"></param> public ResponseData DoRefreshToken(Parameters parameters, IRTokenRepository _repo, IOptions <Audience> _settings) { var token = _repo.GetToken(parameters.refresh_token, parameters.client_id).Result; if (token == null) { return(new ResponseData { Code = "905", Message = "can not refresh token", Data = null }); } if (token.IsStop == 1) { return(new ResponseData { Code = "906", Message = "refresh token has expired", Data = null }); } var refresh_token = Guid.NewGuid().ToString().Replace("-", ""); token.IsStop = 1; var updateFlag = _repo.ExpireToken(token).Result; var addFlag = _repo.AddToken(new RToken { ClientId = parameters.client_id, RefreshToken = refresh_token, Id = Guid.NewGuid().ToString(), IsStop = 0 }); if (updateFlag && addFlag.Result) { return(new ResponseData { Code = "999", Message = "OK", Data = GetJwt(parameters.client_id, refresh_token, _settings) }); } else { return(new ResponseData { Code = "910", Message = "can not expire token or a new token", Data = null }); } }
/// <summary>Get the access-token by username and password</summary> /// <param name="parameters"></param> /// <param name="_repo"></param> /// <param name="_settings"></param> public ResponseData DoPassword(Parameters parameters, IRTokenRepository _repo, IOptions <Audience> _settings) { try { var refresh_token = Guid.NewGuid().ToString().Replace("-", ""); var rToken = new RToken { ClientId = parameters.username, RefreshToken = refresh_token, Id = Guid.NewGuid().ToString(), IsStop = 0 }; if (_repo.AddToken(rToken).Result) { dynamic UserInfo = new System.Dynamic.ExpandoObject(); UserInfo.FirstName = parameters.fullname; UserInfo.UserName = parameters.username; return(new ResponseData { Code = "999", Message = "OK", Content = UserInfo, Data = GetJwt(parameters.username, refresh_token, _settings, BsonSerializer.Deserialize <RegisterModel>(MongoHelper.CheckForDatas("UserName", parameters.username, null, null, "Authentication", "Authentication")).UserRole) }); } else { return(new ResponseData { Code = "909", Message = "can not add token to database", Data = null }); } } catch (Exception ex) { LoggerDataAccess.CreateLog("AuthHelper", "DoPassword", ex.Message); return(new ResponseData { Code = "400", Message = "Failed", Data = null }); } }
/// <summary>Get the access_token by refresh_token</summary> /// <param name="parameters"></param> /// <param name="_repo"></param> /// <param name="_settings"></param> public ResponseData DoRefreshToken(Parameters parameters, IRTokenRepository _repo, IOptions <Audience> _settings) { try { var token = _repo.GetToken(parameters.refresh_token, parameters.client_id).Result; if (token == null) { return(new ResponseData { Code = "905", Message = "can not refresh token", Data = null }); } if (token.IsStop == 1) { return(new ResponseData { Code = "906", Message = "refresh token has expired", Data = null }); } var refresh_token = Guid.NewGuid().ToString().Replace("-", ""); token.IsStop = 1; var updateFlag = _repo.ExpireToken(token).Result; var addFlag = _repo.AddToken(new RToken { ClientId = parameters.client_id, RefreshToken = refresh_token, Id = Guid.NewGuid().ToString(), IsStop = 0 }); if (updateFlag && addFlag.Result) { return(new ResponseData { Code = "999", Message = "OK", Data = GetJwt(parameters.client_id, refresh_token, _settings, BsonSerializer.Deserialize <RegisterModel>(MongoHelper.CheckForDatas("UserName", parameters.client_id, null, null, "Authentication", "Authentication")).UserRole) }); } else { return(new ResponseData { Code = "910", Message = "can not expire token or a new token", Data = null }); } } catch (Exception ex) { LoggerDataAccess.CreateLog("AuthHelper", "DoRefreshToken", ex.Message); return(new ResponseData { Code = "400", Message = "Failed", Data = null }); } }