text
stringlengths
1
93.6k
import requests
VERSION = '0.1'
class LogFormatter(logging.Formatter):
"""
Class to format log messages based on their type.
"""
FORMATS = {logging.DEBUG: "[d] %(message)s",
logging.INFO: "[*] %(message)s",
logging.ERROR: "[-] %(message)s",
logging.CRITICAL: "[-] FATAL: %(message)s",
'DEFAULT': "%(message)s"}
def format(self, record):
self._fmt = self.FORMATS.get(record.levelno, self.FORMATS['DEFAULT'])
return logging.Formatter.format(self, record)
def code_to_token(args):
"""
Convert code to token.
"""
token = None
params = {}
for param in ['client_id', 'redirect_uri', 'response_type', 'scope']:
params[param] = vars(args)[param]
request = do_request(args.server, 'GET', params, args)
try:
if request and request.status_code == 302:
if 'Location' in request.headers:
if args.token_name in request.headers['Location']:
token = request.headers['Location'].split(args.token_name + '=')[1].split('&')[0]
except IndexError:
pass
if token:
logging.info('Token = %s', token)
else:
logging.debug('No token found: %s', request.headers)
return token
def do_request(url, method, params, args):
"""
Wrapper around the request object.
Perform HTTP @method on @uri using @dictionary.
"""
request = None
dictionary = dict(cookies=args.cookies,
allow_redirects=False,
proxies=args.proxy,
verify=args.verify)
logging.debug('Performing {0} on {1}'.format(method, url))
logging.debug(dictionary)
if not args.verify:
requests.packages.urllib3.disable_warnings(
requests.packages.urllib3.exceptions.InsecureRequestWarning)
try:
if method == 'DELETE':
request = requests.delete(url, params=params, **dictionary)
elif method == 'GET':
request = requests.get(url, params=params, **dictionary)
if method == 'POST':
request = requests.post(url)
if method == 'PUT':
request = requests.put(url)
except requests.exceptions.ConnectionError as exception:
logging.error('Could not connect to {0}'.format(exception.message))
return None
except requests.exceptions.MissingSchema:
logging.error('The schema seems to be missing for {0}'.format(url), -1)
return None
if request:
logging.debug(request.headers)
return request
def parse_arguments(banner):
"""
Parse and return command line arguments.
"""
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent(banner + '''\
- Test script for OAuth 2.0 framework
Copyright (C) 2017 Peter Mosmans [Radically Open Security]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.'''))
parser.add_argument('--debug', action='store_true',
help='Show debug information')
parser.add_argument('-v', '--verbose', action='store_true',
help='Be more verbose')
parser.add_argument('--client-id', action='store',