forked from TranzerDev/PKPassCreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
211 lines (167 loc) · 6.35 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import json
import hashlib
import os
import sys
import shutil
from typing import Callable, Optional, TypedDict
from zipfile import ZipFile
from pass_dict import pass_dict
PK_PASS_NAME = "Generic"
OPENSSL_APP = "openssl"
SUPPORTED_ASSET_FILES = [
"icon.png",
"background.png",
"logo.png",
"footer.png",
"strip.png",
"thumbnail.png",
]
def with_clean_up(func: Callable):
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except Exception as e:
print(e)
finally:
generated_files = [
"pass.json",
"manifest.json",
"passcertificate.pem",
"passkey.pem",
"signature",
]
for asset_file in SUPPORTED_ASSET_FILES + generated_files:
if os.path.exists(asset_file):
os.remove(asset_file)
return wrapper
@with_clean_up
def main():
arguments = parse_arguments()
pass_type_identifier = arguments["pass_type_identifier"]
if pass_type_identifier == "":
raise Exception("pass type identifier is empty")
team_identifier = arguments["team_identifier"]
if team_identifier == "":
raise Exception("team identifier is empty")
pass_dict_copy = create_pass_dict(
pass_type_identifier=pass_type_identifier,
team_identifier=team_identifier,
)
pass_dict_json = json.dumps(pass_dict_copy, indent=2)
with open("pass.json", "w") as f:
f.write(pass_dict_json)
create_manifest_json(asset_path=f"{PK_PASS_NAME}.pass")
certificate_password = arguments["certificate_password"]
certificate_path = arguments["certificate_path"]
# Create pass certificate
os_code = os.system(f"{OPENSSL_APP} pkcs12 -in {certificate_path} -clcerts -nokeys -out passcertificate.pem -passin pass:{certificate_password}")
if os_code != 0:
raise Exception("could not create pass certificate")
key_password = "password"
# Create pass key
os_code = os.system(f"{OPENSSL_APP} pkcs12 -in {certificate_path} -nocerts -out passkey.pem -passin pass:{certificate_password} -passout pass:{key_password}")
if os_code != 0:
raise Exception("could not create pass key")
wwdr_path = arguments["wwdr_path"]
# Create signature
os_code = os.system(f"{OPENSSL_APP} smime -binary -sign -certfile {wwdr_path} -signer passcertificate.pem -inkey passkey.pem -in manifest.json -out signature -outform DER -passin pass:{key_password}")
if os_code != 0:
raise Exception("could not create signature")
asset_files_to_delete = [
"passkey.pem",
"passcertificate.pem",
"signature",
"pass.json",
]
asset_files = [
"signature",
"pass.json",
"manifest.json",
]
for (_, _, filenames) in os.walk(f"{PK_PASS_NAME}.pass"):
for filename in filenames:
if filename in SUPPORTED_ASSET_FILES:
shutil.copy2(f"{PK_PASS_NAME}.pass/{filename}", filename)
asset_files_to_delete.append(filename)
asset_files.append(filename)
with ZipFile(f"{PK_PASS_NAME}.pkpass", "w") as zip_file:
for asset_file in asset_files:
zip_file.write(asset_file)
os_code = os.system(f"open {PK_PASS_NAME}.pkpass")
if os_code != 0:
raise Exception("could not open pkpass")
class Arguments(TypedDict):
certificate_password: str
pass_type_identifier: str
team_identifier: str
certificate_path: str
wwdr_path: str
def parse_arguments() -> "Arguments":
certificate_password: Optional[str] = None
pass_type_identifier: Optional[str] = None
team_identifier: Optional[str] = None
certificate_path: Optional[str] = None
wwdr_path: Optional[str] = None
skip_next_value = False
for index, arg in enumerate(sys.argv[1:]):
if skip_next_value:
skip_next_value = False
continue
def get_next_value():
if index + 1 < len(sys.argv):
nonlocal skip_next_value
skip_next_value = True
return sys.argv[index + 2]
match arg:
case "--certificate-password":
certificate_password = get_next_value()
case "--pass-type-identifier":
pass_type_identifier = get_next_value()
case "--team-identifier":
team_identifier = get_next_value()
case "--certificate-path":
certificate_path = get_next_value()
case "--wwdr-path":
wwdr_path = get_next_value()
if certificate_password is None:
certificate_password = input("what is the password of the provided certificate?\n")
if pass_type_identifier is None:
pass_type_identifier = input("provide a pass type identifier\n")
if team_identifier is None:
team_identifier = input("provide a team identifier\n")
if certificate_path is None:
certificate_path = input("provide the path to your certificate\n")
if wwdr_path is None:
wwdr_path = input("provide the path to your WWDR certificate\n")
return {
"certificate_password": certificate_password,
"pass_type_identifier": pass_type_identifier,
"team_identifier": team_identifier,
"certificate_path": certificate_path,
"wwdr_path": wwdr_path,
}
def create_manifest_json(asset_path: str):
with open("pass.json", "r") as f:
pass_json = f.read()
hashed_pass_json = hashlib.sha1(pass_json.encode('utf-8')).hexdigest()
manifest_dict = {"pass.json": hashed_pass_json}
for (_, _, filenames) in os.walk(asset_path):
for filename in filenames:
if filename in SUPPORTED_ASSET_FILES:
manifest_dict[filename] = hashlib.sha1(
open(f"{asset_path}/{filename}", "rb").read()
).hexdigest()
with open(f"manifest.json", "w") as f:
f.write(json.dumps(manifest_dict, indent=4))
def create_pass_dict(pass_type_identifier: str, team_identifier: str):
pass_dict_copy = pass_dict.copy()
pass_dict_copy["passTypeIdentifier"] = pass_type_identifier
pass_dict_copy["teamIdentifier"] = team_identifier
return pass_dict_copy
main()