| Total Complexity | 4 |
| Total Lines | 29 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | """ |
||
| 14 | class CoturnHMAC: |
||
| 15 | def __init__(self, coturn_urls=None, coturn_keys=None): |
||
| 16 | self.coturn_urls = coturn_urls or COTURN_URLS |
||
| 17 | self.coturn_keys = coturn_keys or COTURN_KEYS |
||
| 18 | |||
| 19 | def fetch_token(self, username='faf-user', ttl=None): |
||
| 20 | servers = [] |
||
| 21 | |||
| 22 | ttl = ttl or TWILIO_TTL |
||
| 23 | if ttl is not None: |
||
| 24 | ttl = int(ttl) |
||
| 25 | else: |
||
| 26 | ttl = 3600*24 |
||
| 27 | |||
| 28 | |||
| 29 | # create hmac of coturn_key + timestamp:username |
||
| 30 | timestamp = int(time.time()) + ttl |
||
| 31 | token_name = "{}:{}".format(timestamp, username) |
||
| 32 | |||
| 33 | for coturn_url, coturn_key in zip(self.coturn_urls, self.coturn_keys): |
||
| 34 | secret = hmac.new(coturn_key) |
||
| 35 | hmac.update(token_name) |
||
| 36 | auth_token = base64.b64encode(hmac.digest()) |
||
| 37 | |||
| 38 | server.append(dict(url=coturn_url, |
||
| 39 | username=token_name, |
||
| 40 | credential=auth_token)) |
||
| 41 | |||
| 42 | return servers |
||
| 43 |