Refactor doRequestOAuth

The selected scopes list can't be created until completion; otherwise
turning off a scope in one API turns it off in all other APIs.

Elimination of child scopes is still supported in select-default.

Top level API list is sorted.
This commit is contained in:
Ross Scroggs
2016-01-04 12:23:54 -08:00
parent cb79688a73
commit d258d4da63

View File

@ -8781,32 +8781,28 @@ def doDeleteOAuth():
UBER_SCOPES = { UBER_SCOPES = {
u'gmail-v1': [u'https://mail.google.com/'], u'gmail-v1': [u'https://mail.google.com/'],
u'drive-v2': [u'https://www.googleapis.com/auth/drive'],
u'appsactivity-v1': [u'https://www.googleapis.com/auth/activity']
} }
def select_default_scopes(apis): def select_default_scopes(apis):
scopes = []
for api_name, api in apis.items(): for api_name, api in apis.items():
if api_name in UBER_SCOPES.keys(): if api_name in UBER_SCOPES:
scopes += UBER_SCOPES[api_name] api[u'use_scopes'] = UBER_SCOPES[api_name]
else: else:
scopes += api[u'auth'][u'oauth2'][u'scopes'].keys() scopes = api[u'auth'][u'oauth2'][u'scopes'].keys()
scopes.sort() scopes.sort()
selected_scopes = set() api[u'use_scopes'] = []
# reduce # of scopes by checking if a scope is a substring of another # reduce # of scopes by checking if a scope is a substring of another
# which should mean it covers same API operations. Add a . at end # which should mean it covers same API operations. Add a . at end
# to prevent things like directory.users removing directory.userschema # to prevent things like directory.users removing directory.userschema
i = 0 i = 0
count = len(scopes) count = len(scopes)
while i < count: while i < count:
scope = scopes[i] scope = scopes[i]
selected_scopes.add(scope) api[u'use_scopes'].append(scope)
i += 1 i += 1
scope += u'.' scope += u'.'
while (i < count) and scopes[i].startswith(scope): while (i < count) and scopes[i].startswith(scope):
i += 1 i += 1
return selected_scopes
def getSelection(limit): def getSelection(limit):
while True: while True:
@ -8838,21 +8834,22 @@ def doRequestOAuth():
all_apis[api_name] = service._rootDesc all_apis[api_name] = service._rootDesc
api_titles[api_name] = api_name api_titles[api_name] = api_name
api_index = [] api_index = []
for title, api_name in sorted(api_titles.items()): for _, api_name in sorted(api_titles.items()):
api_index.append(api_name) api_index.append(api_name)
i = len(api_index) i = len(api_index)
if GM_Globals[GM_GAMSCOPES_LIST]: if GM_Globals[GM_GAMSCOPES_LIST]:
selected_scopes = set(GM_Globals[GM_GAMSCOPES_LIST]) for api in all_apis:
all_apis[api][u'use_scopes'] = list(set(GM_Globals[GM_GAMSCOPES_LIST]) & set(all_apis[api][u'auth'][u'oauth2'][u'scopes'].keys()))
else: else:
selected_scopes = select_default_scopes(all_apis) select_default_scopes(all_apis)
while True: while True:
#os.system([u'clear', u'cls'][GM_Globals[GM_WINDOWS]]) #os.system([u'clear', u'cls'][GM_Globals[GM_WINDOWS]])
print u'Select the APIs to use with GAM.' print u'Select the APIs to use with GAM.'
print print
for n in range(i): for n in range(i):
api = all_apis[api_index[n]] api = all_apis[api_index[n]]
api_scopes = api[u'auth'][u'oauth2'][u'scopes'] api_scopes = api[u'auth'][u'oauth2'][u'scopes'].keys()
num_scopes_selected = len(set(api_scopes).intersection(selected_scopes)) num_scopes_selected = len(api[u'use_scopes'])
num_scopes_total = len(api_scopes) num_scopes_total = len(api_scopes)
if num_scopes_selected > 0: if num_scopes_selected > 0:
select_value = u'*' select_value = u'*'
@ -8867,14 +8864,18 @@ def doRequestOAuth():
print print
selection = getSelection(i+3) selection = getSelection(i+3)
if selection == i: # defaults if selection == i: # defaults
selected_scopes = select_default_scopes(all_apis) select_default_scopes(all_apis)
elif selection == i+1: # unselect all elif selection == i+1: # unselect all
selected_scopes.clear() for api in all_apis.keys():
all_apis[api][u'use_scopes'] = []
elif selection == i+3: # continue elif selection == i+3: # continue
selected_scopes = []
for api in all_apis.keys():
selected_scopes += all_apis[api][u'use_scopes']
if not selected_scopes: if not selected_scopes:
print u'YOU MUST SELECT AT LEAST ONE SCOPE' print u'YOU MUST SELECT AT LEAST ONE SCOPE'
continue continue
GM_Globals[GM_GAMSCOPES_LIST] = list(selected_scopes) GM_Globals[GM_GAMSCOPES_LIST] = list(set(selected_scopes))
writeFile(GC_Values[GC_GAMSCOPES_JSON], json.dumps(GM_Globals[GM_GAMSCOPES_LIST])) writeFile(GC_Values[GC_GAMSCOPES_JSON], json.dumps(GM_Globals[GM_GAMSCOPES_LIST]))
print u'Scopes file: {0}, Created'.format(GC_Values[GC_GAMSCOPES_JSON]) print u'Scopes file: {0}, Created'.format(GC_Values[GC_GAMSCOPES_JSON])
break break
@ -8884,18 +8885,17 @@ def doRequestOAuth():
api = api_index[selection] api = api_index[selection]
api_scopes = all_apis[api][u'auth'][u'oauth2'][u'scopes'].keys() api_scopes = all_apis[api][u'auth'][u'oauth2'][u'scopes'].keys()
if len(api_scopes) == 1: if len(api_scopes) == 1:
one_scope = api_scopes[0] if len(all_apis[api][u'use_scopes']) == 1:
if one_scope in selected_scopes: all_apis[api][u'use_scopes'] = []
selected_scopes.remove(one_scope)
else: else:
selected_scopes.add(one_scope) all_apis[api][u'use_scopes'] = api_scopes
else: else:
while True: while True:
#os.system([u'clear', u'cls'][GM_Globals[GM_WINDOWS]]) #os.system([u'clear', u'cls'][GM_Globals[GM_WINDOWS]])
print print
x = 0 x = 0
for scope in api_scopes: for scope in api_scopes:
if scope in selected_scopes: if scope in all_apis[api][u'use_scopes']:
select_value = u'*' select_value = u'*'
else: else:
select_value = u' ' select_value = u' '
@ -8910,20 +8910,21 @@ def doRequestOAuth():
print print
selection = getSelection(x+4) selection = getSelection(x+4)
if selection < x: # select if selection < x: # select
if api_scopes[selection] in selected_scopes: if api_scopes[selection] in all_apis[api][u'use_scopes']:
selected_scopes.remove(api_scopes[selection]) all_apis[api][u'use_scopes'].remove(api_scopes[selection])
else: else:
selected_scopes.add(api_scopes[selection]) all_apis[api][u'use_scopes'].append(api_scopes[selection])
elif selection == x: # defaults elif selection == x: # defaults
selected_scopes = selected_scopes.difference(api_scopes) just_this_api = {api: all_apis[api]}
selected_scopes = selected_scopes.union(select_default_scopes({api: all_apis[api]})) select_default_scopes(just_this_api)
all_apis[api][u'use_scopes'] = just_this_api[api][u'use_scopes']
elif selection == x+1: # read-only elif selection == x+1: # read-only
selected_scopes = selected_scopes.difference(api_scopes) all_apis[api][u'use_scopes'] = []
for scope in api_scopes: for scope in api_scopes:
if scope.endswith(u'.readonly'): if scope.endswith(u'.readonly'):
selected_scopes.add(scope) all_apis[api][u'use_scopes'].append(scope)
elif selection == x+2: # unselect all elif selection == x+2: # unselect all
selected_scopes = selected_scopes.difference(api_scopes) all_apis[api][u'use_scopes'] = []
elif selection == x+4: # back elif selection == x+4: # back
break break
else: # cancel else: # cancel