Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom JWT error messages for authentication failures #42

Conversation

JinnJarBurger
Copy link
Contributor

Fixes #22

Description

A custom AuthenticationEntryPoint has been implemented named JwtAuthenticationEntryPoint which intercepts any JWT Exceptions from the SecurityFilterChain and sets up the ProblemDetails accordingly wrapped inside the ErrorRes Object . The response content type has been set to "application/json" for increased readability.

Screenshots

Copy link
Contributor

deepsource-io bot commented Oct 25, 2024

Here's the code health analysis summary for commits d44ca98..23b6b26. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource Java LogoJava✅ SuccessView Check ↗
DeepSource Test coverage LogoTest coverage⚠️ Artifact not reportedTimed out: Artifact was never reportedView Check ↗
DeepSource Docker LogoDocker✅ SuccessView Check ↗
DeepSource Secrets LogoSecrets✅ SuccessView Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

@JinnJarBurger
Copy link
Contributor Author

JinnJarBurger commented Oct 25, 2024

Hello @nmarulo, please check this PR also I have some queries:

  • The security filter chain allows all the request to be accessed without any authentication.
  • Are you planning on opening up the "/api/auth/**" open to everyone and then make the rest of the URL authenticated?
  • If so should this PR address this or is this going to be handled later on?

Also please let me know if everything's alright. Thanks!

@nmarulo nmarulo self-assigned this Oct 25, 2024
Copy link
Member

@nmarulo nmarulo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR 😄.

When I finish reviewing it, I may comment on something else.

@nmarulo
Copy link
Member

nmarulo commented Oct 26, 2024

Hello @nmarulo, please check this PR also I have some queries:

  • The security filter chain allows all the request to be accessed without any authentication.

If this is correct, I want to be able to test the application without logging in or registering.

  • Are you planning on opening up the "/api/auth/**" open to everyone and then make the rest of the URL authenticated?

Yes, the idea is that if you want a private list, you will have to register and log in.

  • If so should this PR address this or is this going to be handled later on?

No, I just want if the token fails to return a custom message in the response body.

@JinnJarBurger
Copy link
Contributor Author

Also I forgot to mention to be able to test if the authentication handling is correct or not we can customize the SpringFilterChain accordingly:

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.securityMatcher(appProperties.getPathPrefix() + "/**")
            .authorizeHttpRequests(authorize -> {
                authorize.requestMatchers(appProperties.getPathPrefix() + "/auth/**")
                         .permitAll()
                         .anyRequest()
                         .authenticated();
            })
            .httpBasic(AbstractHttpConfigurer::disable)
            .csrf(AbstractHttpConfigurer::disable)
            .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
            .oauth2ResourceServer(configOAuth2())
            .cors(value -> value.configurationSource(corsConfigurationSource()));
        
        return http.build();
    }

This way we can get the bearer token after logging in then access all the other endpoints with the valid bearer token. If the bearer token is invalid (manipulation) or expired, the response body should output a custom error message with all the necessary details accordingly. Thanks!

@JinnJarBurger JinnJarBurger force-pushed the feature/issue-22-custom-jwt-error-message branch 2 times, most recently from db74f3d to eef1393 Compare October 27, 2024 15:32
@nmarulo nmarulo added the hacktoberfest-accepted PR aceptadas para el hacktoberfest label Oct 27, 2024
@JinnJarBurger JinnJarBurger force-pushed the feature/issue-22-custom-jwt-error-message branch from eef1393 to 23b6b26 Compare October 27, 2024 15:55
@nmarulo
Copy link
Member

nmarulo commented Oct 27, 2024

@JinnJarBurger Thanks 👍

@nmarulo nmarulo merged commit 71c7b93 into despensa-app:master Oct 27, 2024
5 of 6 checks passed
@JinnJarBurger
Copy link
Contributor Author

@JinnJarBurger Thanks 👍

Anytime! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted PR aceptadas para el hacktoberfest Size: S
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom JWT error message
2 participants