Fix auth request, now able to specify needed scope for the client

This commit is contained in:
Ansem 2017-04-10 15:23:08 +00:00 cometido por Lorenz Diener
pare ccaaea0072
commit e51e5cc521

Veure arxiu

@ -2,7 +2,6 @@
import os import os
from urllib.parse import urlencode
import os.path import os.path
import mimetypes import mimetypes
import time import time
@ -10,10 +9,13 @@ import random
import string import string
import pytz import pytz
import datetime import datetime
from contextlib import closing
from urllib.parse import urlencode
import pytz
import requests
import dateutil import dateutil
import dateutil.parser import dateutil.parser
from contextlib import closing
import requests
class Mastodon: class Mastodon:
""" """
@ -48,6 +50,7 @@ class Mastodon:
Returns client_id and client_secret. Returns client_id and client_secret.
""" """
request_data = { request_data = {
'client_name': client_name, 'client_name': client_name,
'scopes': " ".join(scopes) 'scopes': " ".join(scopes)
@ -154,9 +157,9 @@ class Mastodon:
self._refresh_token = value self._refresh_token = value
return return
def auth_request_url(self, client_id: str = None, redirect_uris: str = "urn:ietf:wg:oauth:2.0:oob") -> str: def auth_request_url(self, client_id: str = None, redirect_uris: str = "urn:ietf:wg:oauth:2.0:oob", scopes: list = ['read', 'write', 'follow']) -> str:
"""Returns the url that a client needs to request the grant from the server. """Returns the url that a client needs to request the grant from the server.
https://mastodon.social/oauth/authorize?client_id=XXX&response_type=code&redirect_uris=YYY https://mastodon.social/oauth/authorize?client_id=XXX&response_type=code&redirect_uris=YYY&scope=read+write+follow
""" """
if client_id is None: if client_id is None:
client_id = self.client_id client_id = self.client_id
@ -169,6 +172,7 @@ class Mastodon:
params['client_id'] = client_id params['client_id'] = client_id
params['response_type'] = "code" params['response_type'] = "code"
params['redirect_uri'] = redirect_uris params['redirect_uri'] = redirect_uris
params['scope'] = " ".join(scopes)
formatted_params = urlencode(params) formatted_params = urlencode(params)
return "".join([self.api_base_url, "/oauth/authorize?", formatted_params]) return "".join([self.api_base_url, "/oauth/authorize?", formatted_params])
@ -203,7 +207,7 @@ class Mastodon:
params = self.__generate_params(locals(), ['scopes', 'to_file', 'username', 'password', 'code']) params = self.__generate_params(locals(), ['scopes', 'to_file', 'username', 'password', 'code'])
params['grant_type'] = 'refresh_token' params['grant_type'] = 'refresh_token'
else: else:
raise MastodonIllegalArgumentError('Invalid user name, password, redirect_uris or scopes') raise MastodonIllegalArgumentError('Invalid arguments given. username and password or code are required.')
params['client_id'] = self.client_id params['client_id'] = self.client_id
params['client_secret'] = self.client_secret params['client_secret'] = self.client_secret
@ -216,7 +220,10 @@ class Mastodon:
except Exception as e: except Exception as e:
import traceback import traceback
traceback.print_exc() traceback.print_exc()
raise MastodonIllegalArgumentError('Invalid user name, password, redirect_uris or scopes: %s' % e) if username is not None or password is not None:
raise MastodonIllegalArgumentError('Invalid user name, password, or redirect_uris: %s' % e)
elif code is not None:
raise MastodonIllegalArgumentError('Invalid access token or redirect_uris: %s' % e)
requested_scopes = " ".join(sorted(scopes)) requested_scopes = " ".join(sorted(scopes))
received_scopes = " ".join(sorted(response["scope"].split(" "))) received_scopes = " ".join(sorted(response["scope"].split(" ")))