Skip to content

Commit 0019ab3

Browse files
committed
Add ssl context helper
1 parent c75cb85 commit 0019ab3

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

tests/test_util.py

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
as_utc_from_local,
2323
get_configuration_url,
2424
get_latest_activity,
25+
get_ssl_context,
2526
update_doorbell_image_from_activity,
2627
update_lock_detail_from_activity,
2728
)
@@ -398,3 +399,8 @@ def test_get_configuration_url():
398399
assert get_configuration_url(Brand.AUGUST) == "https://account.august.com"
399400
assert get_configuration_url(Brand.YALE_ACCESS) == "https://account.august.com"
400401
assert get_configuration_url(Brand.YALE_HOME) == "https://account.aaecosystem.com"
402+
403+
404+
def test_get_ssl_context():
405+
"""Test getting the ssl context is cached."""
406+
assert get_ssl_context() is get_ssl_context()

yalexs/util.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import datetime
2+
from functools import cache
3+
import random
4+
import ssl
25
from typing import Optional, Union
36

47
from .activity import (
@@ -112,3 +115,15 @@ def as_utc_from_local(dtime: datetime.datetime) -> datetime.datetime:
112115
def get_configuration_url(brand: Brand) -> str:
113116
"""Return the configuration URL for the brand."""
114117
return CONFIGURATION_URLS[brand]
118+
119+
120+
@cache
121+
def get_ssl_context() -> ssl.SSLContext:
122+
"""Return an SSL context for cloudflare."""
123+
context = ssl.create_default_context()
124+
ciphers = [cipher["name"] for cipher in context.get_ciphers()]
125+
default_ciphers = ciphers[:3]
126+
backup_ciphers = ciphers[3:]
127+
random.shuffle(backup_ciphers)
128+
context.set_ciphers(":".join((*default_ciphers, *backup_ciphers)))
129+
return context

0 commit comments

Comments
 (0)