|
| 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()) |
0 commit comments