Skip to content

Commit

Permalink
refactor: move get_jkws method in oauth.py
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Sep 18, 2023
1 parent a2fb88f commit d7c6896
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
21 changes: 2 additions & 19 deletions canaille/oidc/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import uuid

from authlib.integrations.flask_oauth2 import current_token
from authlib.jose import JsonWebKey
from authlib.jose import jwt
from authlib.oauth2 import OAuth2Error
from canaille import csrf
Expand All @@ -28,10 +27,9 @@
from .oauth import authorization
from .oauth import ClientConfigurationEndpoint
from .oauth import ClientRegistrationEndpoint
from .oauth import DEFAULT_JWT_ALG
from .oauth import DEFAULT_JWT_KTY
from .oauth import generate_user_info
from .oauth import get_issuer
from .oauth import get_jwks
from .oauth import IntrospectionEndpoint
from .oauth import require_oauth
from .oauth import RevocationEndpoint
Expand Down Expand Up @@ -211,22 +209,7 @@ def client_registration_management(client_id):

@bp.route("/jwks.json")
def jwks():
kty = current_app.config["OIDC"]["JWT"].get("KTY", DEFAULT_JWT_KTY)
alg = current_app.config["OIDC"]["JWT"].get("ALG", DEFAULT_JWT_ALG)
jwk = JsonWebKey.import_key(
current_app.config["OIDC"]["JWT"]["PUBLIC_KEY"], {"kty": kty}
)
return jsonify(
{
"keys": [
{
"use": "sig",
"alg": alg,
**jwk,
}
]
}
)
return jsonify(get_jwks())


@bp.route("/userinfo")
Expand Down
20 changes: 19 additions & 1 deletion canaille/oidc/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from authlib.integrations.flask_oauth2 import AuthorizationServer
from authlib.integrations.flask_oauth2 import ResourceProtector
from authlib.jose import JsonWebKey
from authlib.oauth2.rfc6749.grants import (
AuthorizationCodeGrant as _AuthorizationCodeGrant,
)
Expand Down Expand Up @@ -66,7 +67,7 @@ def get_issuer():
return request.url_root


def get_jwt_config(grant):
def get_jwt_config(grant=None):
return {
"key": current_app.config["OIDC"]["JWT"]["PRIVATE_KEY"],
"alg": current_app.config["OIDC"]["JWT"].get("ALG", DEFAULT_JWT_ALG),
Expand All @@ -75,6 +76,23 @@ def get_jwt_config(grant):
}


def get_jwks():
kty = current_app.config["OIDC"]["JWT"].get("KTY", DEFAULT_JWT_KTY)
alg = current_app.config["OIDC"]["JWT"].get("ALG", DEFAULT_JWT_ALG)
jwk = JsonWebKey.import_key(
current_app.config["OIDC"]["JWT"]["PUBLIC_KEY"], {"kty": kty}
)
return {
"keys": [
{
"use": "sig",
"alg": alg,
**jwk,
}
]
}


def claims_from_scope(scope):
claims = {"sub"}
if "profile" in scope:
Expand Down

0 comments on commit d7c6896

Please sign in to comment.