ssa/secure

此包的最新版本(dev-master)没有可用的许可证信息。

Ssa secure 模块用于 ssa 框架。

dev-master 2015-03-22 18:27 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:49:41 UTC


README

Ssa secure 是 SSA[https://github.com/deblockt/ssa] 的扩展。

提供管理登录服务。

安装

您需要在您的 composer 依赖中添加

	"ssa/secure": "dev-master"

配置

要使用此模块,您需要在您的 serviceManager 中添加服务。

config.php

ServiceManager::getInstance()->registerAllServices(array(
    'authenticateService' => array('class' => 'ssa\secure\services\AuthenticateService')
));

在这个示例中,服务名称是 authenticateService,但您可以选择其他服务名称。

之后,您必须配置模块

  • 添加安全提供者:用于登录用户的类
  • 更改 tokenCryptKey:这是用于加密用户 ID 的密钥。更改默认令牌密钥以提高安全性。

config.php

use ssa\secure\SecureConfiguration;

// SecurityProvider is your own class who implement ISecurityProvider
SecureConfiguration::getInstance()->setSecurityProvider(new SecurityProvider());
SecureConfiguration::getInstance()->setTokenCryptKey('yourCryptKey');

SecurityProvider.php

<?php

use ssa\secure\ISecurityProvider;
use ssa\secure\SecureConfiguration;

/**
 * class used for login users
 */
class SecurityProvider implements ISecurityProvider {

	/**
	 * method used to authenticate user
	 *
	 * @param string $login the connection login
	 * @param string $mdp the connection password
	 *
	 * @return the userd id. It is is used for generate unique token for this user. null is user not exists.
	 * 		   or array if you need get specifique data on javascript, array need have a id key (it'is a unique if for identifiate user)
	 */
	public function authenticate($login, $password) {
		// here just check the user passord
		// surely that you need to do an database access
		if ($password == 'admin') {
			return array('id' => $login, 'name' => $login);
		}
		return null; // user is not recognized
	}
	
}

用法

JavaScript

您需要在您的页面上添加认证服务

<!-- Your service url, may be different -->
<script type="text/javascript" src="javascript.php?service=authenticateService"></script>

要登录用户,只需执行

authenticateService.login('username', 'password');

要登出用户,只需执行

authenticateService.logout();

要获取您的认证信息(如果您的认证方法返回一个数组)

authenticateService.getUserInfos()();

JavaScript 安全监听器

有三个监听器可用

  • DisconnectListener:当服务返回错误时调用的监听器
  • ConnectedListener:登录成功时调用的监听器
  • BadUserOrPasswordListener:登录失败时调用的监听器。错误用户名或密码
authenticateService.addDisconnectListener(function(){
	$('span.result').html('You are not logged');
});

// token is the crypted user id
// userInfos is array return by the authenticate method. The is index is not available on client side.
authenticateService.addConnectedListener(function(token, userInfos){
	$('span.result').html('You are logged, you can call service.');
});

authenticateService.addBadUserOrPasswordListener(function(){
	$('span.result').html('Bad loggin or password');
});

PHP

如果您想安全化服务(用户需要登录才能调用服务),只需在您的服务方法上添加 @Secure 注解。

use ssa\secure\annotations\Secure;

/**
 * @author thomas
 */
class HelloWorld {
    
    /**
	 * @Secure
	 *
     * @param string $userId the userId is automatically add by secure module. It's the id returned by authenticate method 
     * @return string 
     */
    public function helloYou($userId) {
        return 'hello ' .$userId.'!!!';
    }
}

所有安全服务都可能需要 userId 参数,这是安全模块自动添加的。**警告**:此参数必须是最后一个参数,或者下一个参数必须具有默认值。