package com.zyeeda.framework.security.internal;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.tapestry5.ioc.annotations.Marker;
import org.apache.tapestry5.ioc.annotations.Primary;
import org.apache.tapestry5.ioc.annotations.ServiceId;
import org.apache.tapestry5.ioc.services.RegistryShutdownHub;

import com.zyeeda.framework.openid.consumer.shiro.BypassCredentialsMatcher;
import com.zyeeda.framework.openid.consumer.shiro.OpenIdConsumerRealm;
import com.zyeeda.framework.security.SecurityService;
import com.zyeeda.framework.service.AbstractService;

@Marker(Primary.class)
@ServiceId("openid-consumer-security-service")
public class OpenIdConsumerSecurityServiceProvider extends AbstractService implements SecurityService<SecurityManager> {
	
	private SecurityManager securityMgr;

	public OpenIdConsumerSecurityServiceProvider(RegistryShutdownHub shutdownHub) {
		super(shutdownHub);
		
		this.securityMgr = new ShiroSecurityManager();
	}

	@Override
	public SecurityManager getSecurityManager() {
		return this.securityMgr;
	}

	@Override
	public String getCurrentUser() {
		Subject current = SecurityUtils.getSubject();
		return current.getPrincipal().toString();
	}
	
	private class ShiroSecurityManager extends DefaultWebSecurityManager {
		public ShiroSecurityManager() {
			super();
			OpenIdConsumerRealm realm = new OpenIdConsumerRealm();
			CredentialsMatcher matcher = new BypassCredentialsMatcher();
			realm.setCredentialsMatcher(matcher);
			this.setRealm(realm);
		}
	}

}

@@ -84,16 +84,72 @@
 Utils;%0D%0A
+import org.apache.shiro.authc.AuthenticationException;%0D%0A
 import o
@@ -1433,22 +1433,34 @@
 t();%0D%0A%09%09
-return
+Object principal =
  current
@@ -1474,16 +1474,135 @@
 ncipal()
+;%0D%0A%09%09if (principal == null) %7B%0D%0A%09%09%09throw new AuthenticationException(%22Subject not signed in.%22);%0D%0A%09%09%7D%0D%0A%09%09return principal
 .toStrin

throw exception when user not login