public IActionResult Refresh(Token token) { var accessTokenAsString = JwtHelper.GetTokenSubstring(Request.Headers["Authorization"].ToString()); if (accessTokenAsString == "null") { return(Unauthorized()); } var userCredentials = JwtHelper.GetCredentialsFromToken(accessTokenAsString); var savedRefreshToken = _refreshTokenRepository.GetUserRefreshToken(token.Refresh, userCredentials.Id); if (savedRefreshToken == null) { throw new SecurityTokenException("Invalid refresh token!"); } var newJwtToken = _jwtHelper.GetAccessToken(userCredentials); var newRefreshToken = _jwtHelper.GetRefreshToken(); if (!_refreshTokenRepository.AddRefreshToken(newRefreshToken, userCredentials.Id)) { return(Unauthorized()); } if (!_refreshTokenRepository.DeleteRefreshToken(savedRefreshToken)) { return(Unauthorized()); } return(Ok(new Token(newJwtToken, newRefreshToken))); }
public IActionResult DeleteToken() { var accessTokenAsString = JwtHelper.GetTokenSubstring(Request.Headers["Authorization"].ToString()); var userCredentials = JwtHelper.GetCredentialsFromToken(accessTokenAsString); _refreshTokenRepository.DeleteRefreshToken(userCredentials.Id.ToString()); return(Ok()); }
public IActionResult GetAllInstructors() { var accessTokenAsString = JwtHelper.GetTokenSubstring(Request.Headers["Authorization"].ToString()); if (accessTokenAsString == "null") { return(Unauthorized()); } var userCredentials = JwtHelper.GetCredentialsFromToken(accessTokenAsString); return(Ok(_instructorRepository.GetAllInstructorsByDrivingSchoolId(userCredentials.DrivingSchoolId))); }
public IActionResult GetRole() { var accessTokenAsString = JwtHelper.GetTokenSubstring(Request.Headers["Authorization"].ToString()); var userCredentials = JwtHelper.GetCredentialsFromToken(accessTokenAsString); var role = _refreshTokenRepository.GetRole(userCredentials.Id); if (string.IsNullOrEmpty(role)) { return(NotFound()); } return(Ok(role)); }
public IActionResult GetAdminById() { var accessTokenAsString = JwtHelper.GetTokenSubstring(Request.Headers["Authorization"].ToString()); if (accessTokenAsString == "null") { return(Unauthorized()); } var userCredentials = JwtHelper.GetCredentialsFromToken(accessTokenAsString); var admin = _adminRepository.GetAdminByUserId(userCredentials.Id); if (admin == null) { return(NotFound()); } return(Ok(admin)); }
public IActionResult GetDrivingSchoolByAdminId() { var accessTokenAsString = JwtHelper.GetTokenSubstring(Request.Headers["Authorization"].ToString()); if (accessTokenAsString == "null") { return(Unauthorized()); } var userCredentials = JwtHelper.GetCredentialsFromToken(accessTokenAsString); var classes = _drivingSchoolRepository.GetDrivingSchoolByAdminId(userCredentials.Id); if (classes == null) { return(NotFound()); } return(Ok(classes)); }
public IActionResult GetStudentsByDrivingSchoolId() { var accessTokenAsString = JwtHelper.GetTokenSubstring(Request.Headers["Authorization"].ToString()); if (accessTokenAsString == "null") { return(Unauthorized()); } var userCredentials = JwtHelper.GetCredentialsFromToken(accessTokenAsString); var studentToGet = _studentRepository.GetStudentsByDrivingSchoolId(userCredentials.DrivingSchoolId); if (studentToGet == null) { return(NotFound()); } return(Ok(studentToGet)); }
public IActionResult GetInstructorById() { var accessTokenAsString = JwtHelper.GetTokenSubstring(Request.Headers["Authorization"].ToString()); if (accessTokenAsString == "null") { return(Unauthorized()); } var userCredentials = JwtHelper.GetCredentialsFromToken(accessTokenAsString); var id = userCredentials.Id; var instructorToGet = _instructorRepository.GetInstructorById(id); if (instructorToGet == null) { return(NotFound()); } return(Ok(instructorToGet)); }
public IActionResult AddVehicleSession(VehicleSession vehicleSessionToAdd) { var accessTokenAsString = JwtHelper.GetTokenSubstring(Request.Headers["Authorization"].ToString()); if (accessTokenAsString == "null") { return(Unauthorized()); } var userCredentials = JwtHelper.GetCredentialsFromToken(accessTokenAsString); vehicleSessionToAdd.StudentId = userCredentials.Id; var wasSuccessful = _vehicleSessionRepository.AddVehicleSession(vehicleSessionToAdd); if (wasSuccessful) { return(Ok()); } return(Forbid()); }
public IActionResult AddStudent(Student studentToAdd) { var accessTokenAsString = JwtHelper.GetTokenSubstring(Request.Headers["Authorization"].ToString()); if (accessTokenAsString == "null") { return(Unauthorized()); } var userCredentials = JwtHelper.GetCredentialsFromToken(accessTokenAsString); studentToAdd.User.DrivingSchoolId = 1; var wasAddSuccessful = _studentRepository.AddStudent(studentToAdd); if (wasAddSuccessful) { return(Ok()); } return(Forbid()); }
public IActionResult AddInstructor(Instructor instructorToAdd) { var accessTokenAsString = JwtHelper.GetTokenSubstring(Request.Headers["Authorization"].ToString()); if (accessTokenAsString == "null") { return(Unauthorized()); } var userCredentials = JwtHelper.GetCredentialsFromToken(accessTokenAsString); var fullName = CredentialsHelper.GenerateUsername(instructorToAdd.FirstName, instructorToAdd.LastName); instructorToAdd.User.DrivingSchoolId = userCredentials.Id; var wasAddSuccessful = _instructorRepository.AddInstructor(instructorToAdd); if (wasAddSuccessful) { return(Ok()); } return(Forbid()); }
public IActionResult GetStudentById(int id) { var accessTokenAsString = JwtHelper.GetTokenSubstring(Request.Headers["Authorization"].ToString()); if (accessTokenAsString == "null") { return(Unauthorized()); } var userCredentials = JwtHelper.GetCredentialsFromToken(accessTokenAsString); if (userCredentials.Role == Role.Student) // student can only GET themselves { id = userCredentials.Id; } var studentToGet = _studentRepository.GetStudentById(id); if (studentToGet == null) { return(NotFound()); } return(Ok(studentToGet)); }