// zxid.java - Java CGI script that calls libzxid using JNI // Copyright (c) 2007-2009 Symlabs (symlabs@symlabs.com), All Rights Reserved. // Author: Sampo Kellomaki (sampo@iki.fi) // This is confidential unpublished proprietary source code of the author. // NO WARRANTY, not even implied warranties. Contains trade secrets. // Distribution prohibited unless authorized in writing. // Licensed under Apache License 2.0, see file COPYING. // $Id: zxid.java,v 1.13 2009-11-29 12:23:06 sampo Exp $ // 12.1.2007, created --Sampo import zxidjava.*; public class zxid { static { System.loadLibrary("zxidjni"); } public static void main(String argv[]) throws java.io.IOException { int ret; zx_str rets; zxid_conf cf; System.err.print("Start...\n"); cf = zxidjni.new_conf("/var/zxid/"); String url = "https://sp1.zxidsp.org:8443/zxid-java.sh"; String cdc_url = "https://sp1.zxidcommon.org:8443/zxid-java.sh"; zxidjni.url_set(cf, url); zxidjni.set_opt(cf, 1, 1); String qs = System.getenv("QUERY_STRING"); // Deprecation warnings about this are bogus and indicative of Java designer's disregard of their user base - indeed disconnect from reality, see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4199068 zxid_cgi cgi = zxidjni.new_cgi(cf, qs); char op = cgi.getOp(); if (op == 'P') { int len = Integer.parseInt(System.getenv("CONTENT_LENGTH")); byte[] b = new byte[len]; int got = System.in.read(b, 0, len); qs = new String(b, 0, got); System.err.print("post\n"); zxidjni.parse_cgi(cgi, qs); op = cgi.getOp(); } if (op == 0) op = 'M'; zxid_ses ses; String sid = cgi.getSid(); if (sid != null && sid.length() > 0) { ses = zxidjni.fetch_ses(cf, sid); if (ses != null) { if (mgmt_screen(cf, cgi, ses, op) != 0) System.exit(0); } } ses = zxidjni.fetch_ses(cf, ""); // Just allocate an empty one System.err.println("Not logged in case op="+op+" qs:"+qs); switch (op) { case 'M': // Invoke LECP or redirect to CDC reader. //if (zxidjni.lecp_check(cf, cgi) != 0) System.exit(0); System.out.print("Location: " + cdc_url + "?o=C\r\n\r\n"); System.exit(0); case 'C': // CDC Read: Common Domain Cookie Reader zxidjni.cdc_read(cf, cgi); System.exit(0); case 'E': // Return from CDC read, or start here to by-pass CDC read. //if (zxidjni.lecp_check(cf, cgi) != 0) System.exit(0); if (zxidjni.cdc_check(cf, cgi) != 0) System.exit(0); break; case 'L': System.err.print("Start login\n"); url = zxidjni.start_sso_url(cf, cgi).getS(); if (url.length() > 0) { System.err.print("login redir\n"); System.out.print("Location: " + url + "\r\n\r\n"); System.exit(0); } System.err.print("Login trouble"); break; case 'A': ret = zxidjni.sp_deref_art(cf, cgi, ses); System.err.println("deref art ret="+ret); if (ret == zxidjniConstants.ZXID_REDIR_OK) System.exit(0); if (ret == zxidjniConstants.ZXID_SSO_OK) if (mgmt_screen(cf, cgi, ses, op) != 0) System.exit(0); break; case 'P': // *** broken with newer swig generated -noproxy code rets = zxidjni.sp_dispatch(cf, cgi, ses); System.err.println("saml_resp ret=" + rets); if (rets.getS() == "O") System.exit(0); if (rets.getS() == "K") if (mgmt_screen(cf, cgi, ses, op) != 0) System.exit(0); break; case 'Q': rets = zxidjni.sp_dispatch(cf, cgi, ses); if (rets.getS() == "O") System.exit(0); if (rets.getS() == "K") if (mgmt_screen(cf, cgi, ses, op) != 0) System.exit(0); break; case 'B': System.out.print("CONTENT-TYPE: text/xml\r\n\r\n"); String md = zxidjni.sp_meta(cf, cgi).getS(); System.out.print(md); System.exit(0); default: System.err.println("Unknown op="+op); } System.out.print("CONTENT-TYPE: text/html\r\n\r\n"); System.out.print("ZXID SP Java SSO\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("

