|
| 1 | +from pubnub import utils |
| 2 | +from pubnub.endpoints.endpoint import Endpoint |
| 3 | +from pubnub.errors import PNERR_RESOURCES_MISSING, PNERR_TTL_MISSING, PNERR_INVALID_META |
| 4 | +from pubnub.exceptions import PubNubException |
| 5 | +from pubnub.enums import HttpMethod, PNOperationType |
| 6 | +from pubnub.models.consumer.v3.access_manager import PNGrantTokenResult |
| 7 | + |
| 8 | + |
| 9 | +class GrantToken(Endpoint): |
| 10 | + GRANT_TOKEN_PATH = "/v3/pam/%s/grant" |
| 11 | + |
| 12 | + READ = 1 |
| 13 | + WRITE = 2 |
| 14 | + MANAGE = 4 |
| 15 | + DELETE = 8 |
| 16 | + CREATE = 16 |
| 17 | + |
| 18 | + def __init__(self, pubnub): |
| 19 | + Endpoint.__init__(self, pubnub) |
| 20 | + self._ttl = None |
| 21 | + self._meta = None |
| 22 | + self._channelList = [] |
| 23 | + self._groupList = [] |
| 24 | + self._userList = [] |
| 25 | + self._spaceList = [] |
| 26 | + |
| 27 | + self._sort_params = True |
| 28 | + |
| 29 | + def ttl(self, ttl): |
| 30 | + self._ttl = ttl |
| 31 | + return self |
| 32 | + |
| 33 | + def meta(self, meta): |
| 34 | + self._meta = meta |
| 35 | + return self |
| 36 | + |
| 37 | + def users(self, users): |
| 38 | + self._userList = users |
| 39 | + return self |
| 40 | + |
| 41 | + def spaces(self, spaces): |
| 42 | + self._spaceList = spaces |
| 43 | + return self |
| 44 | + |
| 45 | + def custom_params(self): |
| 46 | + return {} |
| 47 | + |
| 48 | + def build_data(self): |
| 49 | + params = {'ttl': str(int(self._ttl))} |
| 50 | + |
| 51 | + permissions = {} |
| 52 | + resources = {} |
| 53 | + patterns = {} |
| 54 | + |
| 55 | + utils.parse_resources(self._channelList, "channels", resources, patterns) |
| 56 | + utils.parse_resources(self._groupList, "groups", resources, patterns) |
| 57 | + utils.parse_resources(self._userList, "users", resources, patterns) |
| 58 | + utils.parse_resources(self._spaceList, "spaces", resources, patterns) |
| 59 | + |
| 60 | + permissions['resources'] = resources |
| 61 | + permissions['patterns'] = patterns |
| 62 | + |
| 63 | + if self._meta is not None: |
| 64 | + if isinstance(self._meta, dict): |
| 65 | + permissions['meta'] = self._meta |
| 66 | + else: |
| 67 | + raise PubNubException(pn_error=PNERR_INVALID_META) |
| 68 | + else: |
| 69 | + permissions['meta'] = {} |
| 70 | + |
| 71 | + params['permissions'] = permissions |
| 72 | + |
| 73 | + return utils.write_value_as_string(params) |
| 74 | + |
| 75 | + def build_path(self): |
| 76 | + return GrantToken.GRANT_TOKEN_PATH % self.pubnub.config.subscribe_key |
| 77 | + |
| 78 | + def http_method(self): |
| 79 | + return HttpMethod.POST |
| 80 | + |
| 81 | + def validate_params(self): |
| 82 | + self.validate_subscribe_key() |
| 83 | + self.validate_secret_key() |
| 84 | + self.validate_ttl() |
| 85 | + self.validate_resources() |
| 86 | + |
| 87 | + def create_response(self, envelope): |
| 88 | + return PNGrantTokenResult.from_json(envelope['data']) |
| 89 | + |
| 90 | + def is_auth_required(self): |
| 91 | + return False |
| 92 | + |
| 93 | + def affected_channels(self): |
| 94 | + # generate a list of channels when they become supported in PAMv3 |
| 95 | + return None |
| 96 | + |
| 97 | + def affected_channels_groups(self): |
| 98 | + # generate a list of groups when they become supported in PAMv3 |
| 99 | + return None |
| 100 | + |
| 101 | + def request_timeout(self): |
| 102 | + return self.pubnub.config.non_subscribe_request_timeout |
| 103 | + |
| 104 | + def connect_timeout(self): |
| 105 | + return self.pubnub.config.connect_timeout |
| 106 | + |
| 107 | + def operation_type(self): |
| 108 | + return PNOperationType.PNAccessManagerGrantToken |
| 109 | + |
| 110 | + def name(self): |
| 111 | + return "Grant Token" |
| 112 | + |
| 113 | + def validate_resources(self): |
| 114 | + if (self._userList is None or len(self._userList) == 0) and \ |
| 115 | + (self._spaceList is None or len(self._spaceList) == 0): |
| 116 | + raise PubNubException(pn_error=PNERR_RESOURCES_MISSING) |
| 117 | + |
| 118 | + def validate_ttl(self): |
| 119 | + if self._ttl is None: |
| 120 | + raise PubNubException(pn_error=PNERR_TTL_MISSING) |
0 commit comments