Fix auth request, now able to specify needed scope for the client
This commit is contained in:
pare
54541f81de
commit
0d251bbf46
S'han modificat 1 arxius amb 14 adicions i 7 eliminacions
|
@ -2,17 +2,19 @@
|
||||||
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from urllib.parse import urlencode
|
|
||||||
import os.path
|
import os.path
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import time
|
import time
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
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:
|
||||||
"""
|
"""
|
||||||
|
@ -47,6 +49,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)
|
||||||
|
@ -153,9 +156,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
|
||||||
|
@ -168,6 +171,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])
|
||||||
|
|
||||||
|
@ -202,7 +206,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
|
||||||
|
@ -215,7 +219,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(" ")))
|
||||||
|
|
Loading…
Referencia en una nova incidència