Skip to content

Commit c2a955d

Browse files
authored
Merge pull request #126 from MrTeferi/master
Add refresh_patreon command to rotate access token
2 parents 7e2d230 + 8e9f34c commit c2a955d

File tree

6 files changed

+72
-6
lines changed

6 files changed

+72
-6
lines changed

MPCAutofill/MPCAutofill/.env.dist

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ REDDIT=https://www.reddit.com/r/mpcproxies/
3939
DISCORD=http://mprox.link/discord
4040
THEME=superhero
4141

42-
# PATREON - Key from "Creator's Access Token"
43-
PATREON_KEY=
42+
# Patreon keys
43+
PATREON_ACCESS=
44+
PATREON_REFRESH=
45+
PATREON_CLIENT=
46+
PATREON_SECRET=
4447
PATREON_URL=

MPCAutofill/MPCAutofill/settings.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@
106106

107107
# Database
108108
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
109-
110109
DATABASES = {
111110
"default": {
112111
"ENGINE": env("DATABASE_ENGINE", default="django.db.backends.sqlite3"),
@@ -186,5 +185,8 @@
186185
DEFAULT_CARDBACK_IMAGE_NAME = env("DEFAULT_CARDBACK_IMAGE_NAME", default="Black Lotus")
187186

188187
# PATREON
188+
PATREON_ACCESS = env("PATREON_ACCESS", default="")
189+
PATREON_REFRESH = env("PATREON_REFRESH", default="")
190+
PATREON_CLIENT = env("PATREON_CLIENT", default="")
191+
PATREON_SECRET = env("PATREON_SECRET", default="")
189192
PATREON_URL = env("PATREON_URL", default="")
190-
PATREON_KEY = env("PATREON_KEY", default="")
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os.path
2+
import platform
3+
from typing import Any
4+
5+
import dotenv
6+
import requests
7+
8+
from django.core.management.base import BaseCommand
9+
10+
from MPCAutofill.settings import (
11+
BASE_DIR,
12+
PATREON_CLIENT,
13+
PATREON_REFRESH,
14+
PATREON_SECRET,
15+
)
16+
17+
patreon_header = {
18+
"User-Agent": f"Patreon-Python, version 0.5.1, platform {platform.platform()}",
19+
}
20+
21+
22+
class Command(BaseCommand):
23+
help = "Refreshes the Patreon access token"
24+
25+
def handle(self, *args: Any, **kwargs: dict[str, Any]) -> None:
26+
# TODO: Potentially more secure to store these keys in database?
27+
# Make a request to refresh the Patreon access token
28+
res = requests.post(
29+
# https://docs.patreon.com/#step-7-keeping-up-to-date
30+
url="https://www.patreon.com/api/oauth2/token",
31+
params={
32+
"grant_type": "refresh_token",
33+
"refresh_token": PATREON_REFRESH,
34+
"client_id": PATREON_CLIENT,
35+
"client_secret": PATREON_SECRET,
36+
},
37+
headers=patreon_header,
38+
).json()
39+
40+
# Were the tokens received?
41+
if "access_token" not in res or "refresh_token" not in res:
42+
print(f"Response: {res}")
43+
raise KeyError("Patreon response missing access_token or key_token!")
44+
45+
# Set access token
46+
dotenv.set_key(
47+
os.path.join(BASE_DIR, "MPCAutofill/.env"), "PATREON_ACCESS", res["access_token"], quote_mode="never"
48+
)
49+
50+
# Set refresh token
51+
dotenv.set_key(
52+
os.path.join(BASE_DIR, "MPCAutofill/.env"), "PATREON_REFRESH", res["refresh_token"], quote_mode="never"
53+
)
54+
55+
# Notify user
56+
print("Patreon Access and Refresh tokens updated successfully!")

MPCAutofill/cardpicker/utils/patreon.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
import requests
66

7-
from MPCAutofill.settings import PATREON_KEY
7+
from MPCAutofill.settings import PATREON_ACCESS
88

99
# Header must be included to access Patreon info
1010
patreon_header = {
11-
"Authorization": f"Bearer {PATREON_KEY}",
11+
"Authorization": f"Bearer {PATREON_ACCESS}",
1212
"User-Agent": f"Patreon-Python, version 0.5.1, platform {platform.platform()}",
1313
}
1414

MPCAutofill/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ attrs
22
chardet
33
click==8.0.4
44
defusedxml
5+
python-dotenv
56
Django~=4.0
67
django-crispy-forms~=1.14
78
django-elasticsearch-dsl~=7.2

refresh_patreon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# helper script to refresh patreon tokens
2+
3+
cd ~/mpc-autofill/MPCAutofill
4+
~/mpc-autofill/MPCAutofill/env/bin/python ~/mpc-autofill/MPCAutofill/manage.py refresh_patreon

0 commit comments

Comments
 (0)