Commit 61c0ae24 authored by Gradl, Tobias's avatar Gradl, Tobias
Browse files

Merge branch 'v2.1-dev' into 'v2.x-master'

V2.1 dev

See merge request !3
parents 8cddb2b1 f43d1645
Pipeline #17953 passed with stages
in 7 minutes and 16 seconds
......@@ -5,7 +5,7 @@ plugins {
allprojects {
group = 'eu.dariah.de'
version = '2.0.0-RELEASE'
version = '2.1.0-SNAPSHOT'
repositories {
maven {
......
package eu.dariah.de.dariahsp.authentication;
import java.util.ArrayList;
import java.util.HashSet;
import org.pac4j.core.context.WebContext;
......@@ -10,21 +9,16 @@ import org.pac4j.core.exception.CredentialsException;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.core.util.CommonHelper;
import org.pac4j.core.util.Pac4jConstants;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import eu.dariah.de.dariahsp.config.local.LocalUsers;
import eu.dariah.de.dariahsp.model.RoleImpl;
import eu.dariah.de.dariahsp.model.UserImpl;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Getter @Setter
public class LocalUsernamePasswordAuthenticator implements Authenticator<UsernamePasswordCredentials>, UserDetailsService {
public class LocalUsernamePasswordAuthenticator implements Authenticator<UsernamePasswordCredentials> {
private LocalUsers[] localUserConfigurations;
private PasswordEncoder encoder;
......@@ -71,26 +65,4 @@ public class LocalUsernamePasswordAuthenticator implements Authenticator<Usernam
throw e;
}
}
@Override
public UserDetails loadUserByUsername(String username) {
if (localUserConfigurations==null) {
throw new UsernameNotFoundException("Username not found: no local users configured");
}
for (LocalUsers uc : localUserConfigurations) {
if (uc.getUsername().equals(username)) {
UserImpl u = new UserImpl();
u.setUsername(uc.getUsername());
u.setHashedPassword(uc.getPasshash());
if (uc.getRoles()!=null && !uc.getRoles().isEmpty()) {
u.setAuthorities(new ArrayList<>(uc.getRoles().size()));
for (String r : uc.getRoles()) {
u.getAuthorities().add(new RoleImpl(r));
}
}
return u;
}
}
throw new UsernameNotFoundException("Username not found: no local users configured");
}
}
package eu.dariah.de.dariahsp.model;
import java.util.ArrayList;
import java.util.Set;
import org.pac4j.core.profile.CommonProfile;
......@@ -40,4 +41,27 @@ public class ExtendedUserProfile extends CommonProfile {
"attributes", this.getAttributes(), "roles", this.getRoles(), "externalRoles", this.getExternalRoles(),
"isRemembered", this.isRemembered(), "clientName", this.getClientName());
}
/**
* Facilitates work with extension of UserPojo
* @param <T> extension of UserPojo
*
* @param pojo T
*/
public <T extends UserPojo> void fillUserPojo(T pojo) {
if (pojo==null) {
return;
}
pojo.setAuthorities(new ArrayList<>(this.getRoles()));
pojo.setId(this.getId());
pojo.setIssuer(this.getIssuerId());
pojo.setUsername(this.getUsername());
pojo.setExpired(this.isExpired());
}
public UserPojo toUserPojo() {
UserPojo u = new UserPojo();
this.fillUserPojo(u);
return u;
}
}
\ No newline at end of file
package eu.dariah.de.dariahsp.model;
import org.springframework.security.core.GrantedAuthority;
public interface Role extends GrantedAuthority {
public int getId();
public String getDescription();
}
package eu.dariah.de.dariahsp.model;
import lombok.Data;
@Data
public class RoleImpl implements Role {
private static final long serialVersionUID = 2655806586598209266L;
private String authority;
private String description;
public RoleImpl() {}
public RoleImpl(String authority) {
this.authority = authority;
}
@Override public int getId() { return 0; }
}
package eu.dariah.de.dariahsp.model;
import java.time.LocalDateTime;
import java.util.Collection;
import org.springframework.security.core.userdetails.UserDetails;
import de.unibamberg.minf.dme.model.base.Identifiable;
public interface User extends UserDetails, Identifiable {
public String getId();
public String getNameId();
public boolean isExpired();
public String getEndpointId();
public String getEndpointName();
public String getLanguage();
public void setEndpointId(String localDomain);
public void setEndpointName(String defaultLocalDomain);
public boolean isHasAllAttributes();
public void setHasAllAttributes(boolean b);
public void setAuthorities(Collection<Role> roles);
public Collection<Role> getRoles();
public void setUsername(String username);
public void setExpired(boolean b);
public void setLastLogin(LocalDateTime now);
}
package eu.dariah.de.dariahsp.model;
import java.time.LocalDateTime;
import java.util.Collection;
import lombok.Data;
@Data
public class UserImpl implements User {
private static final long serialVersionUID = -3955895740048975623L;
private Collection<Role> authorities;
protected String id;
private String endpointId;
private String endpointName;
private String username;
private String hashedPassword;
private boolean expired;
private String language;
private boolean hasAllAttributes;
private LocalDateTime lastLogin;
public UserImpl() {}
public UserImpl(User user, Collection<Role> authorities) {
this.id = user.getId();
this.username = user.getNameId();
this.expired = user.isExpired();
this.endpointId = user.getEndpointId();
this.language = user.getLanguage();
this.authorities = authorities;
this.endpointName = user.getEndpointName();
this.hasAllAttributes = false;
}
@Override public String getUsername() { return username; }
@Override public String getPassword() { return hashedPassword; }
@Override public Collection<Role> getAuthorities() { return authorities; }
@Override public boolean isAccountNonExpired() { return !expired; }
@Override public boolean isAccountNonLocked() { return !expired; }
@Override public boolean isCredentialsNonExpired() { return !expired; }
@Override public boolean isEnabled() { return !expired; }
@Override public String getNameId() { return username; }
@Override public Collection<Role> getRoles() { return this.getAuthorities(); }
}
package eu.dariah.de.dariahsp.model;
import java.time.LocalDateTime;
import java.util.Collection;
import de.unibamberg.minf.dme.model.base.BaseIdentifiable;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper=false)
public class UserPojo extends BaseIdentifiable {
private static final long serialVersionUID = -3955895740048975623L;
private Collection<String> authorities;
private String id;
private String issuer;
private String username;
private boolean expired;
private String language;
private LocalDateTime lastLogin;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment