Commit 977dcd76 authored by Gradl, Tobias's avatar Gradl, Tobias
Browse files

5: Implement compatible User/Role behavior from CommonProfile

Task-Url: #5
parent 1e8a9d66
Pipeline #17544 passed with stage
in 1 minute and 50 seconds
......@@ -5,21 +5,15 @@ plugins {
description = 'dariahsp - core library'
dependencies {
implementation "de.unibamberg.minf.core:core-metamodel:$coreVersion"
api "de.unibamberg.minf.core:core-metamodel:$coreVersion"
api "org.pac4j:spring-security-pac4j:$springPac4jVersion"
api "org.pac4j:spring-webmvc-pac4j:$webmvcPac4jVersion"
api "org.pac4j:pac4j-saml:$pac4jVersion"
api "org.pac4j:pac4j-http:$pac4jVersion"
implementation 'org.springframework.boot:spring-boot-starter-web'
//implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
compileOnly 'org.projectlombok:lombok'
//developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
//providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
implementation "org.pac4j:spring-security-pac4j:$springPac4jVersion"
implementation "org.pac4j:spring-webmvc-pac4j:$webmvcPac4jVersion"
implementation "org.pac4j:pac4j-saml:$pac4jVersion"
implementation "org.pac4j:pac4j-http:$pac4jVersion"
implementation "org.aspectj:aspectjweaver"
testImplementation librarySets.commonTest
}
......
package eu.dariah.de.dariahsp.authenticator;
import java.util.Optional;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.credentials.Credentials;
import org.pac4j.core.profile.UserProfile;
import org.pac4j.core.profile.creator.AuthenticatorProfileCreator;
import org.pac4j.core.profile.creator.ProfileCreator;
import eu.dariah.de.dariahsp.model.ExtendedUserProfile;
import eu.dariah.de.dariahsp.model.UserImpl;
public class UserProfileCreator<C extends Credentials> implements ProfileCreator<C> {
public final static UserProfileCreator INSTANCE = new UserProfileCreator<>();
@Override
public Optional<UserProfile> create(final C credentials, final WebContext context) {
if (credentials.getUserProfile()==null) {
return Optional.empty();
}
ExtendedUserProfile profile = new ExtendedUserProfile(credentials.getUserProfile());
return Optional.ofNullable(profile);
}
}
......@@ -12,6 +12,9 @@ import org.pac4j.core.authorization.authorizer.RequireAnyRoleAuthorizer;
import org.pac4j.core.client.Client;
import org.pac4j.core.client.Clients;
import org.pac4j.core.config.Config;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.profile.ProfileManager;
import org.pac4j.core.profile.factory.ProfileManagerFactory;
import org.pac4j.http.client.indirect.FormClient;
import org.pac4j.saml.client.SAML2Client;
import org.pac4j.saml.config.SAML2Configuration;
......@@ -28,6 +31,7 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import eu.dariah.de.dariahsp.CustomAuthorizer;
import eu.dariah.de.dariahsp.authenticator.LocalUsernamePasswordAuthenticator;
import eu.dariah.de.dariahsp.authenticator.UserProfileCreator;
import eu.dariah.de.dariahsp.metadata.MetadataHelper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
......@@ -71,10 +75,13 @@ public class SecurityConfig {
List<Client> clients = new ArrayList<>();
Optional<SAML2Client> samlClient = getSamlClient();
Optional<FormClient> formClient = getFormClient();
if (samlClient.isPresent()) {
samlClient.get().setProfileCreator(UserProfileCreator.INSTANCE);
clients.add(samlClient.get());
}
if (formClient.isPresent()) {
formClient.get().setProfileCreator(UserProfileCreator.INSTANCE);
clients.add(formClient.get());
}
......
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package eu.dariah.de.dariahsp.core;
public class Library {
public boolean someLibraryMethod() {
return true;
}
}
package eu.dariah.de.dariahsp.model;
import java.util.Set;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.core.util.CommonHelper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper=true)
public class ExtendedUserProfile extends CommonProfile {
private Set<String> externalRoles;
public ExtendedUserProfile(CommonProfile profile) {
this.setId(profile.getId());
for (String key : profile.getAttributes().keySet()) {
this.addAttribute(key, profile.getAttributes().get(key));
}
for (String key : profile.getAuthenticationAttributes().keySet()) {
this.addAuthenticationAttribute(key, profile.getAuthenticationAttributes().get(key));
}
this.setRemembered(profile.isRemembered());
this.setExternalRoles(profile.getRoles());
this.setPermissions(profile.getPermissions());
this.setClientName(profile.getClientName());
this.setLinkedId(profile.getLinkedId());
}
public UserImpl toUser() {
UserImpl user = new UserImpl();
// TODO: Complete this...
user.setUsername(this.getUsername());
return user;
}
@Override
public String toString() {
return super.toString();
}
}
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package eu.dariah.de.dariahsp.core;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class LibraryTest {
@Test void testSomeLibraryMethod() {
Library classUnderTest = new Library();
assertTrue(classUnderTest.someLibraryMethod(), "someLibraryMethod should return 'true'");
}
}
......@@ -4,24 +4,19 @@ plugins {
dependencies {
implementation project(':dariahsp-core')
//implementation "de.unibamberg.minf.core:core-metamodel:$coreVersion"
//implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
/*implementation "org.pac4j:spring-security-pac4j:$springPac4jVersion"
implementation "org.pac4j:spring-webmvc-pac4j:$webmvcPac4jVersion"
implementation "org.pac4j:pac4j-saml:$pac4jVersion"
implementation "org.pac4j:pac4j-http:$pac4jVersion"
testImplementation librarySets.commonTest
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}*/
}
}
bootJar {
......
Supports Markdown
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