diff --git a/jetty-security/src/main/java/org/eclipse/jetty/security/HashLoginService.java b/jetty-security/src/main/java/org/eclipse/jetty/security/HashLoginService.java index 360a8af3e4a..87559a22938 100644 --- a/jetty-security/src/main/java/org/eclipse/jetty/security/HashLoginService.java +++ b/jetty-security/src/main/java/org/eclipse/jetty/security/HashLoginService.java @@ -22,6 +22,7 @@ import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import org.eclipse.jetty.server.UserIdentity; import org.eclipse.jetty.util.log.Log; @@ -163,9 +164,9 @@ public class HashLoginService extends AbstractLoginService if (roles == null) return null; - List list = new ArrayList<>(); - for (RolePrincipal r:roles) - list.add(r.getName()); + List list = roles.stream() + .map( rolePrincipal -> rolePrincipal.getName() ) + .collect( Collectors.toList() ); return list.toArray(new String[roles.size()]); } diff --git a/jetty-security/src/test/java/org/eclipse/jetty/security/UserStoreTest.java b/jetty-security/src/test/java/org/eclipse/jetty/security/UserStoreTest.java new file mode 100644 index 00000000000..cd61c4c5cb2 --- /dev/null +++ b/jetty-security/src/test/java/org/eclipse/jetty/security/UserStoreTest.java @@ -0,0 +1,70 @@ +// +// ======================================================================== +// Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.security; + +import org.eclipse.jetty.server.UserIdentity; +import org.eclipse.jetty.util.security.Credential; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class UserStoreTest +{ + UserStore userStore; + + @Before + public void setup() { + userStore = new UserStore(); + } + + @Test + public void addUser() + { + this.userStore.addUser( "foo", Credential.getCredential( "beer" ), new String[]{"pub"} ); + Assert.assertEquals(1, this.userStore.getKnownUserIdentities().size()); + UserIdentity userIdentity = this.userStore.getUserIdentity( "foo" ); + Assert.assertNotNull( userIdentity ); + Assert.assertEquals( "foo", userIdentity.getUserPrincipal().getName() ); + Set + roles = userIdentity.getSubject().getPrincipals( AbstractLoginService.RolePrincipal.class); + List list = roles.stream() + .map( rolePrincipal -> rolePrincipal.getName() ) + .collect( Collectors.toList() ); + Assert.assertEquals(1, list.size()); + Assert.assertEquals( "pub", list.get( 0 ) ); + } + + @Test + public void removeUser() + { + this.userStore.addUser( "foo", Credential.getCredential( "beer" ), new String[]{"pub"} ); + Assert.assertEquals(1, this.userStore.getKnownUserIdentities().size()); + UserIdentity userIdentity = this.userStore.getUserIdentity( "foo" ); + Assert.assertNotNull( userIdentity ); + Assert.assertEquals( "foo", userIdentity.getUserPrincipal().getName() ); + userStore.removeUser( "foo" ); + userIdentity = this.userStore.getUserIdentity( "foo" ); + Assert.assertNull( userIdentity ); + } + +}