コード例 #1
0
        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)));
        }
コード例 #2
0
        public IActionResult DeleteToken()
        {
            var accessTokenAsString = JwtHelper.GetTokenSubstring(Request.Headers["Authorization"].ToString());
            var userCredentials     = JwtHelper.GetCredentialsFromToken(accessTokenAsString);

            _refreshTokenRepository.DeleteRefreshToken(userCredentials.Id.ToString());
            return(Ok());
        }
コード例 #3
0
        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)));
        }
コード例 #4
0
        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));
        }
コード例 #5
0
        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));
        }
コード例 #6
0
        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));
        }
コード例 #7
0
        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));
        }
コード例 #8
0
        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));
        }
コード例 #9
0
        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());
        }
コード例 #10
0
        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());
        }
コード例 #11
0
        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());
        }
コード例 #12
0
        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));
        }