Commit cab93b20 authored by Daniele Venzano's avatar Daniele Venzano

Merge CORS and other API changes. OAuth was left out for now.

parents 844dcc69 e331f298
......@@ -18,7 +18,7 @@
from tornado.web import RequestHandler
from zoe_api.api_endpoint import APIEndpoint # pylint: disable=unused-import
from zoe_api.rest_api.utils import catch_exceptions
from zoe_api.rest_api.utils import catch_exceptions, manage_cors_headers
class DiscoveryAPI(RequestHandler):
......@@ -30,14 +30,7 @@ class DiscoveryAPI(RequestHandler):
def set_default_headers(self):
"""Set up the headers for enabling CORS."""
if self.request.headers.get('Origin') is None:
self.set_header("Access-Control-Allow-Origin", "*")
else:
self.set_header("Access-Control-Allow-Origin", self.request.headers.get('Origin'))
self.set_header("Access-Control-Allow-Credentials", "true")
self.set_header("Access-Control-Allow-Headers", "x-requested-with, Content-Type, origin, authorization, accept, client-security-token")
self.set_header("Access-Control-Allow-Methods", "OPTIONS, GET")
self.set_header("Access-Control-Max-Age", "1000")
manage_cors_headers(self)
def options(self):
"""Needed for CORS."""
......
......@@ -18,7 +18,7 @@
from tornado.web import RequestHandler
import tornado.escape
from zoe_api.rest_api.utils import catch_exceptions, get_auth
from zoe_api.rest_api.utils import catch_exceptions, get_auth, manage_cors_headers
import zoe_api.exceptions
from zoe_api.api_endpoint import APIEndpoint # pylint: disable=unused-import
......@@ -32,14 +32,7 @@ class ExecutionAPI(RequestHandler):
def set_default_headers(self):
"""Set up the headers for enabling CORS."""
if self.request.headers.get('Origin') is None:
self.set_header("Access-Control-Allow-Origin", "*")
else:
self.set_header("Access-Control-Allow-Origin", self.request.headers.get('Origin'))
self.set_header("Access-Control-Allow-Credentials", "true")
self.set_header("Access-Control-Allow-Headers", "x-requested-with, Content-Type, origin, authorization, accept, client-security-token")
self.set_header("Access-Control-Allow-Methods", "OPTIONS, GET, DELETE")
self.set_header("Access-Control-Max-Age", "1000")
manage_cors_headers(self)
def options(self, execution_id):
"""Needed for CORS."""
......@@ -82,22 +75,6 @@ class ExecutionDeleteAPI(RequestHandler):
"""Initializes the request handler."""
self.api_endpoint = kwargs['api_endpoint'] # type: APIEndpoint
def set_default_headers(self):
"""Set up the headers for enabling CORS."""
if self.request.headers.get('Origin') is None:
self.set_header("Access-Control-Allow-Origin", "*")
else:
self.set_header("Access-Control-Allow-Origin", self.request.headers.get('Origin'))
self.set_header("Access-Control-Allow-Credentials", "true")
self.set_header("Access-Control-Allow-Headers", "x-requested-with, Content-Type, origin, authorization, accept, client-security-token")
self.set_header("Access-Control-Allow-Methods", "OPTIONS, DELETE")
self.set_header("Access-Control-Max-Age", "1000")
def options(self, execution_id):
"""Needed for CORS."""
self.set_status(204)
self.finish()
@catch_exceptions
def delete(self, execution_id: int):
"""
......@@ -125,22 +102,6 @@ class ExecutionCollectionAPI(RequestHandler):
"""Initializes the request handler."""
self.api_endpoint = kwargs['api_endpoint'] # type: APIEndpoint
def set_default_headers(self):
"""Set up the headers for enabling CORS."""
if self.request.headers.get('Origin') is None:
self.set_header("Access-Control-Allow-Origin", "*")
else:
self.set_header("Access-Control-Allow-Origin", self.request.headers.get('Origin'))
self.set_header("Access-Control-Allow-Credentials", "true")
self.set_header("Access-Control-Allow-Headers", "x-requested-with, Content-Type, origin, authorization, accept, client-security-token")
self.set_header("Access-Control-Allow-Methods", "OPTIONS, GET, POST")
self.set_header("Access-Control-Max-Age", "1000")
def options(self):
"""Needed for CORS."""
self.set_status(204)
self.finish()
@catch_exceptions
def get(self):
"""
......
......@@ -17,7 +17,7 @@
from tornado.web import RequestHandler
from zoe_api.rest_api.utils import catch_exceptions
from zoe_api.rest_api.utils import catch_exceptions, manage_cors_headers
from zoe_api.api_endpoint import APIEndpoint # pylint: disable=unused-import
from zoe_lib.config import get_conf
......@@ -33,14 +33,7 @@ class InfoAPI(RequestHandler):
def set_default_headers(self):
"""Set up the headers for enabling CORS."""
if self.request.headers.get('Origin') is None:
self.set_header("Access-Control-Allow-Origin", "*")
else:
self.set_header("Access-Control-Allow-Origin", self.request.headers.get('Origin'))
self.set_header("Access-Control-Allow-Credentials", "true")
self.set_header("Access-Control-Allow-Headers", "x-requested-with, Content-Type, origin, authorization, accept, client-security-token")
self.set_header("Access-Control-Allow-Methods", "OPTIONS, GET")
self.set_header("Access-Control-Max-Age", "1000")
manage_cors_headers(self)
def options(self):
"""Needed for CORS."""
......
# Copyright (c) 2016, Daniele Venzano
# Copyright (c) 2016, Quang-Nhat Hoang-Xuan
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -16,7 +16,8 @@
"""The Info API endpoint."""
from tornado.web import RequestHandler
from zoe_api.rest_api.utils import get_auth, catch_exceptions
from zoe_api.rest_api.utils import get_auth, catch_exceptions, manage_cors_headers
from zoe_api.api_endpoint import APIEndpoint # pylint: disable=unused-import
class LoginAPI(RequestHandler):
......@@ -28,14 +29,7 @@ class LoginAPI(RequestHandler):
def set_default_headers(self):
"""Set up the headers for enabling CORS."""
if self.request.headers.get('Origin') is None:
self.set_header("Access-Control-Allow-Origin", "*")
else:
self.set_header("Access-Control-Allow-Origin", self.request.headers.get('Origin'))
self.set_header("Access-Control-Allow-Headers", "x-requested-with, Content-Type, origin, authorization, accept, client-security-token")
self.set_header("Access-Control-Allow-Methods", "OPTIONS, GET, DELETE")
self.set_header("Access-Control-Max-Age", "1000")
self.set_header("Access-Control-Allow-Credentials", "true")
manage_cors_headers(self)
@catch_exceptions
def options(self):
......@@ -48,9 +42,9 @@ class LoginAPI(RequestHandler):
"""HTTP GET method."""
uid, role = get_auth(self)
cookieVal = uid + '.' + role
self.set_secure_cookie('zoe', cookieVal)
cookie_val = uid + '.' + role
self.set_secure_cookie('zoe', cookie_val)
ret = {
'uid': uid,
......
......@@ -20,7 +20,7 @@ import logging
from tornado.web import RequestHandler
from zoe_api.rest_api.utils import catch_exceptions, get_auth
from zoe_api.rest_api.utils import catch_exceptions, get_auth, manage_cors_headers
from zoe_api.api_endpoint import APIEndpoint # pylint: disable=unused-import
log = logging.getLogger(__name__)
......@@ -37,14 +37,7 @@ class ServiceAPI(RequestHandler):
def set_default_headers(self):
"""Set up the headers for enabling CORS."""
if self.request.headers.get('Origin') is None:
self.set_header("Access-Control-Allow-Origin", "*")
else:
self.set_header("Access-Control-Allow-Origin", self.request.headers.get('Origin'))
self.set_header("Access-Control-Allow-Credentials", "true")
self.set_header("Access-Control-Allow-Headers", "x-requested-with, Content-Type, origin, authorization, accept, client-security-token")
self.set_header("Access-Control-Allow-Methods", "OPTIONS, GET")
self.set_header("Access-Control-Max-Age", "1000")
manage_cors_headers(self)
@catch_exceptions
def options(self, service_id):
......
......@@ -18,7 +18,7 @@
from tornado.web import RequestHandler
from zoe_api.api_endpoint import APIEndpoint # pylint: disable=unused-import
from zoe_api.rest_api.utils import catch_exceptions
from zoe_api.rest_api.utils import catch_exceptions, manage_cors_headers
class SchedulerStatsAPI(RequestHandler):
......@@ -30,14 +30,7 @@ class SchedulerStatsAPI(RequestHandler):
def set_default_headers(self):
"""Set up the headers for enabling CORS."""
if self.request.headers.get('Origin') is None:
self.set_header("Access-Control-Allow-Origin", "*")
else:
self.set_header("Access-Control-Allow-Origin", self.request.headers.get('Origin'))
self.set_header("Access-Control-Allow-Credentials", "true")
self.set_header("Access-Control-Allow-Headers", "x-requested-with, Content-Type, origin, authorization, accept, client-security-token")
self.set_header("Access-Control-Allow-Methods", "OPTIONS, GET")
self.set_header("Access-Control-Max-Age", "1000")
manage_cors_headers(self)
@catch_exceptions
def options(self):
......
# Copyright (c) 2016, Daniele Venzano
# Copyright (c) 2016, Quang-Nhat Hoang-Xuan
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -16,7 +16,8 @@
"""The Info API endpoint."""
from tornado.web import RequestHandler
from zoe_api.rest_api.utils import get_auth, catch_exceptions
from zoe_api.rest_api.utils import get_auth, catch_exceptions, manage_cors_headers
from zoe_api.api_endpoint import APIEndpoint # pylint: disable=unused-import
class UserInfoAPI(RequestHandler):
......@@ -28,21 +29,14 @@ class UserInfoAPI(RequestHandler):
def set_default_headers(self):
"""Set up the headers for enabling CORS."""
if self.request.headers.get('Origin') is None:
self.set_header("Access-Control-Allow-Origin", "*")
else:
self.set_header("Access-Control-Allow-Origin", self.request.headers.get('Origin'))
self.set_header("Access-Control-Allow-Credentials", "true")
self.set_header("Access-Control-Allow-Headers", "x-requested-with, Content-Type, origin, authorization, accept, client-security-token")
self.set_header("Access-Control-Allow-Methods", "OPTIONS, GET, DELETE")
self.set_header("Access-Control-Max-Age", "1000")
@catch_exceptions
manage_cors_headers(self)
@catch_exceptions
def options(self):
"""Needed for CORS."""
self.set_status(204)
self.finish()
@catch_exceptions
def get(self):
"""HTTP GET method."""
......
......@@ -120,3 +120,15 @@ def get_auth(handler: tornado.web.RequestHandler):
log.debug('Authentication done using auth-mechanism')
return uid, role
def manage_cors_headers(handler: tornado.web.RequestHandler):
"""Set up the headers for enabling CORS."""
if handler.request.headers.get('Origin') is None:
handler.set_header("Access-Control-Allow-Origin", "*")
else:
handler.set_header("Access-Control-Allow-Origin", handler.request.headers.get('Origin'))
handler.set_header("Access-Control-Allow-Credentials", "true")
handler.set_header("Access-Control-Allow-Headers", "x-requested-with, Content-Type, origin, authorization, accept, client-security-token")
handler.set_header("Access-Control-Allow-Methods", "OPTIONS, GET, DELETE")
handler.set_header("Access-Control-Max-Age", "1000")
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment