Skip to content

Commit

Permalink
port to oauthlib
Browse files Browse the repository at this point in the history
* ported to from oauth2client (deprecated) to oauthlib
* slight refactoring into utils/appliation
* refactored application logic into a more flask-familiar layout
  • Loading branch information
cheshirekow committed May 23, 2019
1 parent 6aba49b commit 316b869
Show file tree
Hide file tree
Showing 14 changed files with 524 additions and 543 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

*.pyc
*.o
*.so
Expand All @@ -24,3 +23,9 @@ dist/

# bazel poop
bazel-*

# vscode poop
.vscode/ipch

# node package manager
node_modules
28 changes: 18 additions & 10 deletions cmake/codestyle.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
# Create format and lint rules for module files
#
# usage:
# ~~~
# format_and_lint(module
# bar.h bar.cc
# CMAKE CMakeLists.txt test/CMakeLists.txt
# CC foo.h foo.cc
# PY foo.py)
#
# ~~~

# Will create rules `${module}_lint` and `${module}_format` using the standard
# code formatters and lint checkers for the appropriate language. These
# tools are:
#
# ~~~
# CMAKE:
# formatter: cmake-format
#
Expand All @@ -23,7 +26,7 @@
# PYTHON:
# formatter: autopep8
# linter: pylint
#
# ~~~
function(format_and_lint module)
set(cmake_files_)
set(cc_files_)
Expand Down Expand Up @@ -65,24 +68,29 @@ function(format_and_lint module)
set(fmtcmds_)
set(depfiles_)
if(cmake_files_)
list(APPEND fmtcmds_ COMMAND python -Bm cmake_format -i ${cmake_files_})
list(APPEND fmtcmds_ COMMAND env PYTHONPATH=${CMAKE_SOURCE_DIR}
python -Bm cmake_format -i ${cmake_files_})
list(APPEND depfiles_ ${cmake_files_}
${CMAKE_SOURCE_DIR}/.cmake-format.py)
endif()
if(cc_files_)
list(APPEND fmtcmds_ COMMAND clang-format -style file -i ${cc_files_})
list(APPEND lntcmds_ COMMAND clang-tidy -p ${CMAKE_BINARY_DIR} ${cc_files_})
list(APPEND fmtcmds_ COMMAND clang-format-6.0 -style file -i ${cc_files_})
list(APPEND lntcmds_
COMMAND clang-tidy-6.0 -p ${CMAKE_BINARY_DIR} ${cc_files_})
list(APPEND lntcmds_ COMMAND cpplint ${cc_files_})
list(APPEND depfiles_ ${cc_files_}
${CMAKE_SOURCE_DIR}/.clang-format
${CMAKE_SOURCE_DIR}/CPPLINT.cfg
${CMAKE_SOURCE_DIR}/setup.cfg)
${CMAKE_SOURCE_DIR}/CPPLINT.cfg)
endif()
if(py_files_)
list(APPEND fmtcmds_ COMMAND autopep8 -i ${py_files_})
list(APPEND fmtcmds_ COMMAND yapf -i ${py_files_})
list(APPEND lntcmds_ COMMAND pylint ${py_files_})
list(APPEND lntcmds_ COMMAND flake8 ${py_files_})
list(APPEND lntcmds_ COMMAND env PYTHONPATH=${CMAKE_SOURCE_DIR}
pylint ${py_files_})
# NOTE(josh): flake8 tries to use semaphores which fail in our containers
# https://bugs.python.org/issue3770 (probably due to /proc/shmem or
# something not being mounted)
list(APPEND lntcmds_ COMMAND env PYTHONPATH=${CMAKE_SOURCE_DIR}
flake8 --jobs 1 ${py_files_})
list(APPEND depfiles_ ${py_files_}
${CMAKE_SOURCE_DIR}/.flake8
${CMAKE_SOURCE_DIR}/.pep8
Expand Down
4 changes: 2 additions & 2 deletions cmake/doctools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
function(sphinx module)
set(stamp_path_ ${CMAKE_CURRENT_BINARY_DIR}/${module}_doc.stamp)
add_custom_command(OUTPUT ${stamp_path_}
COMMAND sphinx-build -M html ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND env PYTHONPATH=${CMAKE_SOURCE_DIR}
sphinx-build -M html ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
COMMAND touch ${stamp_path_}
DEPENDS ${ARGN}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
add_custom_target(${module}_doc DEPENDS ${stamp_path_})
add_dependencies(doc ${module}_doc)
endfunction()

7 changes: 6 additions & 1 deletion oauthsub/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
format_and_lint(oauthsub __init__.py __main__.py auth_service.py)
format_and_lint(oauthsub
__init__.py
__main__.py
auth_service.py
configuration.py
util.py)

add_subdirectory(doc)
2 changes: 1 addition & 1 deletion oauthsub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
See: https://developers.google.com/api-client-library/python/auth/web-app
"""

VERSION = "0.1.3"
VERSION = "0.2.0"
42 changes: 26 additions & 16 deletions oauthsub/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import oauthsub
from oauthsub import auth_service
from oauthsub import configuration

logger = logging.getLogger("oauthsub")

Expand Down Expand Up @@ -39,8 +40,8 @@ def dump_config(config, outfile):
Dump configuration to the output stream
"""
ppr = pprint.PrettyPrinter(indent=2)
for key in auth_service.Configuration.get_fields():
helptext = auth_service.VARDOCS.get(key, None)
for key in configuration.Configuration.get_fields():
helptext = configuration.VARDOCS.get(key, None)
if helptext:
for line in textwrap.wrap(helptext, 78):
outfile.write('# ' + line + '\n')
Expand Down Expand Up @@ -69,12 +70,12 @@ def setup_parser(parser, config_dict):
choices=["flask", "gevent", "twisted"],
help="Which WGSI server to use")

for key in auth_service.Configuration.get_fields():
for key in configuration.Configuration.get_fields():
if key in ("server",):
continue

value = config_dict[key]
helptext = auth_service.VARDOCS.get(key, None)
helptext = configuration.VARDOCS.get(key, None)
# NOTE(josh): argparse store_true isn't what we want here because we want
# to distinguish between "not specified" = "default" and "specified"
if isinstance(value, bool):
Expand All @@ -96,32 +97,39 @@ def setup_parser(parser, config_dict):


def main():
format_str = '%(levelname)-4s %(filename)s [%(lineno)-3s] : %(message)s'
logging.basicConfig(level=logging.DEBUG,
format=format_str,
datefmt='%Y-%m-%d %H:%M:%S',
filemode='w')
# This is necessary for testing with non-HTTPS localhost
# Remove this if deploying to production
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'

# This is necessary because Azure does not guarantee
# to return scopes in the same case and order as requested
os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
os.environ['OAUTHLIB_IGNORE_SCOPE_CHANGE'] = '1'

logging.basicConfig(level=logging.DEBUG, filemode='w')
parser = argparse.ArgumentParser(
prog='oauthsub', description=auth_service.__doc__)

config_dict = auth_service.Configuration().serialize()
config_dict = configuration.Configuration().serialize()
setup_parser(parser, config_dict)
args = parser.parse_args()

if args.dump_config:
dump_config(auth_service.Configuration(), sys.stdout)
dump_config(configuration.Configuration(), sys.stdout)
sys.exit(0)

if args.config_file:
configpath = os.path.expanduser(args.config_file)
config_dict["__file__"] = os.path.realpath(configpath)
with io.open(configpath, 'r', encoding='utf8') as infile:
# pylint: disable=W0122
exec(infile.read(), config_dict)
config_dict.pop("__file__")

for key, value in vars(args).items():
if key in config_dict and value is not None:
config_dict[key] = value
config = auth_service.Configuration(**config_dict)
config = configuration.Configuration(**config_dict)

# Create directory for logs if it doesn't exist
if not os.path.exists(config.logdir):
Expand All @@ -138,12 +146,14 @@ def main():
format_str = ('%(asctime)s %(levelname)-4s %(filename)s [%(lineno)-3s] :'
' %(message)s')
filelog.setFormatter(logging.Formatter(format_str))
logging.getLogger('').addHandler(filelog)
logging.getLogger("").addHandler(filelog)

config_dict = config.serialize()
config_dict.pop('secrets', None)
config_dict.pop('client_secrets', None)
logging.info('Configuration: %s', json.dumps(config_dict, indent=2))
logging.info(
'Configuration: %s',
json.dumps(config_dict, indent=2, sort_keys=True))

# NOTE(josh): hack to deal with jinja's failure to resolve relative imports
# to absolute paths
Expand All @@ -154,7 +164,7 @@ def main():
app.run(threaded=True, host=config.host, port=config.port)
elif config.server == "gevent":
from gevent.pywsgi import WSGIServer
WSGIServer((config.host, config.port), app.flask).serve_forever()
WSGIServer((config.host, config.port), app).serve_forever()
elif config.server == "twisted":
from twisted.web import server
from twisted.web.wsgi import WSGIResource
Expand All @@ -164,7 +174,7 @@ def main():
thread_pool = ThreadPool()
thread_pool.start()
reactor.addSystemEventTrigger('after', 'shutdown', thread_pool.stop)
resource = WSGIResource(reactor, thread_pool, app.flask)
resource = WSGIResource(reactor, thread_pool, app)
factory = server.Site(resource)
reactor.listenTCP(config.port, factory)
reactor.run()
Expand Down
Loading

0 comments on commit 316b869

Please sign in to comment.