io.undertow.examples.security.basic.MapIdentityManager Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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 io.undertow.examples.security.basic;
import io.undertow.security.idm.Account;
import io.undertow.security.idm.Credential;
import io.undertow.security.idm.IdentityManager;
import io.undertow.security.idm.PasswordCredential;
import java.security.Principal;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
/**
* A simple {@link IdentityManager} implementation, that just takes a map of users to their
* password.
*
* This is in now way suitable for real world production use.
*
*
* @author Stuart Douglas
*/
class MapIdentityManager implements IdentityManager {
private final Map users;
MapIdentityManager(final Map users) {
this.users = users;
}
@Override
public Account verify(Account account) {
// An existing account so for testing assume still valid.
return account;
}
@Override
public Account verify(String id, Credential credential) {
Account account = getAccount(id);
if (account != null && verifyCredential(account, credential)) {
return account;
}
return null;
}
@Override
public Account verify(Credential credential) {
// TODO Auto-generated method stub
return null;
}
private boolean verifyCredential(Account account, Credential credential) {
if (credential instanceof PasswordCredential) {
char[] password = ((PasswordCredential) credential).getPassword();
char[] expectedPassword = users.get(account.getPrincipal().getName());
return Arrays.equals(password, expectedPassword);
}
return false;
}
private Account getAccount(final String id) {
if (users.containsKey(id)) {
return new Account() {
private final Principal principal = new Principal() {
@Override
public String getName() {
return id;
}
};
@Override
public Principal getPrincipal() {
return principal;
}
@Override
public Set getRoles() {
return Collections.emptySet();
}
};
}
return null;
}
}