/**
* # Copyright (c) 2012, Perforce Software, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.perforce.p4javademo.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.perforce.p4java.server.callback.ISSOCallback;
/**
* Implementation:
*
* IOptionsServer server = ServerFactory.getOptionsServer("p4java://perforce:1666", null):
* server.registerSSOCallback(new SSOCallback(), "perforce:1666);
* server.setUserName("bluto");
* server.connect();
*
* @author jkovisto
*
*/
public class SSOCallback implements ISSOCallback {
public SSOCallback () {
}
public Status getSSOCredentials(StringBuffer credBuffer, String ssoKey, String userName) {
Status status = Status.UNSET;
if (credBuffer == null) {
throw new NullPointerException(
"NULL credBuffer passed to SSO callback");
}
String execCmd = System.getenv("P4LOGINSSO");
if (execCmd != null && execCmd.length() > 0) {
List<String> cmdList = new ArrayList<String>();
cmdList.add(execCmd);
if (userName != null) {
cmdList.add(userName);
}
if (ssoKey != null) {
cmdList.add(ssoKey);
}
ProcessBuilder processBuilder = new ProcessBuilder(cmdList);
Process proc = null;
InputStream inStream = null;
int exitCode = 0;
try {
proc = processBuilder.start();
inStream = proc.getInputStream();
int bytesRead = 0;
byte[] bytes = new byte[1024];
while ((bytesRead = inStream.read(bytes)) >= 0) {
credBuffer.append(new String(bytes, 0, bytesRead));
}
stripEOL(credBuffer);
exitCode = proc.waitFor();
if (exitCode == 0) {
status = Status.PASS;
} else {
status = Status.FAIL;
}
} catch (IOException exc) {
System.out.println("Error running P4LOGINSSO executable.");
exc.printStackTrace();
status = Status.FAIL;
} catch (InterruptedException exc) {
System.out.println("Error running P4LOGINSSO executable");
exc.printStackTrace();
status = Status.FAIL;
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException exc) {
// Ignore
}
}
}
//System.out.println("Cred: " + credBuffer.toString());
} else {
System.out.println(
"SSO login not attempted because P4LOGINSSO not set in environment.");
}
return status;
}
private void stripEOL(StringBuffer buffer) {
// Strip trailing eol
int length = buffer.length();
if (length > 0) {
if (buffer.charAt(length - 1) == '\n') {
buffer.deleteCharAt(length - 1);
}
}
length = buffer.length();
if (length > 0) {
if (buffer.charAt(length - 1) == '\r') {
buffer.deleteCharAt(length - 1);
}
}
}
}