org.wildfly.elytron.web.undertow.common.ExternalAuthenticationBase Maven / Gradle / Ivy
/*
* Copyright 2020 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wildfly.elytron.web.undertow.common;
import static org.wildfly.security.password.interfaces.ClearPassword.ALGORITHM_CLEAR;
import java.security.Principal;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Rule;
import org.wildfly.security.auth.SupportLevel;
import org.wildfly.security.auth.permission.LoginPermission;
import org.wildfly.security.auth.realm.SimpleMapBackedSecurityRealm;
import org.wildfly.security.auth.realm.SimpleRealmEntry;
import org.wildfly.security.auth.server.RealmIdentity;
import org.wildfly.security.auth.server.RealmUnavailableException;
import org.wildfly.security.auth.server.SecurityDomain;
import org.wildfly.security.auth.server.SecurityRealm;
import org.wildfly.security.credential.Credential;
import org.wildfly.security.credential.PasswordCredential;
import org.wildfly.security.evidence.Evidence;
import org.wildfly.security.password.PasswordFactory;
import org.wildfly.security.password.spec.ClearPasswordSpec;
import org.wildfly.security.permission.PermissionVerifier;
/**
* Test case to test the HTTP External mechanism where authentication is backed by Elytron.
*
* @author Ashley Abdel-Sayed
*/
public abstract class ExternalAuthenticationBase extends AbstractHttpServerMechanismTest {
protected ExternalAuthenticationBase() throws Exception {
}
@Rule
public UndertowServer server = createUndertowServer();
private AtomicInteger realmIdentityInvocationCount = new AtomicInteger(0);
@Override
protected String getMechanismName() {
return "EXTERNAL";
}
@Override
protected SecurityDomain doCreateSecurityDomain() throws Exception {
PasswordFactory passwordFactory = PasswordFactory.getInstance(ALGORITHM_CLEAR);
Map passwordMap = new HashMap<>();
passwordMap.put("remoteUser", new SimpleRealmEntry(Collections.singletonList(new PasswordCredential(passwordFactory.generatePassword(new ClearPasswordSpec("doesnotmatter".toCharArray()))))));
SimpleMapBackedSecurityRealm delegate = new SimpleMapBackedSecurityRealm();
delegate.setPasswordMap(passwordMap);
SecurityRealm securityRealm = new SecurityRealm() {
@Override
public RealmIdentity getRealmIdentity(Principal principal) throws RealmUnavailableException {
realmIdentityInvocationCount.incrementAndGet();
return delegate.getRealmIdentity(principal);
}
@Override
public SupportLevel getCredentialAcquireSupport(Class extends Credential> credentialType, String algorithmName, AlgorithmParameterSpec algorithmParameterSpec) throws RealmUnavailableException {
return delegate.getCredentialAcquireSupport(credentialType, algorithmName, algorithmParameterSpec);
}
@Override
public SupportLevel getEvidenceVerifySupport(Class extends Evidence> evidenceType, String algorithmName) throws RealmUnavailableException {
return delegate.getEvidenceVerifySupport(evidenceType, algorithmName);
}
};
SecurityDomain.Builder builder = SecurityDomain.builder()
.setDefaultRealmName("TestRealm");
builder.addRealm("TestRealm", securityRealm).build();
builder.setPermissionMapper((principal, roles) -> PermissionVerifier.from(new LoginPermission()));
return builder.build();
}
protected abstract UndertowServer createUndertowServer() throws Exception;
}