Path: blob/master/test/jdk/com/sun/security/sasl/PropertiesFileCallbackHandler.java
41152 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.security.auth.callback.*;24import java.util.Map;25import java.util.Properties;26import java.io.*;27import javax.security.sasl.AuthorizeCallback;28import javax.security.sasl.RealmCallback;2930public final class PropertiesFileCallbackHandler implements CallbackHandler {31private Properties pwDb, namesDb, proxyDb;3233/**34* Contents of files are in the Properties file format.35*36* @param pwFile name of file containing name/password pairs37* @param namesFile name of file containing name to canonicalized name38* @param proxyFile name of file containing authname to list of authzids39*/40public PropertiesFileCallbackHandler(String pwFile, String namesFile,41String proxyFile) throws IOException {42String dir = System.getProperty("test.src");43if (dir == null) {44dir = ".";45}46dir = dir + "/";4748if (pwFile != null) {49pwDb = new Properties();50pwDb.load(new FileInputStream(dir+pwFile));51}5253if (namesFile != null) {54namesDb = new Properties();55namesDb.load(new FileInputStream(dir+namesFile));56}5758if (proxyFile != null) {59proxyDb = new Properties();60proxyDb.load(new FileInputStream(dir+proxyFile));61}62}6364public void handle(Callback[] callbacks)65throws UnsupportedCallbackException {66NameCallback ncb = null;67PasswordCallback pcb = null;68AuthorizeCallback acb = null;69RealmCallback rcb = null;7071for (int i = 0; i < callbacks.length; i++) {72if (callbacks[i] instanceof NameCallback) {73ncb = (NameCallback) callbacks[i];74} else if (callbacks[i] instanceof PasswordCallback) {75pcb = (PasswordCallback) callbacks[i];76} else if (callbacks[i] instanceof AuthorizeCallback) {77acb = (AuthorizeCallback) callbacks[i];78} else if (callbacks[i] instanceof RealmCallback) {79rcb = (RealmCallback) callbacks[i];80} else {81throw new UnsupportedCallbackException(callbacks[i]);82}83}8485// Process retrieval of password; can get password iff86// username is available in NameCallback87//88// Ignore realm for now; could potentially use different dbs for89// different realms9091if (pcb != null && ncb != null) {92String username = ncb.getDefaultName();93String pw = pwDb.getProperty(username);94if (pw != null) {95char[] pwchars = pw.toCharArray();96pcb.setPassword(pwchars);97// Clear pw98for (int i = 0; i <pwchars.length; i++) {99pwchars[i] = 0;100}101102// Set canonicalized username if any103String canonAuthid =104(namesDb != null? namesDb.getProperty(username) : null);105if (canonAuthid != null) {106ncb.setName(canonAuthid);107}108}109}110111// Check for authorization112113// Ignore realm for now; could potentially use different dbs for114// different realms115116if (acb != null) {117String authid = acb.getAuthenticationID();118String authzid = acb.getAuthorizationID();119if (authid.equals(authzid)) {120// Self is always authorized121acb.setAuthorized(true);122123} else {124// Check db for allowed authzids125String authzes = (proxyDb != null ? proxyDb.getProperty(authid)126: null);127if (authzes != null && authzes.indexOf(authzid) >= 0) {128// XXX need to search for subtrings or use StringTokenizer129// to avoid incorrectly matching subnames130acb.setAuthorized(true);131}132}133134if (acb.isAuthorized()) {135// Set canonicalized name136String canonAuthzid = (namesDb != null ?137namesDb.getProperty(authzid) : null);138if (canonAuthzid != null) {139acb.setAuthorizedID(canonAuthzid);140}141}142}143}144}145146147