first commit

This commit is contained in:
Mr.Qin
2022-08-19 19:48:37 +08:00
commit afdd648b65
3275 changed files with 631084 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\SuiteAuth;
use EasyWeChat\Kernel\AccessToken as BaseAccessToken;
/**
* AccessToken.
*
* @author xiaomin <keacefull@gmail.com>
*/
class AccessToken extends BaseAccessToken
{
/**
* @var string
*/
protected $requestMethod = 'POST';
/**
* @var string
*/
protected $endpointToGetToken = 'cgi-bin/service/get_suite_token';
/**
* @var string
*/
protected $tokenKey = 'suite_access_token';
/**
* @var string
*/
protected $cachePrefix = 'easywechat.kernel.suite_access_token.';
/**
* Credential for get token.
*/
protected function getCredentials(): array
{
return [
'suite_id' => $this->app['config']['suite_id'],
'suite_secret' => $this->app['config']['suite_secret'],
'suite_ticket' => $this->app['suite_ticket']->getTicket(),
];
}
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\SuiteAuth;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
/**
* ServiceProvider.
*
* @author xiaomin <keacefull@gmail.com>
*/
class ServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}.
*/
public function register(Container $app)
{
$app['suite_ticket'] = function ($app) {
return new SuiteTicket($app);
};
isset($app['suite_access_token']) || $app['suite_access_token'] = function ($app) {
return new AccessToken($app);
};
}
}

View File

@@ -0,0 +1,76 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\SuiteAuth;
use EasyWeChat\Kernel\Exceptions\RuntimeException;
use EasyWeChat\Kernel\Traits\InteractsWithCache;
use EasyWeChat\OpenWork\Application;
/**
* SuiteTicket.
*
* @author xiaomin <keacefull@gmail.com>
*/
class SuiteTicket
{
use InteractsWithCache;
/**
* @var Application
*/
protected $app;
/**
* SuiteTicket constructor.
*/
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* @return $this
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function setTicket(string $ticket)
{
$this->getCache()->set($this->getCacheKey(), $ticket, 1800);
if (!$this->getCache()->has($this->getCacheKey())) {
throw new RuntimeException('Failed to cache suite ticket.');
}
return $this;
}
/**
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function getTicket(): string
{
if ($cached = $this->getCache()->get($this->getCacheKey())) {
return $cached;
}
throw new RuntimeException('Credential "suite_ticket" does not exist in cache.');
}
protected function getCacheKey(): string
{
return 'easywechat.open_work.suite_ticket.'.$this->app['config']['suite_id'];
}
}