Skip to content

Commit 9794f91

Browse files
committed
refactor: switch to token credentials
1 parent 80c7bfa commit 9794f91

File tree

6 files changed

+70
-88
lines changed

6 files changed

+70
-88
lines changed

.profile.d/certificates.sh

Lines changed: 0 additions & 9 deletions
This file was deleted.

.profile.d/keys.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
mkdir -p keys
4+
5+
AUTH_KEY=APNS_AUTH_KEY_${APNS_AUTH_KEY_ID}
6+
echo "${!AUTH_KEY}" > keys/$APNS_AUTH_KEY_ID.pem

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ verify_ssl = true
77
pytest = "*"
88

99
[packages]
10-
apns2 = "==0.7.1"
10+
apns2 = {editable = true, git = "https://github.com/Pr0Ger/PyAPNs2.git"}
1111
bugsnag = "==4.1.0"
1212
cryptography = "==3.3.2"
1313
gunicorn = "==20.1.0"

Pipfile.lock

Lines changed: 54 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

palaverapi/utils.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,31 @@
22
from typing import Optional
33

44
from apns2.client import APNsClient, NotificationPriority
5+
from apns2.credentials import TokenCredentials
56
from apns2.errors import BadDeviceToken, Unregistered
67
from apns2.payload import Payload
78
from bugsnag import Client
89

910
from palaverapi.models import Device, database
1011

1112
TOPIC = 'com.kylefuller.palaver'
12-
DIRECTORY = os.path.abspath(
13-
os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'certificates')
13+
KEYS_DIRECTORY = os.path.abspath(
14+
os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'keys')
1415
)
16+
AUTH_KEY_ID = os.environ.get('APNS_AUTH_KEY_ID')
17+
TEAM_ID = os.environ.get('APNS_TEAM_ID')
1518
bugsnag_client = Client(asynchronous=False, install_sys_hook=False)
1619
apns_client = None
1720

1821

1922
def load_apns_client() -> APNsClient:
2023
global apns_client
21-
2224
if apns_client is None:
23-
apns_client = APNsClient(
24-
os.path.join(DIRECTORY, 'production.pem'), heartbeat_period=30
25+
auth_key_path = os.path.join(KEYS_DIRECTORY, f'{AUTH_KEY_ID}.pem')
26+
token_credentials = TokenCredentials(
27+
auth_key_path=auth_key_path, auth_key_id=AUTH_KEY_ID, team_id=TEAM_ID
2528
)
29+
apns_client = APNsClient(credentials=token_credentials, heartbeat_period=30)
2630

2731
return apns_client
2832

tasks.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,6 @@ def syncdb(context):
2020
Token.create_table()
2121

2222

23-
@task
24-
def upload_certs(context):
25-
with open('certificates/private.pem') as fp:
26-
priv_key = fp.read()
27-
28-
with open('certificates/public.pem') as fp:
29-
pub_key = fp.read()
30-
31-
run('heroku config:add "APNS_PRIVATE_KEY={}" --app palaverapi'.format(priv_key))
32-
run('heroku config:add "APNS_PUBLIC_KEY={}" --app palaverapi'.format(pub_key))
33-
34-
3523
def configure_db():
3624
database_path = os.path.join(
3725
os.path.dirname(os.path.realpath(__file__)), 'tests.sqlite'

0 commit comments

Comments
 (0)