<?php
class Auth{
	/**
		Accesskey가 유효한지 확인
	*/	
	function verify($accesskey){
		$infos =query('select username, pw from info');
		$result['status'] = 'Unauthorized';
		if(!empty($infos)){
			foreach($infos as $info){
				if($accesskey == sha1($info[username].'_'.$info[pw])){
					$result['status'] = 'OK';
				}
			}
		}
		return $result;
	}
	/**
		getAccesskey
		username sha1 암호화된 pw 를 이용하여 accesskey 생성 
		형식: sha1(username_pw) 
	*/	
	function getAccesskey($a){
		if(!$a[username])
			die('username 필수');
		if(!$a[pw])
			die('비밀번호 필수');
		$result['status'] = 'Unauthorized';
		$infos =query('select username, pw from info where username=:username and pw=:pw', $a);
		if($infos[0]){
			$result['status'] = 'OK';
			$result['accesskey'] = sha1($infos[0][username].'_'.$infos[0][pw]);
		}
		return $result;
	}
}

$auth = new Auth();

$app->action('auth/getAccesskey', function() use($auth){
	reply_json($auth->getAccesskey($_REQUEST));
});

$app->action('auth/verify', function() use($auth){
	reply_json($auth->verify($_REQUEST['accesskey']));
});

?>