|
| 1 | +import time |
| 2 | +from twilio import jwt |
| 3 | + |
| 4 | + |
| 5 | +class IpMessagingGrant(object): |
| 6 | + """ Grant to access Twilio IP Messaging """ |
| 7 | + def __init__(self, service_sid=None, endpoint_id=None, |
| 8 | + deployment_role_sid=None, push_credential_sid=None): |
| 9 | + self.service_sid = service_sid |
| 10 | + self.endpoint_id = endpoint_id |
| 11 | + self.deployment_role_sid = deployment_role_sid |
| 12 | + self.push_credential_sid = push_credential_sid |
| 13 | + |
| 14 | + @property |
| 15 | + def key(self): |
| 16 | + return "ip_messaging" |
| 17 | + |
| 18 | + def to_payload(self): |
| 19 | + grant = {} |
| 20 | + if self.service_sid: |
| 21 | + grant['service_sid'] = self.service_sid |
| 22 | + if self.endpoint_id: |
| 23 | + grant['endpoint_id'] = self.endpoint_id |
| 24 | + if self.deployment_role_sid: |
| 25 | + grant['deployment_role_sid'] = self.deployment_role_sid |
| 26 | + if self.push_credential_sid: |
| 27 | + grant['push_credential_sid'] = self.push_credential_sid |
| 28 | + |
| 29 | + return grant |
| 30 | + |
| 31 | + |
| 32 | +class ConversationsGrant(object): |
| 33 | + """ Grant to access Twilio Conversations """ |
| 34 | + def __init__(self, configuration_profile_sid=None): |
| 35 | + self.configuration_profile_sid = configuration_profile_sid |
| 36 | + |
| 37 | + @property |
| 38 | + def key(self): |
| 39 | + return "rtc" |
| 40 | + |
| 41 | + def to_payload(self): |
| 42 | + grant = {} |
| 43 | + if self.configuration_profile_sid: |
| 44 | + grant['configuration_profile_sid'] = self.configuration_profile_sid |
| 45 | + |
| 46 | + return grant |
| 47 | + |
| 48 | + |
| 49 | +class AccessToken(object): |
| 50 | + """ Access Token used to access Twilio Resources """ |
| 51 | + def __init__(self, account_sid, signing_key_sid, secret, |
| 52 | + identity=None, ttl=3600, nbf=None): |
| 53 | + self.account_sid = account_sid |
| 54 | + self.signing_key_sid = signing_key_sid |
| 55 | + self.secret = secret |
| 56 | + |
| 57 | + self.identity = identity |
| 58 | + self.ttl = ttl |
| 59 | + self.nbf = nbf |
| 60 | + self.grants = [] |
| 61 | + |
| 62 | + def add_grant(self, grant): |
| 63 | + self.grants.append(grant) |
| 64 | + |
| 65 | + def to_jwt(self, algorithm='HS256'): |
| 66 | + now = int(time.time()) |
| 67 | + headers = { |
| 68 | + "typ": "JWT", |
| 69 | + "cty": "twilio-fpa;v=1" |
| 70 | + } |
| 71 | + |
| 72 | + grants = {} |
| 73 | + if self.identity: |
| 74 | + grants["identity"] = self.identity |
| 75 | + |
| 76 | + for grant in self.grants: |
| 77 | + grants[grant.key] = grant.to_payload() |
| 78 | + |
| 79 | + payload = { |
| 80 | + "jti": '{0}-{1}'.format(self.signing_key_sid, now), |
| 81 | + "iss": self.signing_key_sid, |
| 82 | + "sub": self.account_sid, |
| 83 | + "exp": now + self.ttl, |
| 84 | + "grants": grants |
| 85 | + } |
| 86 | + |
| 87 | + if self.nbf is not None: |
| 88 | + payload['nbf'] = self.nbf |
| 89 | + |
| 90 | + return jwt.encode(payload, self.secret, headers=headers, |
| 91 | + algorithm=algorithm) |
| 92 | + |
| 93 | + def __str__(self): |
| 94 | + return self.to_jwt() |
0 commit comments