-
Notifications
You must be signed in to change notification settings - Fork 0
/
cattenbak.py
executable file
·219 lines (185 loc) · 7.38 KB
/
cattenbak.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
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
import requests
import json
import datetime
import argparse
import urllib.parse
from typing import Optional, List, Any, Dict, Set
cat_api = "https://cat.eduroam.org/user/API.php"
cat_download_api = "https://cat.eduroam.org/user/API.php"
def get_old_discovery_from_file(filename: str) -> Optional[Dict]:
try:
with open(filename, "r") as fh:
return json.load(fh)
except json.decoder.JSONDecodeError as e:
print(e)
return None
except FileNotFoundError as e:
print(e)
return None
def discovery_needs_refresh(old_discovery: Optional[Dict], new_discovery: Dict) -> bool:
if old_discovery is None:
return True
old_instances = old_discovery["instances"] if "instances" in old_discovery else None
new_instances = new_discovery["instances"] if "instances" in new_discovery else None
assert isinstance(new_instances, List)
if not isinstance(old_instances, List) or old_instances is None or new_instances is None:
return True
return not old_instances == new_instances
def store_file(discovery: List, filename: str):
with open(filename, "w") as fh:
json.dump(
discovery,
fh,
separators=(",", ":"),
allow_nan=False,
sort_keys=True,
ensure_ascii=True,
)
fh.write("\r\n")
def get_preferred_name(names: List, country: str) -> Optional[str]:
if len(names) == 0:
return None
lang = {}
for name in names:
lang[name["lang"]] = name["value"]
if "C" in lang:
return lang["C"]
elif country.lower() in lang:
return lang[country.lower()]
elif "en" in lang:
return lang["en"]
else:
return names[0]["value"]
def get_profiles(idp: Dict) -> List:
profiles = []
if "profiles" in idp:
profile_default = True
for profile in idp["profiles"]:
profile_name = get_preferred_name(profile["names"], idp["country"])
if profile_name is None:
profile_name = get_preferred_name(idp["names"], idp["country"])
if profile["redirect"]:
if "#letswifi" in profile["redirect"]:
# todo: visit .well-known/letswifi.json for actual endpoints
# Get base URL for OAuth flow
parsed_url = urllib.parse.urlparse(profile["redirect"])._replace(fragment="")
parsed_url = parsed_url._replace(path=parsed_url.path.rstrip("/"))
profiles.append(
{
"id": "letswifi_cat_%s" % (profile["id"]),
"name": profile_name,
"default": profile_default,
"eapconfig_endpoint": parsed_url._replace(path=parsed_url.path + "/api/eap-config/").geturl(),
"token_endpoint": parsed_url._replace(path=parsed_url.path + "/oauth/token/").geturl(),
"authorization_endpoint": parsed_url._replace(path=parsed_url.path + "/oauth/authorize/").geturl(),
"oauth": True,
}
)
profile_default = False
else:
profiles.append(
{
"id": "cat_%s" % (profile["id"]),
"redirect": profile["redirect"],
"name": profile_name,
}
)
else:
profiles.append(
{
"id": "cat_%s" % (profile["id"]),
"name": profile_name,
"eapconfig_endpoint": "%s?action=downloadInstaller&device=eap-generic&profile=%s" % (cat_download_api, profile["id"]),
"oauth": False,
}
)
return profiles
def generate(old_seq: int = None):
candidate_seq = int(datetime.datetime.utcnow().strftime("%Y%m%d00"))
if old_seq is None:
# Use a high number so we have a better chance to be over
# Let's hope this doesn't happen more than once a day ;)
seq = candidate_seq + 80
elif candidate_seq <= old_seq:
seq = old_seq + 1
else:
seq = candidate_seq
return {
"version": 1,
"seq": seq,
"instances": instances(),
}
def instances() -> List:
idp_names: Set[str] = set()
instances: List[Any] = []
r_List_everything = requests.get(cat_api + "?action=listIdentityProvidersWithProfiles")
data = r_List_everything.json()["data"]
for idp in data:
idp_name = get_preferred_name(data[idp]["names"], data[idp]["country"])
if idp_name is not None:
# Filter out duplicates
if idp_name in idp_names:
found = False
for previous_idp in instances:
if previous_idp["name"] == idp_name and previous_idp["country"] != data[idp]["country"]:
previous_idp["name"] = "%s [%s]" % (
idp_name,
previous_idp["country"],
)
found = True
if found:
idp_name = "%s [%s]" % (idp_name, data[idp]["country"])
else:
pass # it's a duplicate within it's own country
idp_names.add(idp_name)
geo = []
if "geo" in data[idp]:
for coords in data[idp]["geo"]:
geo.append(
{
"lon": round(float(coords["lon"]), 3),
"lat": round(float(coords["lat"]), 3),
}
)
profiles = get_profiles(data[idp])
if profiles:
instances.append(
{
"id": "cat_%s" % data[idp]["entityID"],
"name": idp_name,
"country": data[idp]["country"],
"cat_idp": int(data[idp]["entityID"]),
"geo": geo,
"profiles": profiles,
}
)
return sorted(instances, key=lambda idp: idp["name"])
def parse_args() -> Dict[str, str]:
parser = argparse.ArgumentParser(description="Generate geteduroam discovery files")
parser.add_argument(
"--file-path",
nargs="?",
metavar="FILE",
dest="file_path",
default="discovery.json",
help="path where to write V1 discovery file to fileystem",
)
parser.add_argument(
"-f",
"--force",
action="store_true",
help="Force writing file even if nothing changed",
)
return vars(parser.parse_args())
if __name__ == "__main__":
args = parse_args()
old_discovery = get_old_discovery_from_file(args["file_path"])
if not old_discovery or not "seq" in old_discovery:
old_discovery = None
discovery = generate(old_seq=old_discovery["seq"] if old_discovery else None)
if not old_discovery or args["force"] or discovery_needs_refresh(old_discovery, discovery):
print("Storing discovery seq %s" % discovery["seq"])
store_file(discovery, args["file_path"])
else:
print("Unchanged %d" % old_discovery["seq"])