-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in SHIBUI-975 (pull request #234)
SHIBUI-975
- Loading branch information
Showing
13 changed files
with
279 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...end/src/main/groovy/edu/internet2/tier/shibboleth/admin/ui/configuration/DevConfig.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.configuration | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.security.model.Role | ||
import edu.internet2.tier.shibboleth.admin.ui.security.model.User | ||
import edu.internet2.tier.shibboleth.admin.ui.security.repository.UserRepository | ||
import org.springframework.context.annotation.Profile | ||
import org.springframework.stereotype.Component | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
import javax.annotation.PostConstruct | ||
|
||
@Component | ||
@Profile('dev') | ||
class DevConfig { | ||
private final UserRepository adminUserRepository | ||
|
||
DevConfig(UserRepository adminUserRepository) { | ||
this.adminUserRepository = adminUserRepository | ||
} | ||
|
||
@Transactional | ||
@PostConstruct | ||
void createDevAdminUsers() { | ||
if (adminUserRepository.count() == 0) { | ||
def user = new User().with { | ||
username = 'admin' | ||
password = '{noop}adminpass' | ||
roles.add(new Role(name: 'ROLE_ADMIN')) | ||
it | ||
} | ||
|
||
adminUserRepository.save(user) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/security/model/Role.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.security.model; | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.domain.AbstractAuditable; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
import javax.persistence.CascadeType; | ||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.ManyToMany; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Models a basic administrative role concept in the system. | ||
* | ||
* @author Dmitriy Kopylenko | ||
*/ | ||
@Entity | ||
@NoArgsConstructor | ||
@Getter | ||
@Setter | ||
@EqualsAndHashCode(callSuper = true, exclude = "users") | ||
@ToString(exclude = "users") | ||
public class Role extends AbstractAuditable { | ||
|
||
@Column(unique = true) | ||
private String name; | ||
|
||
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "roles", fetch = FetchType.EAGER) | ||
private Set<User> users = new HashSet<>(); | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/security/model/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.security.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import edu.internet2.tier.shibboleth.admin.ui.domain.AbstractAuditable; | ||
import lombok.*; | ||
|
||
import javax.persistence.CascadeType; | ||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.JoinTable; | ||
import javax.persistence.ManyToMany; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Models a basic administrative user in the system. | ||
* | ||
* @author Dmitriy Kopylenko | ||
*/ | ||
@Entity | ||
@NoArgsConstructor | ||
@Getter | ||
@Setter | ||
@EqualsAndHashCode(callSuper = true, exclude = "roles") | ||
@ToString(exclude = "roles") | ||
public class User extends AbstractAuditable { | ||
|
||
@Column(nullable = false, unique = true) | ||
private String username; | ||
|
||
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) | ||
@Column(nullable = false) | ||
private String password; | ||
|
||
private String firstName; | ||
|
||
private String lastName; | ||
|
||
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) | ||
@ManyToMany(cascade = CascadeType.ALL) | ||
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id")) | ||
private Set<Role> roles = new HashSet<>(); | ||
} |
16 changes: 16 additions & 0 deletions
16
.../main/java/edu/internet2/tier/shibboleth/admin/ui/security/repository/RoleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.security.repository; | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.security.model.Role; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* Spring Data repository to manage entities of type {@link Role}. | ||
* | ||
* @author Dmitriy Kopylenko | ||
*/ | ||
public interface RoleRepository extends JpaRepository<Role, Long> { | ||
|
||
Optional<Role> findByName(final String name); | ||
} |
16 changes: 16 additions & 0 deletions
16
.../main/java/edu/internet2/tier/shibboleth/admin/ui/security/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.security.repository; | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.security.model.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* Spring Data repository to manage entities of type {@link User}. | ||
* | ||
* @author Dmitriy Kopylenko | ||
*/ | ||
public interface UserRepository extends JpaRepository<User, Long> { | ||
|
||
Optional<User> findByUsername(String username); | ||
} |
47 changes: 47 additions & 0 deletions
47
...java/edu/internet2/tier/shibboleth/admin/ui/security/springsecurity/AdminUserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.security.springsecurity; | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.security.model.Role; | ||
import edu.internet2.tier.shibboleth.admin.ui.security.model.User; | ||
import edu.internet2.tier.shibboleth.admin.ui.security.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Set; | ||
import static java.util.stream.Collectors.toSet; | ||
|
||
/** | ||
* Spring Security {@link UserDetailsService} implementation for local administration of admin users in the system. | ||
* | ||
* @author Dmitriy Kopylenko | ||
*/ | ||
@RequiredArgsConstructor | ||
public class AdminUserService implements UserDetailsService { | ||
|
||
private final UserRepository userRepository; | ||
|
||
@Override | ||
@Transactional(readOnly = true) | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
User user = userRepository | ||
.findByUsername(username) | ||
.orElseThrow(() -> new UsernameNotFoundException(String.format("User [%s] is not found", username))); | ||
|
||
Set<GrantedAuthority> grantedAuthorities = user.getRoles().stream() | ||
.map(Role::getName) | ||
.map(SimpleGrantedAuthority::new) | ||
.collect(toSet()); | ||
|
||
if (grantedAuthorities.isEmpty()) { | ||
//As defined by the UserDetailsService API contract | ||
throw new UsernameNotFoundException(String.format("No roles are defined for user [%s]", username)); | ||
} | ||
|
||
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), grantedAuthorities); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...u/internet2/tier/shibboleth/admin/ui/security/springsecurity/AdminUserServiceTests.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.security.springsecurity | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.security.repository.RoleRepository | ||
import edu.internet2.tier.shibboleth.admin.ui.security.repository.UserRepository | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException | ||
import org.springframework.test.context.ActiveProfiles | ||
import spock.lang.Specification | ||
|
||
/** | ||
* Tests for <code>AdminUserService</code> | ||
* | ||
* @author Dmitriy Kopylenko | ||
*/ | ||
@SpringBootTest | ||
@ActiveProfiles('dev') | ||
class AdminUserServiceTests extends Specification { | ||
|
||
@Autowired | ||
AdminUserService adminUserService | ||
|
||
@Autowired | ||
RoleRepository adminRoleRepository | ||
|
||
@Autowired | ||
UserRepository adminUserRepository | ||
|
||
|
||
def "Loading existing admin user with admin role"() { | ||
given: 'Valid user with admin role is available (loaded by Spring Boot Listener in dev profile)' | ||
def user = adminUserService.loadUserByUsername('admin') | ||
|
||
expect: | ||
user.username == 'admin' | ||
user.password == '{noop}adminpass' | ||
user.getAuthorities().size() == 1 | ||
user.getAuthorities()[0].authority == 'ROLE_ADMIN' | ||
user.enabled | ||
user.accountNonExpired | ||
user.credentialsNonExpired | ||
} | ||
|
||
def "Loading NON-existing admin user with admin role"() { | ||
when: 'Non-existent admin user is tried to be looked up' | ||
adminUserService.loadUserByUsername('nonexisting') | ||
|
||
then: | ||
thrown UsernameNotFoundException | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Security | ||
|
||
Security in the system is controlled by Spring Security. | ||
|
||
Currently, the following roles are recognized: | ||
|
||
1. `ADMIN` | ||
1. `USER` |