ZXID SP Java Federated SSO (user NOT logged in, no session.)

\n");
      System.out.print("
\n"); System.out.print("

Login Using New IdP

\n"); System.out.print("A new IdP is one whose metadata we do not have yet. We need to know\n"); System.out.print("the Entity ID in order to fetch the metadata using the well known\n"); System.out.print("location method. You will need to ask the adminstrator of the IdP to\n"); System.out.print("tell you what the EntityID is.\n"); System.out.print("

IdP EntityID URL \n"); System.out.print("\n"); System.out.print("\n"); zxid_entity idp = zxidjni.load_cot_cache(cf); if (idp != null) { System.out.print("

Login Using Known IdP

\n"); while (idp != null) { String eid = idp.getEid(); int eid_len = idp.getEid_len(); //eid = substr($eid, 0, $eid_len); //warn "eid_len($eid_len) eid($eid)"; System.out.print("\n"); System.out.print("\n"); idp = idp.getN(); } } String version_str = zxidjni.version_str(); System.out.print("

CoT configuration parameters your IdP may need to know

\n"); System.out.print("Entity ID of this SP: "+url+"?o=B (Click on the link to fetch SP metadata.)\n"); System.out.print("

Technical options (typically hidden fields on production site)

\n"); System.out.print(" Allow new federation to be created
\n"); System.out.print(" Do not allow IdP to interact (e.g. ask password) (IsPassive flag)
\n"); System.out.print(" IdP should reauthenticate user (ForceAuthn flag)
\n"); System.out.print("NID Format:
\n"); System.out.print("Affiliation:
\n"); System.out.print("Consent:
\n"); System.out.print("Authn Req Context:
\n"); System.out.print("Matching Rule:
\n"); System.out.print("

zxid.org " + version_str); } public static int mgmt_screen(zxid_conf cf, zxid_cgi cgi, zxid_ses ses, char op) { int ret; String msg; zx_str rets; System.err.print("mgmt op=" + op); switch (op) { case 'l': zxidjni.del_ses(cf, ses); msg = "Local logout Ok. Session terminated."; return 0; // Simply abandon local session. Falls thru to login screen. case 'r': zxidjni.sp_slo_redir(cf, cgi, ses); zxidjni.del_ses(cf, ses); return 1; // Redirect already happened. Do not show login screen. case 's': zxidjni.sp_slo_soap(cf, cgi, ses); zxidjni.del_ses(cf, ses); msg = "SP Initiated logout (SOAP). Session terminated."; return 0; // Falls thru to login screen. case 't': zxidjni.sp_mni_redir(cf, cgi, ses, null); return 1; // Redirect already happened. Do not show login screen. case 'u': zxidjni.sp_mni_soap(cf, cgi, ses, null); msg = "SP Initiated defederation (SOAP)."; break; case 'P': rets = zxidjni.sp_dispatch(cf, cgi, ses); if (rets.getS() == "O") return 0; if (rets.getS() == "K") return 1; // REDIR OK break; case 'Q': rets = zxidjni.sp_dispatch(cf, cgi, ses); if (rets.getS() == "O") return 0; if (rets.getS() == "K") return 1; // REDIR OK break; } String sid = ses.getSid(); String nid = ses.getNid(); System.out.print("CONTENT-TYPE: text/html\r\n\r\n"); System.out.print("ZXID SP Mgmt\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("

ZXID SP Java Management (user logged in, session active)

\n
\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("

Technical options (typically hidden fields on production site)

\n"); System.out.print("sid("+sid+") nid("+nid+") Reload\n"); System.out.print("

\n"); System.out.print("zxid.org\n"); return 1; } } /* EOF */