Skip to content

Commit f71cafa

Browse files
committed
Added ability to retrieve API tokens for guest issuers
This commit adds the ability to obtain the api access tokens for guest issuers (see https://developer.webex.com/docs/guest-issuer for details)
1 parent 719b586 commit f71cafa

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
'future',
5151
'requests>=2.4.2',
5252
'requests-toolbelt',
53+
'PyJWT'
5354
]
5455

5556

webexteamssdk/api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from .team_memberships import TeamMembershipsAPI
4646
from .teams import TeamsAPI
4747
from .webhooks import WebhooksAPI
48+
from .guest_issuer import GuestIssuerAPI
4849

4950

5051
class WebexTeamsAPI(object):
@@ -143,6 +144,7 @@ def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL,
143144
single_request_timeout=single_request_timeout
144145
)
145146
self.events = EventsAPI(self._session, object_factory)
147+
self.guest_issuer = GuestIssuerAPI(self._session, object_factory)
146148

147149
@property
148150
def access_token(self):

webexteamssdk/api/guest_issuer.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# -*- coding: utf-8 -*-
2+
"""Webex Teams Guest Issuer API wrapper.
3+
4+
Copyright (c) 2016-2018 Cisco and/or its affiliates.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
"""
24+
25+
from __future__ import (
26+
absolute_import,
27+
division,
28+
print_function,
29+
unicode_literals,
30+
)
31+
32+
from builtins import *
33+
34+
from past.builtins import basestring
35+
36+
from ..generator_containers import generator_container
37+
from ..restsession import RestSession
38+
from ..utils import (
39+
check_type,
40+
dict_from_items_with_values,
41+
check_response_code
42+
)
43+
from ..response_codes import EXPECTED_RESPONSE_CODE
44+
45+
46+
import jwt
47+
import base64
48+
import requests
49+
50+
API_ENDPOINT = 'jwt'
51+
OBJECT_TYPE = 'guest_issuer_token'
52+
53+
class GuestIssuerAPI(object):
54+
"""Webex Teams Guest Issuer API.
55+
56+
Wraps the Webex Teams Guest Issuer API and exposes the API as native
57+
methods that return native Python objects.
58+
59+
"""
60+
61+
def __init__(self, session, object_factory):
62+
"""Initialize a new GuestIssuerAPI object with the provided RestSession
63+
64+
Args:
65+
session(RestSession): The RESTful session object to be used for
66+
API calls to the Webex Teams service
67+
68+
Raises:
69+
TypeError: If the parameter types are incorrect
70+
"""
71+
check_type(session, RestSession)
72+
73+
super(GuestIssuerAPI, self).__init__()
74+
75+
self._session = session
76+
self._object_factory = object_factory
77+
78+
def create(self, subject, displayName, issuerToken, expiration, secret):
79+
"""Create a new guest issuer using the provided issuer token.
80+
81+
This function returns a guest issuer with an api access token.
82+
83+
Args:
84+
subject(basestring): Unique and public identifier
85+
displayName(basestring): Display Name of the guest user
86+
issuerToken(basestring): Issuer token from developer hub
87+
expiration(basestring): Expiration time as a unix timestamp
88+
secret(basestring): The secret used to sign your guest issuers
89+
90+
Returns:
91+
GuestIssuerToken: A Guest Issuer with a valid access token.
92+
93+
Raises:
94+
TypeError: If the parameter types are incorrect
95+
ApiError: If the webex teams cloud returns an error.
96+
"""
97+
#ToDo(mneiding): Check types
98+
99+
payload = {
100+
"sub": subject,
101+
"name": displayName,
102+
"iss": issuerToken,
103+
"exp": expiration
104+
}
105+
106+
key = base64.b64decode(secret)
107+
jwt_token = jwt.encode(payload, key, algorithm='HS256')
108+
109+
url = self._session.base_url + API_ENDPOINT + "/" + "login"
110+
headers = {
111+
'Authorization': "Bearer " + jwt_token.decode('utf-8')
112+
}
113+
response = requests.post(url, headers=headers)
114+
check_response_code(response, EXPECTED_RESPONSE_CODE['GET'])
115+
116+
return self._object_factory(OBJECT_TYPE, response.json())

webexteamssdk/models/immutable.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
from .mixins.team_membership import TeamMembershipBasicPropertiesMixin
5555
from .mixins.webhook import WebhookBasicPropertiesMixin
5656
from .mixins.webhook_event import WebhookEventBasicPropertiesMixin
57+
from .mixins.guest_issuer_token import GuestIssuerTokenBasicPropertiesMixin
5758

5859

5960
class ImmutableData(object):
@@ -228,6 +229,9 @@ def data(self):
228229
"""The event resource data."""
229230
return ImmutableData(self._json_data.get('data'))
230231

232+
class GuestIssuerToken(ImmutableData, GuestIssuerTokenBasicPropertiesMixin):
233+
"""Webex Teams Guest Issuer Token data model"""
234+
231235

232236
immutable_data_models = defaultdict(
233237
lambda: ImmutableData,
@@ -244,6 +248,7 @@ def data(self):
244248
team_membership=TeamMembership,
245249
webhook=Webhook,
246250
webhook_event=WebhookEvent,
251+
guest_issuer_token=GuestIssuerToken,
247252
)
248253

249254

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: utf-8 -*-
2+
"""Webex Teams Guest-Issuer data model.
3+
4+
Copyright (c) 2016-2018 Cisco and/or its affiliates.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
"""
24+
25+
from __future__ import (
26+
absolute_import,
27+
division,
28+
print_function,
29+
unicode_literals,
30+
)
31+
32+
from builtins import *
33+
34+
class GuestIssuerTokenBasicPropertiesMixin(object):
35+
"""Guest issuer token basic properties"""
36+
37+
@property
38+
def access_token(self):
39+
return self._json_data.get('token')
40+
41+
@property
42+
def expires_in(self):
43+
return self._json_data.get('expiresIn')

0 commit comments

Comments
 (0)