Skip to content

Commit 221f4e4

Browse files
authored
Merge pull request #295 from moririnson/feature/access-token-v21
channel access token v21
2 parents 9369440 + 4d04ec1 commit 221f4e4

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

src/LINEBot.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,70 @@ public function revokeChannelAccessToken($channelAccessToken)
751751
);
752752
}
753753

754+
/**
755+
* Create channel access token v2.1
756+
*
757+
* You can issue up to 30 tokens.
758+
* If you reach the maximum limit, additional requests of issuing channel access tokens are blocked.
759+
*
760+
* @see https://developers.line.biz/en/docs/messaging-api/generate-json-web-token/#generate_jwt
761+
* @param string $jwt
762+
* @return Response
763+
*/
764+
public function createChannelAccessToken21($jwt)
765+
{
766+
$url = $this->endpointBase . '/oauth2/v2.1/token';
767+
return $this->httpClient->post(
768+
$url,
769+
[
770+
'grant_type' => 'client_credentials',
771+
'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
772+
'client_assertion' => $jwt,
773+
],
774+
['Content-Type: application/x-www-form-urlencoded']
775+
);
776+
}
777+
778+
/**
779+
* Revoke channel access token v2.1
780+
*
781+
* @param string $channelId
782+
* @param string $channelSecret
783+
* @param string $channelAccessToken
784+
* @return Response
785+
*/
786+
public function revokeChannelAccessToken21($channelId, $channelSecret, $channelAccessToken)
787+
{
788+
$url = $this->endpointBase . '/oauth2/v2.1/revoke';
789+
return $this->httpClient->post(
790+
$url,
791+
[
792+
'client_id' => $channelId,
793+
'client_secret' => $channelSecret,
794+
'access_token' => $channelAccessToken,
795+
],
796+
['Content-Type: application/x-www-form-urlencoded']
797+
);
798+
}
799+
800+
/**
801+
* Get all valid channel access token key IDs v2.1
802+
*
803+
* @param string $jwt
804+
* @return Response
805+
*/
806+
public function getChannelAccessToken21Keys($jwt)
807+
{
808+
$url = $this->endpointBase . '/oauth2/v2.1/tokens/kid';
809+
return $this->httpClient->get(
810+
$url,
811+
[
812+
'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
813+
'client_assertion' => $jwt,
814+
]
815+
);
816+
}
817+
754818
/**
755819
* Send Narrowcast message.
756820
*

tests/LINEBot/OauthTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,102 @@ public function testRevokeChannelAccessToken()
7575
$this->assertTrue($res->isSucceeded());
7676
$this->assertEquals([], $res->getJSONDecodedBody());
7777
}
78+
79+
public function testCreateChannelAccessToken21()
80+
{
81+
$mock = function ($testRunner, $httpMethod, $url, $data, $header) {
82+
/** @var \PHPUnit\Framework\TestCase $testRunner */
83+
$testRunner->assertEquals('POST', $httpMethod);
84+
$testRunner->assertEquals('https://api.line.me/oauth2/v2.1/token', $url);
85+
$testRunner->assertEquals([
86+
'grant_type' => 'client_credentials',
87+
'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
88+
'client_assertion' => 'JWT',
89+
], $data);
90+
$testRunner->assertEquals(['Content-Type: application/x-www-form-urlencoded'], $header);
91+
92+
return [
93+
'access_token' => 'W1TeHCgfH2Liwa.....',
94+
'expires_in' => 2592000,
95+
'token_type' => 'Bearer',
96+
'key_id' => 'sDTOzw5wIfxxxxPEzcmeQA',
97+
];
98+
};
99+
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
100+
101+
$res = $bot->createChannelAccessToken21('JWT');
102+
103+
$this->assertEquals(200, $res->getHTTPStatus());
104+
$this->assertTrue($res->isSucceeded());
105+
106+
$data = $res->getJSONDecodedBody();
107+
$this->assertEquals('W1TeHCgfH2Liwa.....', $data['access_token']);
108+
$this->assertEquals(2592000, $data['expires_in']);
109+
$this->assertEquals('Bearer', $data['token_type']);
110+
$this->assertEquals('sDTOzw5wIfxxxxPEzcmeQA', $data['key_id']);
111+
}
112+
113+
public function testRevokeChannelAccessToken21()
114+
{
115+
$mock = function ($testRunner, $httpMethod, $url, $data, $header) {
116+
/** @var \PHPUnit\Framework\TestCase $testRunner */
117+
$testRunner->assertEquals('POST', $httpMethod);
118+
$testRunner->assertEquals('https://api.line.me/oauth2/v2.1/revoke', $url);
119+
$testRunner->assertEquals([
120+
'client_id' => 'CHANNEL-ID',
121+
'client_secret' => 'CHANNEL-SECRET',
122+
'access_token' => 'CHANNEL-ACCESS-TOKEN'
123+
], $data);
124+
$testRunner->assertEquals(['Content-Type: application/x-www-form-urlencoded'], $header);
125+
126+
return [];
127+
};
128+
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
129+
130+
$res = $bot->revokeChannelAccessToken21('CHANNEL-ID', 'CHANNEL-SECRET', 'CHANNEL-ACCESS-TOKEN');
131+
132+
$this->assertEquals(200, $res->getHTTPStatus());
133+
$this->assertTrue($res->isSucceeded());
134+
$this->assertEquals([], $res->getJSONDecodedBody());
135+
}
136+
137+
public function testGetChannelAccessToken21Keys1()
138+
{
139+
$mock = function ($testRunner, $httpMethod, $url, $data) {
140+
/** @var \PHPUnit\Framework\TestCase $testRunner */
141+
$testRunner->assertEquals('GET', $httpMethod);
142+
$testRunner->assertEquals('https://api.line.me/oauth2/v2.1/tokens/kid', $url);
143+
$testRunner->assertEquals([
144+
'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
145+
'client_assertion' => 'JWT',
146+
], $data);
147+
148+
return [
149+
'key_ids' => [
150+
'U_gdnFYKTWRxxxxDVZexGg',
151+
'sDTOzw5wIfWxxxxzcmeQA',
152+
'73hDyp3PxGfxxxxD6U5qYA',
153+
'FHGanaP79smDxxxxyPrVw',
154+
'CguB-0kxxxxdSM3A5Q_UtQ',
155+
'G82YP96jhHwyKSxxxx7IFA',
156+
]
157+
];
158+
};
159+
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
160+
161+
$res = $bot->getChannelAccessToken21Keys('JWT');
162+
163+
$this->assertEquals(200, $res->getHTTPStatus());
164+
$this->assertTrue($res->isSucceeded());
165+
166+
$data = $res->getJSONDecodedBody();
167+
$this->assertEquals([
168+
'U_gdnFYKTWRxxxxDVZexGg',
169+
'sDTOzw5wIfWxxxxzcmeQA',
170+
'73hDyp3PxGfxxxxD6U5qYA',
171+
'FHGanaP79smDxxxxyPrVw',
172+
'CguB-0kxxxxdSM3A5Q_UtQ',
173+
'G82YP96jhHwyKSxxxx7IFA',
174+
], $data['key_ids']);
175+
}
78176
}

0 commit comments

Comments
 (0)