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

Chapter 8: Code for checking groups in UserController#requirePermissions fails with NPE if token (that is not Basic) authentication is used #23

Open
jumarko opened this issue Aug 11, 2023 · 1 comment

Comments

@jumarko
Copy link

jumarko commented Aug 11, 2023

After implementing the code for adding and checking groups from the opening of Chapter 8.1,
my app is failing with this NPE:

java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because "groups" is null
	at com.manning.apisecurityinaction.controllers.UserController.lambda$requirePermissions$1(UserController.java:123)

This is because the code in requirePermissions method doesn't count with the possibility for the request attribute "groups" not being present at all (null).

However, the implementation shown in the book only calls request.attribute("groups", ...) in UserController#authenticate.
That method is solely used for Basic authentication though (it returns very early if non-basic auth is used.

Since I'm using OAuth Token authentication (as implemented in chapter 7), it's failing for me.
Did I miss something or it's implemented later in the book?
Or is it just not supposed to be used with token authentication at the moment?

@jumarko
Copy link
Author

jumarko commented Aug 11, 2023

To workaround the problem I added a null check here: https://github.com/jumarko/api-security-in-action/blob/chapter8/natter-api/src/main/java/com/manning/apisecurityinaction/controllers/UserController.java#L123

            if (groups != null) { // TODO: this is needed because TokenController doesn't sets "groups" request attribute yet
                for (var group : groups) {
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant