Skip to content

Commit c9876c5

Browse files
yingtungchloechen
andauthored
Support "Channel access token v2.1" (#372)
* Support "Channel access token v2.1" * add validate channel access token v2.1 and get valid channel token key ids apis * add unit test Co-authored-by: chloechen <chloechen@mobagel.com>
1 parent 117e897 commit c9876c5

File tree

3 files changed

+395
-3
lines changed

3 files changed

+395
-3
lines changed

linebot/api.py

Lines changed: 126 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
"""linebot.api module."""
1616

17-
1817
import json
1918

2019
from .__about__ import __version__
@@ -31,7 +30,10 @@
3130
AudienceGroup, ClickAudienceGroup, ImpAudienceGroup, GetAuthorityLevel, Audience,
3231
CreateAudienceGroup
3332
)
34-
from .models.responses import Group, UserIds, RichMenuAliasResponse, RichMenuAliasListResponse
33+
from .models.responses import (
34+
Group, UserIds, RichMenuAliasResponse, RichMenuAliasListResponse, ChannelAccessTokens,
35+
IssueChannelTokenResponseV2, VerifyChannelTokenResponseV2, ValidAccessTokenKeyIDsResponse
36+
)
3537

3638

3739
class LineBotApi(object):
@@ -1594,6 +1596,128 @@ def get_followers_ids(self, limit=300, start=None, timeout=None):
15941596

15951597
return UserIds.new_from_json_dict(response.json)
15961598

1599+
def issue_channel_access_token_v2_1(
1600+
self, client_assertion, grant_type='client_credentials',
1601+
client_assertion_type='urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
1602+
timeout=None):
1603+
"""Issues a channel access token v2.1.
1604+
1605+
https://developers.line.biz/en/reference/messaging-api/#issue-channel-access-token-v2-1
1606+
1607+
:param str client_assertion: Client assertion.
1608+
:param str grant_type: `client_credentials`
1609+
:param str client_assertion_type: `urn:ietf:params:oauth:client-assertion-type:jwt-bearer`.
1610+
:param timeout: (optional) How long to wait for the server
1611+
to send data before giving up, as a float,
1612+
or a (connect timeout, read timeout) float tuple.
1613+
Default is self.http_client.timeout
1614+
:type timeout: float | tuple(float, float)
1615+
:rtype: :py:class:`linebot.models.responses.IssueChannelTokenResponseV2`
1616+
"""
1617+
response = self._post(
1618+
'/oauth2/v2.1/token',
1619+
data={
1620+
'grant_type': grant_type,
1621+
'client_assertion_type': client_assertion_type,
1622+
'client_assertion': client_assertion,
1623+
},
1624+
headers={'Content-Type': 'application/x-www-form-urlencoded'},
1625+
timeout=timeout
1626+
)
1627+
1628+
return IssueChannelTokenResponseV2.new_from_json_dict(response.json)
1629+
1630+
def revoke_channel_access_token_v2_1(
1631+
self, client_id,
1632+
client_secret, access_token,
1633+
timeout=None):
1634+
"""Revokes a channel access token v2.1.
1635+
1636+
https://developers.line.biz/en/reference/messaging-api/#revoke-channel-access-token-v2-1
1637+
1638+
:param str client_id: Client id.
1639+
:param str client_secret: Channel secret.
1640+
:param str access_token: Channel access token.
1641+
:param timeout: (optional) How long to wait for the server
1642+
to send data before giving up, as a float,
1643+
or a (connect timeout, read timeout) float tuple.
1644+
Default is self.http_client.timeout
1645+
:type timeout: float | tuple(float, float)
1646+
"""
1647+
self._post(
1648+
'/oauth2/v2.1/revoke',
1649+
data={'client_id': client_id,
1650+
'client_secret': client_secret,
1651+
'access_token': access_token},
1652+
timeout=timeout
1653+
)
1654+
1655+
def get_channel_access_tokens_v2_1(
1656+
self, client_assertion,
1657+
client_assertion_type='urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
1658+
timeout=None):
1659+
"""Get issued channel access tokens v2.1.
1660+
1661+
https://developers.line.biz/en/reference/messaging-api/#get-issued-channel-access-tokens-v2-1
1662+
1663+
:param str client_assertion: Client assertion.
1664+
:param str client_assertion_type: `urn:ietf:params:oauth:client-assertion-type:jwt-bearer`.
1665+
:param timeout: (optional) How long to wait for the server
1666+
to send data before giving up, as a float,
1667+
or a (connect timeout, read timeout) float tuple.
1668+
Default is self.http_client.timeout
1669+
:type timeout: float | tuple(float, float)
1670+
:rtype: :py:class:`linebot.models.responses.ChannelAccessTokens`
1671+
"""
1672+
response = self._get(
1673+
'/oauth2/v2.1/tokens',
1674+
params={'client_assertion': client_assertion,
1675+
'client_assertion_type': client_assertion_type},
1676+
timeout=timeout
1677+
)
1678+
return ChannelAccessTokens.new_from_json_dict(response.json)
1679+
1680+
def verify_channel_access_token_v2_1(self, access_token, timeout=None):
1681+
"""Validate channel access token v2.1.
1682+
1683+
https://developers.line.biz/en/reference/messaging-api/#verfiy-channel-access-token-v2-1
1684+
1685+
:param str access_token: Channel access token.
1686+
:param timeout: (optional) How long to wait for the server
1687+
to send data before giving up, as a float,
1688+
or a (connect timeout, read timeout) float tuple.
1689+
Default is self.http_client.timeout
1690+
:type timeout: float | tuple(float, float)
1691+
:rtype: :py:class:`linebot.models.responses.VerifyChannelTokenResponseV2`
1692+
"""
1693+
response = self._get('/oauth2/v2.1/verify',
1694+
params={'access_token': access_token},
1695+
timeout=timeout)
1696+
return VerifyChannelTokenResponseV2.new_from_json_dict(response.json)
1697+
1698+
def get_channel_token_key_ids_v2_1(
1699+
self, client_assertion,
1700+
client_assertion_type='urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
1701+
timeout=None):
1702+
"""Get all valid channel access token key IDs v2.1.
1703+
1704+
https://developers.line.biz/en/reference/messaging-api/#get-all-valid-channel-access-token-key-ids-v2-1
1705+
1706+
:param str client_assertion: Client assertion.
1707+
:param str client_assertion_type: `urn:ietf:params:oauth:client-assertion-type:jwt-bearer`.
1708+
:param timeout: (optional) How long to wait for the server
1709+
to send data before giving up, as a float,
1710+
or a (connect timeout, read timeout) float tuple.
1711+
Default is self.http_client.timeout
1712+
:type timeout: float | tuple(float, float)
1713+
:rtype: :py:class:`linebot.models.responses.VerifyChannelTokenResponseV2`
1714+
"""
1715+
response = self._get('/oauth2/v2.1/tokens/kid',
1716+
params={"client_assertion": client_assertion,
1717+
"client_assertion_type": client_assertion_type},
1718+
timeout=timeout)
1719+
return ValidAccessTokenKeyIDsResponse.new_from_json_dict(response.json)
1720+
15971721
def _get(self, path, endpoint=None, params=None, headers=None, stream=False, timeout=None):
15981722
url = (endpoint or self.endpoint) + path
15991723

linebot/models/responses.py

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
"""linebot.models.responses module."""
1616

17-
1817
from .base import Base
1918
from .insight import (
2019
SubscriptionPeriodInsight, AppTypeInsight, AgeInsight,
@@ -843,3 +842,88 @@ def __init__(self, user_ids=None, next=None, **kwargs):
843842

844843
self.user_ids = user_ids
845844
self.next = next
845+
846+
847+
class IssueChannelTokenResponseV2(Base):
848+
"""IssueAccessTokenResponseV2.
849+
850+
https://developers.line.biz/en/reference/messaging-api/#issue-channel-access-token-v2-1
851+
"""
852+
853+
def __init__(self, access_token=None, expires_in=None, token_type=None, key_id=None, **kwargs):
854+
"""__init__ method.
855+
856+
:param str access_token: Short-lived channel access token.
857+
:param int expires_in: Time until channel access token expires in seconds
858+
from time the token is issued.
859+
:param str token_type: Bearer.
860+
:param key_id: Unique key ID for identifying the channel access token.
861+
:param kwargs:
862+
"""
863+
super(IssueChannelTokenResponseV2, self).__init__(**kwargs)
864+
865+
self.access_token = access_token
866+
self.expires_in = expires_in
867+
self.token_type = token_type
868+
self.key_id = key_id
869+
870+
871+
class ChannelAccessTokens(Base):
872+
"""ChannelAccessTokens.
873+
874+
https://developers.line.biz/en/reference/messaging-api/#get-issued-channel-access-tokens-v2-1
875+
"""
876+
877+
def __init__(self, access_tokens=None, **kwargs):
878+
"""__init__ method.
879+
880+
:param access_tokens: List of channel access token
881+
:type access_tokens: list[str]
882+
:param kwargs:
883+
884+
"""
885+
super(ChannelAccessTokens, self).__init__(**kwargs)
886+
887+
self.access_tokens = access_tokens
888+
889+
890+
class VerifyChannelTokenResponseV2(Base):
891+
"""VerifyChannelTokenResponseV2.
892+
893+
https://developers.line.biz/en/reference/messaging-api/#verfiy-channel-access-token-v2-1
894+
895+
"""
896+
897+
def __init__(self, client_id=None, expires_in=None, scope=None, **kwargs):
898+
"""__init__ method.
899+
900+
:param str client_id: The channel ID for which the channel access token was issued.
901+
:param int expires_in: Number of seconds before the channel access token expires.
902+
:param str scope: Permissions granted to the channel access token.
903+
:param kwargs:
904+
905+
"""
906+
super(VerifyChannelTokenResponseV2, self).__init__(**kwargs)
907+
908+
self.client_id = client_id
909+
self.expires_in = expires_in
910+
self.scope = scope
911+
912+
913+
class ValidAccessTokenKeyIDsResponse(Base):
914+
"""ValidAccessTokenKeyIDsResponse.
915+
916+
https://developers.line.biz/en/reference/messaging-api/#get-all-valid-channel-access-token-key-ids-v2-1
917+
918+
"""
919+
920+
def __init__(self, kids=None, **kwargs):
921+
"""__init__ method.
922+
923+
:param kids: Array of channel access token key IDs.
924+
:type kids: list[str]
925+
:param kwargs:
926+
"""
927+
super(ValidAccessTokenKeyIDsResponse, self).__init__(**kwargs)
928+
929+
self.kids = kids

0 commit comments

Comments
 (0)