|
@ -1,3 +1,4 @@ |
|
|
|
|
|
import logging |
|
|
import json |
|
|
import json |
|
|
import time |
|
|
import time |
|
|
import os |
|
|
import os |
|
@ -15,6 +16,8 @@ try: |
|
|
except ImportError: |
|
|
except ImportError: |
|
|
pass |
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def jwk_from_json(json_key): |
|
|
def jwk_from_json(json_key): |
|
|
key = jwk.JWK() |
|
|
key = jwk.JWK() |
|
@ -80,15 +83,20 @@ class Main(http.Controller): |
|
|
raise OAuthException( |
|
|
raise OAuthException( |
|
|
"client_id param is invalid", OAuthException.INVALID_CLIENT, |
|
|
"client_id param is invalid", OAuthException.INVALID_CLIENT, |
|
|
) |
|
|
) |
|
|
|
|
|
_logger.debug("#### openid client: %s" % client) |
|
|
return client |
|
|
return client |
|
|
|
|
|
|
|
|
def __validate_redirect_uri(self, client, req, **query): |
|
|
def __validate_redirect_uri(self, client, req, **query): |
|
|
|
|
|
_logger.debug("#### openid client: %s" % query) |
|
|
if "redirect_uri" not in query: |
|
|
if "redirect_uri" not in query: |
|
|
raise OAuthException( |
|
|
raise OAuthException( |
|
|
"redirect_uri param is missing", OAuthException.INVALID_GRANT, |
|
|
"redirect_uri param is missing", OAuthException.INVALID_GRANT, |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
redirect_uri = query["redirect_uri"] |
|
|
redirect_uri = query["redirect_uri"] |
|
|
|
|
|
_logger.debug( |
|
|
|
|
|
"#### openid client: %s (%s)" % (client.auth_redirect_uri, redirect_uri) |
|
|
|
|
|
) |
|
|
if client.auth_redirect_uri != redirect_uri: |
|
|
if client.auth_redirect_uri != redirect_uri: |
|
|
raise OAuthException( |
|
|
raise OAuthException( |
|
|
"redirect_uri param doesn't match the pre-configured redirect URI", |
|
|
"redirect_uri param doesn't match the pre-configured redirect URI", |
|
@ -99,6 +107,9 @@ class Main(http.Controller): |
|
|
|
|
|
|
|
|
def __validate_client_secret(self, client, req, **query): |
|
|
def __validate_client_secret(self, client, req, **query): |
|
|
if "client_secret" not in query or query["client_secret"] != client.secret: |
|
|
if "client_secret" not in query or query["client_secret"] != client.secret: |
|
|
|
|
|
_logger.debug( |
|
|
|
|
|
"#### openid client: %s (%s)" % (query["client_secret"], client.secret) |
|
|
|
|
|
) |
|
|
raise OAuthException( |
|
|
raise OAuthException( |
|
|
"client_secret param is not valid", OAuthException.INVALID_CLIENT, |
|
|
"client_secret param is not valid", OAuthException.INVALID_CLIENT, |
|
|
) |
|
|
) |
|
@ -143,6 +154,7 @@ class Main(http.Controller): |
|
|
} |
|
|
} |
|
|
if user.email: |
|
|
if user.email: |
|
|
values["email"] = user.email |
|
|
values["email"] = user.email |
|
|
|
|
|
_logger.debug("#### OPENID (3): %s" % values) |
|
|
return values |
|
|
return values |
|
|
|
|
|
|
|
|
@resource("/oauth/clientinfo", method="GET", auth="client") |
|
|
@resource("/oauth/clientinfo", method="GET", auth="client") |
|
@ -155,12 +167,14 @@ class Main(http.Controller): |
|
|
@http.route("/oauth/authorize", auth="public", type="http", csrf=False) |
|
|
@http.route("/oauth/authorize", auth="public", type="http", csrf=False) |
|
|
def authorize(self, req, **query): |
|
|
def authorize(self, req, **query): |
|
|
# First, validate client_id and redirect_uri params. |
|
|
# First, validate client_id and redirect_uri params. |
|
|
|
|
|
_logger.debug("#### OPENID (auth)") |
|
|
try: |
|
|
try: |
|
|
client = self.__validate_client(req, **query) |
|
|
client = self.__validate_client(req, **query) |
|
|
redirect_uri = self.__validate_redirect_uri(client, req, **query) |
|
|
redirect_uri = self.__validate_redirect_uri(client, req, **query) |
|
|
except OAuthException as e: |
|
|
except OAuthException as e: |
|
|
# If those are not valid, we must not redirect back to the client |
|
|
# If those are not valid, we must not redirect back to the client |
|
|
# - instead, we display a message to the user |
|
|
# - instead, we display a message to the user |
|
|
|
|
|
_logger.debug("#### OPENID (4): %s" % e) |
|
|
return req.render("galicea_openid_connect.error", {"exception": e}) |
|
|
return req.render("galicea_openid_connect.error", {"exception": e}) |
|
|
|
|
|
|
|
|
scopes = query["scope"].split(" ") if query.get("scope") else [] |
|
|
scopes = query["scope"].split(" ") if query.get("scope") else [] |
|
@ -174,6 +188,7 @@ class Main(http.Controller): |
|
|
response_mode = query.get("response_mode") |
|
|
response_mode = query.get("response_mode") |
|
|
try: |
|
|
try: |
|
|
if response_mode and response_mode not in ["query", "fragment"]: |
|
|
if response_mode and response_mode not in ["query", "fragment"]: |
|
|
|
|
|
_logger.debug("#### OPENID (auth 1)") |
|
|
response_mode = None |
|
|
response_mode = None |
|
|
raise OAuthException( |
|
|
raise OAuthException( |
|
|
"The only supported response_modes are 'query' and 'fragment'", |
|
|
"The only supported response_modes are 'query' and 'fragment'", |
|
@ -181,11 +196,13 @@ class Main(http.Controller): |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
if "response_type" not in query: |
|
|
if "response_type" not in query: |
|
|
|
|
|
_logger.debug("#### OPENID (auth 2)") |
|
|
raise OAuthException( |
|
|
raise OAuthException( |
|
|
"response_type param is missing", OAuthException.INVALID_REQUEST, |
|
|
"response_type param is missing", OAuthException.INVALID_REQUEST, |
|
|
) |
|
|
) |
|
|
response_type = query["response_type"] |
|
|
response_type = query["response_type"] |
|
|
if response_type not in RESPONSE_TYPES_SUPPORTED: |
|
|
if response_type not in RESPONSE_TYPES_SUPPORTED: |
|
|
|
|
|
_logger.debug("#### OPENID (auth 3)") |
|
|
raise OAuthException( |
|
|
raise OAuthException( |
|
|
"The only supported response_types are: {}".format( |
|
|
"The only supported response_types are: {}".format( |
|
|
", ".join(RESPONSE_TYPES_SUPPORTED) |
|
|
", ".join(RESPONSE_TYPES_SUPPORTED) |
|
@ -193,12 +210,14 @@ class Main(http.Controller): |
|
|
OAuthException.UNSUPPORTED_RESPONSE_TYPE, |
|
|
OAuthException.UNSUPPORTED_RESPONSE_TYPE, |
|
|
) |
|
|
) |
|
|
except OAuthException as e: |
|
|
except OAuthException as e: |
|
|
|
|
|
_logger.debug("#### OPENID (5): %s" % e) |
|
|
response_params["error"] = e.type |
|
|
response_params["error"] = e.type |
|
|
response_params["error_description"] = e.message |
|
|
|
|
|
|
|
|
response_params["error_description"] = e |
|
|
return self.__redirect( |
|
|
return self.__redirect( |
|
|
redirect_uri, response_params, response_mode or "query" |
|
|
redirect_uri, response_params, response_mode or "query" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
_logger.debug("#### OPENID (auth 4)") |
|
|
if not response_mode: |
|
|
if not response_mode: |
|
|
response_mode = "query" if response_type == "code" else "fragment" |
|
|
response_mode = "query" if response_type == "code" else "fragment" |
|
|
|
|
|
|
|
@ -218,6 +237,7 @@ class Main(http.Controller): |
|
|
werkzeug.url_encode(query) |
|
|
werkzeug.url_encode(query) |
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
_logger.debug("#### OPENID (auth 4.2): %s" % params) |
|
|
return self.__redirect("/web/login", params, "query") |
|
|
return self.__redirect("/web/login", params, "query") |
|
|
|
|
|
|
|
|
response_types = response_type.split() |
|
|
response_types = response_type.split() |
|
@ -261,7 +281,14 @@ class Main(http.Controller): |
|
|
response_params["id_token"] = self.__create_id_token( |
|
|
response_params["id_token"] = self.__create_id_token( |
|
|
req, user.id, client, extra_claims |
|
|
req, user.id, client, extra_claims |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
_logger.debug( |
|
|
|
|
|
"#### OPENID (6): %s, %s, %s" |
|
|
|
|
|
% (redirect_uri, response_params, response_mode) |
|
|
|
|
|
) |
|
|
|
|
|
_logger.debug( |
|
|
|
|
|
"#### OPENID (6.1): %s" |
|
|
|
|
|
% self.__redirect(redirect_uri, response_params, response_mode) |
|
|
|
|
|
) |
|
|
return self.__redirect(redirect_uri, response_params, response_mode) |
|
|
return self.__redirect(redirect_uri, response_params, response_mode) |
|
|
|
|
|
|
|
|
@http.route( |
|
|
@http.route( |
|
@ -281,6 +308,7 @@ class Main(http.Controller): |
|
|
return http.Response(status=200, headers=cors_headers) |
|
|
return http.Response(status=200, headers=cors_headers) |
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
|
|
|
_logger.debug("#### OPENID (7): %s" % query) |
|
|
if "grant_type" not in query: |
|
|
if "grant_type" not in query: |
|
|
raise OAuthException( |
|
|
raise OAuthException( |
|
|
"grant_type param is missing", OAuthException.INVALID_REQUEST, |
|
|
"grant_type param is missing", OAuthException.INVALID_REQUEST, |
|
@ -306,7 +334,7 @@ class Main(http.Controller): |
|
|
OAuthException.UNSUPPORTED_GRANT_TYPE, |
|
|
OAuthException.UNSUPPORTED_GRANT_TYPE, |
|
|
) |
|
|
) |
|
|
except OAuthException as e: |
|
|
except OAuthException as e: |
|
|
body = json.dumps({"error": e.type, "error_description": e.message}) |
|
|
|
|
|
|
|
|
body = json.dumps({"error": e.type, "error_description": e}) |
|
|
return werkzeug.Response(response=body, status=400, headers=cors_headers) |
|
|
return werkzeug.Response(response=body, status=400, headers=cors_headers) |
|
|
|
|
|
|
|
|
def __handle_grant_type_authorization_code(self, req, **query): |
|
|
def __handle_grant_type_authorization_code(self, req, **query): |
|
@ -320,20 +348,25 @@ class Main(http.Controller): |
|
|
) |
|
|
) |
|
|
try: |
|
|
try: |
|
|
payload = jwt_decode(query["code"], self.__get_authorization_code_jwk(req)) |
|
|
payload = jwt_decode(query["code"], self.__get_authorization_code_jwk(req)) |
|
|
|
|
|
_logger.debug("#### OPENID (8): %s" % payload) |
|
|
except jwt.JWTExpired: |
|
|
except jwt.JWTExpired: |
|
|
|
|
|
_logger.debug("#### OPENID (9): %s" % OAuthException.INVALID_GRANT) |
|
|
raise OAuthException( |
|
|
raise OAuthException( |
|
|
"Code expired", OAuthException.INVALID_GRANT, |
|
|
"Code expired", OAuthException.INVALID_GRANT, |
|
|
) |
|
|
) |
|
|
except ValueError: |
|
|
except ValueError: |
|
|
|
|
|
_logger.debug("#### OPENID (10): %s" % OAuthException.INVALID_GRANT) |
|
|
raise OAuthException( |
|
|
raise OAuthException( |
|
|
"code malformed", OAuthException.INVALID_GRANT, |
|
|
"code malformed", OAuthException.INVALID_GRANT, |
|
|
) |
|
|
) |
|
|
if payload["client_id"] != client.client_id: |
|
|
if payload["client_id"] != client.client_id: |
|
|
|
|
|
_logger.debug("#### OPENID (11): %s" % OAuthException.INVALID_GRANT) |
|
|
raise OAuthException( |
|
|
raise OAuthException( |
|
|
"client_id doesn't match the authorization request", |
|
|
"client_id doesn't match the authorization request", |
|
|
OAuthException.INVALID_GRANT, |
|
|
OAuthException.INVALID_GRANT, |
|
|
) |
|
|
) |
|
|
if payload["redirect_uri"] != redirect_uri: |
|
|
if payload["redirect_uri"] != redirect_uri: |
|
|
|
|
|
_logger.debug("#### OPENID (12): %s" % OAuthException.INVALID_GRANT) |
|
|
raise OAuthException( |
|
|
raise OAuthException( |
|
|
"redirect_uri doesn't match the authorization request", |
|
|
"redirect_uri doesn't match the authorization request", |
|
|
OAuthException.INVALID_GRANT, |
|
|
OAuthException.INVALID_GRANT, |
|
@ -353,6 +386,7 @@ class Main(http.Controller): |
|
|
response["id_token"] = self.__create_id_token( |
|
|
response["id_token"] = self.__create_id_token( |
|
|
req, payload["user_id"], client, extra_claims |
|
|
req, payload["user_id"], client, extra_claims |
|
|
) |
|
|
) |
|
|
|
|
|
_logger.debug("#### OPENID (12): %s" % response) |
|
|
return response |
|
|
return response |
|
|
|
|
|
|
|
|
def __handle_grant_type_password(self, req, **query): |
|
|
def __handle_grant_type_password(self, req, **query): |
|
|