cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dev at Weitling <...@weitling.net>
Subject Authentication with Javascript
Date Fri, 03 Nov 2006 05:18:55 GMT
Hello Cocooners,

while looking at the authentication-fw sample I tried to rewrite and
merge the sample.js and auth.js files to match my needs. I want to keep
the referring URL, so that after successful login the user is at the
place he wanted to be.
I would be glad if you'd have a look at my code or have some better ideas! (referringTarget
means
the url the user wanted to get to, entryPointTarget if started with
plain old login page).

Regards,
Florian

##### sitemap snippet #####

      <map:match pattern="">
        <map:redirect-to uri="login" session="true"/>
      </map:match>

      <map:match pattern="login">
        <map:call function="isLoggedIn">
          <map:parameter name="handler" value="flowdemohandler"/>
          <map:parameter name="entryPointTarget" value="protected"/>
        </map:call>
      </map:match>

      <map:match pattern="do-login">
        <map:call function="login">
          <map:parameter name="handler" value="flowdemohandler"/>
          <map:parameter name="parameter_name" value="{request-param:username}"/>
          <map:parameter name="entryPointTarget" value="protected"/>
        </map:call>
      </map:match>

      <map:match pattern="protected">
        <map:call function="protect">
          <map:parameter name="handler" value="flowdemohandler"/>
          <map:parameter name="entryPointTarget" value="internal/protected"/>
        </map:call>
      </map:match>

      <map:match pattern="do-logout">
        <map:call function="logout">
          <map:parameter name="handler" value="flowdemohandler"/>
        </map:call>
      </map:match>
    </map:pipeline>


##### myLogin.js #####

function protect () {
	var referringTarget = cocoon.parameters["referringTarget"];
	var entryPointTarget = cocoon.parameters["entryPointTarget"];
	var authMgr = null;
	try {
		authMgr = cocoon.getComponent(Packages.org.apache.cocoon.webapps.authentication.AuthenticationManager.ROLE);
		if ( authMgr.checkAuthentication(null,cocoon.parameters["handler"],"") ) {
			if ( referringTarget != null ) {
				cocoon.sendPage(referringTarget);
			}
			else {
				if ( entryPointTarget != null ) {
					cocoon.sendPage(entryPointTarget);
				}
				else {
					throw new Error("No target given");
				}
			}
		}
		else {
			/** depends on DefaultAuthenticationManager ! */
			cocoon.redirectTo(authMgr.getForwardingURI(handler));
		}
	}
	finally {
		if ( authMgr != null ) {
			cocoon.releaseComponent(authMgr);
		}
	}
}

function isLoggedIn () {
	var referringTarget = cocoon.parameters["referringTarget"];
	var entryPointTarget = cocoon.parameters["entryPointTarget"];
	var authMgr = null;
	try {
		authMgr = cocoon.getComponent(Packages.org.apache.cocoon.webapps.authentication.AuthenticationManager.ROLE);
		if ( authMgr.isAuthenticated(cocoon.parameters["handler"]) != null ) {
			if ( referringTarget != null ) {
				cocoon.sendPage(referringTarget);
			}
			else {
				if ( entryPointTarget != null ) {
					cocoon.sendPage(entryPointTarget);
				}
				else {
					throw new Error("No target given");
				}
			}
		}
		else {
			/** depends on DefaultAuthenticationManager ! */
			cocoon.redirectTo(authMgr.getForwardingURI(cocoon.parameters["handler"]));
		}
	}
	finally {
		if ( authMgr != null ) {
			cocoon.releaseComponent(authMgr);
		}
	}
}

function login () {
	var referringTarget = cocoon.parameters["referringTarget"];
	var entryPointTarget = cocoon.parameters["entryPointTarget"];
	var authMgr = null;
	try {
		authMgr = cocoon.getComponent(Packages.org.apache.cocoon.webapps.authentication.AuthenticationManager.ROLE);
		if ( authMgr.isAuthenticated(cocoon.parameters["handler"]) != null ) {
			if ( referringTarget != null ) {
				cocoon.sendPage(referringTarget);
			}
			else {
				if ( entryPointTarget != null ) {
					cocoon.sendPage(entryPointTarget);
				}
				else {
					throw new Error("No target given");
				}
			}
		}
		else {
			var authParams = new Packages.org.apache.excalibur.source.SourceParameters();
			for (var name in params) {
				if ( name.startsWith("parameter_") ) {
					authParams.setParameter(name.substring(10),params[name]);
				}
			}
			if ( authMgr.login(handler,"",authParams) != null ) {
				if ( referringTarget != null ) {
					cocoon.sendPage(referringTarget);
				}
				else {
					if ( entryPointTarget != null ) {
						cocoon.sendPage(entryPointTarget);
					}
					else {
						throw new Error("No target given");
					}
				}
			}
		}
	}
	finally {
		if ( authMgr != null ) {
			cocoon.releaseComponent(authMgr);
		}
	}
}

function logout () {
	var authMgr;
	try {
		authMgr = cocoon.getComponent(Packages.org.apache.cocoon.webapps.authentication.AuthenticationManager.ROLE);
		var state = authMgr.getState();
		var handler = cocoon.parameters["handler"];
		if ( handler == null ) {
			if ( state != null ) {
				handler = state.getHandlerName();
 			}
			else {
				throw new Error("LogoutAction requires at least the handler parameter.");
			}
		}
		authMgr.logout(handler,Packages.org.apache.cocoon.webapps.authentication.AuthenticationConstants.LOGOUT_MODE_IMMEDIATELY);
		cocoon.redirectTo(authMgr.getForwardingURI(handler));
	}
	finally {
		cocoon.releaseComponent(authMgr);
	}
}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message