mirror of
http://124.126.16.154:8888/singularity/hyperf-saml.git
synced 2026-01-15 07:35:07 +08:00
feat(sp.isLogin): 实现了获取单点登录状态的服务
This commit is contained in:
@@ -47,7 +47,7 @@ class Base extends AbstractService
|
||||
/**
|
||||
* 创建SamlRequest
|
||||
*
|
||||
* @param string $ascUrl
|
||||
* @param string $acsUrl
|
||||
* @param string $idpID
|
||||
* @param string $issuer
|
||||
* @param string $relayState
|
||||
@@ -58,7 +58,7 @@ class Base extends AbstractService
|
||||
*/
|
||||
public function createSamlRequest(
|
||||
string $idpID,
|
||||
string $ascUrl,
|
||||
string $acsUrl,
|
||||
string $issuer,
|
||||
string $relayState = '',
|
||||
bool $returnUrl = true,
|
||||
@@ -70,7 +70,7 @@ class Base extends AbstractService
|
||||
|
||||
$authnRequest = new AuthnRequest();
|
||||
$authnRequest
|
||||
->setAssertionConsumerServiceURL($ascUrl)
|
||||
->setAssertionConsumerServiceURL($acsUrl)
|
||||
->setID(Helper::generateID())
|
||||
->setIssueInstant(new DateTime())
|
||||
->setDestination($idpID)
|
||||
|
||||
@@ -9,9 +9,9 @@
|
||||
|
||||
namespace Singularity\HyperfSaml\Services\Sp;
|
||||
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Contract\ResponseInterface;
|
||||
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
|
||||
use Singularity\HyperfSaml\Services\Base;
|
||||
use Teapot\StatusCode\RFC\RFC7231;
|
||||
|
||||
@@ -25,44 +25,56 @@ use Teapot\StatusCode\RFC\RFC7231;
|
||||
*/
|
||||
class Sso
|
||||
{
|
||||
/**
|
||||
* @Inject()
|
||||
* @var \Singularity\HyperfSaml\Services\Base
|
||||
*/
|
||||
private Base $base;
|
||||
private string $idpId;
|
||||
private string $idpAssertionUrl;
|
||||
private string $acsUrl;
|
||||
private string $issuer;
|
||||
private string $relayState;
|
||||
|
||||
public function __construct(
|
||||
private Base $base,
|
||||
private RequestInterface $request,
|
||||
private ResponseInterface $response,
|
||||
) {
|
||||
$this->relayState = $this->request->query('RelayState', '');
|
||||
|
||||
$this->idpId = config('saml.server.idp_id');
|
||||
$this->idpAssertionUrl = config('saml.server.idp_assertion_url');
|
||||
|
||||
$this->issuer = config('saml.client.entity_id');
|
||||
$this->acsUrl = config('saml.client.acs_url');
|
||||
}
|
||||
|
||||
/**
|
||||
* @Inject
|
||||
* @var \Hyperf\HttpServer\Contract\RequestInterface
|
||||
* 自动重定向获取用户登录状态
|
||||
*
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
private RequestInterface $request;
|
||||
|
||||
/**
|
||||
* @Inject
|
||||
* @var \Hyperf\HttpServer\Contract\ResponseInterface
|
||||
*/
|
||||
private ResponseInterface $response;
|
||||
public function redirectSsoStatus(): PsrResponseInterface
|
||||
{
|
||||
$url = $this->base->createSamlRequest(
|
||||
idpId: $this->idpAssertionUrl,
|
||||
acsUrl: $this->acsUrl,
|
||||
issuer: $this->issuer,
|
||||
relayState: $this->relayState,
|
||||
);
|
||||
return $this->response->redirect($url, RFC7231::FOUND);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动重定向到单点登录
|
||||
*
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
public function redirect(): \Psr\Http\Message\ResponseInterface
|
||||
public function redirectSso(): PsrResponseInterface
|
||||
{
|
||||
$idpId = config('saml.server.idp_id');
|
||||
|
||||
$acsUrl = config('saml.client.acs_url');
|
||||
$issuer = config('saml.client.entity_id');
|
||||
|
||||
$relayState = $this->request->query('RelayState', '');
|
||||
$language = $this->request->query('language') ?? config('language') ?? 'zh_CN';
|
||||
|
||||
$url = $this->base->createSamlRequest(
|
||||
idpID: $idpId,
|
||||
ascUrl: $acsUrl,
|
||||
issuer: $issuer,
|
||||
relayState: $relayState,
|
||||
idpID: $this->idpId,
|
||||
acsUrl: $this->acsUrl,
|
||||
issuer: $this->issuer,
|
||||
relayState: $this->relayState,
|
||||
exactArguments: [
|
||||
'language' => $language,
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user