Skip to content

Commit

Permalink
feat: load default agent_id, resolved #2619
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Nov 16, 2022
1 parent 8c6da8c commit 3b7707d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Work/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,22 @@ public function createClient(): AccessTokenAwareClient

public function getOAuth(): SocialiteProviderInterface
{
return (new WeWork(
$provider = new WeWork(
[
'client_id' => $this->getAccount()->getCorpId(),
'client_secret' => $this->getAccount()->getSecret(),
'redirect_url' => $this->config->get('oauth.redirect_url'),
]
))->withApiAccessToken($this->getAccessToken()->getToken())
->scopes((array) $this->config->get('oauth.scopes', ['snsapi_base']));
);

$provider->withApiAccessToken($this->getAccessToken()->getToken());
$provider->scopes((array) $this->config->get('oauth.scopes', ['snsapi_base']));

if ($this->config->has('agent_id') && \is_numeric($this->config->get('agent_id'))) {
$provider->withAgentId((int) $this->config->get('agent_id'));
}

return $provider;
}

public function getTicket(): JsApiTicket
Expand Down
34 changes: 34 additions & 0 deletions tests/Work/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use EasyWeChat\Work\JsApiTicket;
use EasyWeChat\Work\Server;
use EasyWeChat\Work\Utils;
use Overtrue\Socialite\Providers\WeWork;
use Psr\Http\Message\ServerRequestInterface;
use Psr\SimpleCache\CacheInterface;
use Symfony\Component\Cache\Psr16Cache;
Expand Down Expand Up @@ -251,4 +252,37 @@ public function test_get_utils()

$this->assertInstanceOf(Utils::class, $app->getUtils());
}

public function test_get_oauth()
{
$app = new Application(
[
'corp_id' => 'wx3cf0f39249000060',
'secret' => 'mock-secret',
'token' => 'mock-token',
'aes_key' => 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG',
]
);

$oauth = $app->getOauth();
$this->assertInstanceOf(WeWork::class, $oauth);
$ref = new \ReflectionProperty($oauth, 'agentId');
$this->assertNull($ref->getValue($oauth));

// with default agent id
$app = new Application(
[
'corp_id' => 'wx3cf0f39249000060',
'secret' => 'mock-secret',
'token' => 'mock-token',
'aes_key' => 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG',
'agent_id' => '100001',
]
);

$oauth = $app->getOauth();
$this->assertInstanceOf(WeWork::class, $oauth);
$ref = new \ReflectionProperty($oauth, 'agentId');
$this->assertSame(100001, $ref->getValue($oauth));
}
}

0 comments on commit 3b7707d

Please sign in to comment.