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

[ELY-2413] Re-authentication after reboot, even though HttpSession are persisted #1747

Merged
merged 1 commit into from
Sep 12, 2022

Conversation

Skyllarr
Copy link
Contributor

@Skyllarr Skyllarr commented Sep 5, 2022

Comment on lines 105 to 111
private Set<String> rolesToSet(Roles roles) {
Iterator<String> iterator = roles.iterator();
Set<String> resultSet = new HashSet<>();
while (iterator.hasNext()) {
resultSet.add(iterator.next());
}
return resultSet;
Copy link
Contributor

@pferraro pferraro Sep 5, 2022

Choose a reason for hiding this comment

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

Given that this set will be generated per user and will typically be small in size, I might suggest using a more lightweight data structure (e.g. TreeSet) and leverage empty and singleton implementations if possible.
e.g.

    private Set<String> rolesToSet(Roles roles) {
        Iterator<String> iterator = roles.iterator();
        if (!iterator.hasNext()) return Set.of();
        String role = iterator.next();
        if (!iterator.hasNext()) return Set.of(role);
        Set<String> result = new TreeSet<>();
        result.add(role);
        while (iterator.hasNext()) {
            result.add(iterator.next());
        }
        return result;
    }

Perhaps this logic could reside within a Roles.toSet() method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pferraro Good points! I have moved the method to the Roles class and I have used your TreeSet approach. I just modified it slightly to not use Set.of because we have to use Java 8 APIs. Thank you!

@fjuma fjuma requested review from pferraro and darranl and removed request for pferraro September 8, 2022 14:21
static Set<String> rolesToSet(Roles roles) {
Assert.checkNotNullParam("roles", roles);
if (roles.isEmpty()) {
return new TreeSet<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

return Collections.emptySet();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Iterator<String> iterator = roles.iterator();
String role = iterator.next();
if (!iterator.hasNext()) {
return new TreeSet<>(Collections.singleton(role));
Copy link
Contributor

Choose a reason for hiding this comment

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

return Collections.singleton(role);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

*/
static Set<String> rolesToSet(Roles roles) {
Assert.checkNotNullParam("roles", roles);
if (roles.isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Roles.isEmpty() internally creates an Iterator, so you can avoid creating the iterator twice by testing for empty via the Iterator (created in line 146).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pferraro thanks, updated

* @param roles collection (not {@code null})
* @return the set of role names (must not be {@code null})
*/
static Set<String> rolesToSet(Roles roles) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use a normal public method instead of a static method?
e.g.

Set<String> toSet();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pferraro this is an interface. Also the above static Roles fromSet(Set<String> set) method is static so it is congruent now

Copy link
Contributor

@pferraro pferraro Sep 8, 2022

Choose a reason for hiding this comment

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

Interfaces can contain default method implementations.
e.g.

default Set<String> toSet() {
    // ...
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Anyway - this is just (my) stylistic preference, and does not need to block approval.

@@ -130,6 +132,30 @@ public boolean isEmpty() {
};
}

/**
* Construct a new roles collection from a set.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest that this method return an immutable view of the roles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pferraro Thank you, fixed

while (iterator.hasNext()) {
result.add(iterator.next());
}
return result;
Copy link
Contributor

Choose a reason for hiding this comment

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

return Collections.unmodifiableSet(result);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@fjuma fjuma merged commit 9c4de97 into wildfly-security:2.x Sep 12, 2022
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

Successfully merging this pull request may close these issues.

4 participants