73 lines
1.8 KiB
Python
73 lines
1.8 KiB
Python
import secrets
|
|
import validators
|
|
|
|
from .database import *
|
|
from .config import logging
|
|
|
|
|
|
def scope_check(scopes):
|
|
read_write = ['follows', 'accounts', 'lists', 'blocks', 'mutes', 'bookmarks', 'notifications', 'favourites', 'search', 'filters', 'statuses']
|
|
admin = ['read', 'write']
|
|
admin_secc = ['accounts', 'reports']
|
|
new_scopes = []
|
|
|
|
for line in scopes:
|
|
scope = line.split(':')
|
|
if len(scope) < 2:
|
|
scope[1] == None
|
|
|
|
if len(scope) < 3:
|
|
scope[2] == None
|
|
|
|
if (scope[0] in ['read', 'write'] and scope[1] in read_write) or scope[0] in ['follow', 'push'] or (scope[0] == 'admin' and scope[1] in admin and scope[2]):
|
|
new_scopes.append(line)
|
|
|
|
else:
|
|
logging.warning(f'Invalid scope: {line}')
|
|
|
|
if len(new_scopes) < 1:
|
|
return
|
|
|
|
else:
|
|
return new_scopes
|
|
|
|
|
|
class create:
|
|
def app(redirect_uri, scope, name, url):
|
|
if None in [scope, name]:
|
|
logging.debug('Missing scope or name for app')
|
|
logging.debug(f'scope: {scope}, name: {name}')
|
|
return 'MissingData'
|
|
|
|
scopes = scope_check(scope)
|
|
|
|
if scopes == None:
|
|
logging.debug(f'Invalid scopes: {scope}')
|
|
return 'InvalidScope'
|
|
|
|
if not validators.url(redirect_uri):
|
|
logging.debug(f'Invalid redirect URL: {redirect_uri}')
|
|
redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
|
|
|
|
if not validators.url(url):
|
|
logging.debug(f'Invalid app URL: {url}')
|
|
return 'InvalidURL'
|
|
|
|
client_id = secrets.token_hex(20)
|
|
client_secret = secrets.token_hex(20)
|
|
|
|
put.oauth.app(client_id, client_secret, redirect_uri, scopes, name, url)
|
|
|
|
return {'client_id': client_id, 'client_secret': client_secret, 'redirect_uris': redirect_uri, 'scopes': scopes}
|
|
|
|
|
|
def authorize(client_id, client_secret, redirect_uri, *args):
|
|
if None in [client_id, client_secret]:
|
|
logging.debug(f'Invalid secrets: {client_id}, {client_secret}')
|
|
return 'InvalidCredentials'
|
|
|
|
return
|
|
|
|
def auth_code(client_id, login_token):
|
|
pass
|