Skip to content

Commit

Permalink
Merge pull request #187 from lalithkota/17.0-develop
Browse files Browse the repository at this point in the history
Auth OIDC: Added PKCE enable flag.
  • Loading branch information
shibu-narayanan authored Oct 4, 2024
2 parents afcea3a + 2d62edf commit fa2af53
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
28 changes: 15 additions & 13 deletions g2p_auth_oidc/models/auth_oauth_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class AuthOauthProvider(models.Model):
client_secret = fields.Char()
client_private_key = fields.Binary(attachment=False)

enable_pkce = fields.Boolean(default=True)
code_verifier = fields.Char("PKCE Code Verifier", default=lambda self: secrets.token_urlsafe(32))

token_map = fields.Char(
Expand Down Expand Up @@ -142,9 +143,10 @@ def _oidc_get_tokens_auth_code_flow(self, params, oidc_redirect_uri=None):
client_id=self.client_id,
grant_type="authorization_code",
code=code,
code_verifier=self.code_verifier,
redirect_uri=oidc_redirect_uri,
)
if self.enable_pkce:
token_request_data["code_verifier"] = self.code_verifier
response = requests.post(self.token_endpoint, data=token_request_data, timeout=10)
response.raise_for_status()
response_json = response.json()
Expand All @@ -155,9 +157,10 @@ def _oidc_get_tokens_auth_code_flow(self, params, oidc_redirect_uri=None):
client_id=self.client_id,
grant_type="authorization_code",
code=code,
code_verifier=self.code_verifier,
redirect_uri=oidc_redirect_uri,
)
if self.enable_pkce:
token_request_data["code_verifier"] = self.code_verifier
response = requests.post(
self.token_endpoint,
auth=token_request_auth,
Expand All @@ -173,9 +176,10 @@ def _oidc_get_tokens_auth_code_flow(self, params, oidc_redirect_uri=None):
client_secret=self.client_secret,
grant_type="authorization_code",
code=code,
code_verifier=self.code_verifier,
redirect_uri=oidc_redirect_uri,
)
if self.enable_pkce:
token_request_data["code_verifier"] = self.code_verifier
response = requests.post(self.token_endpoint, data=token_request_data, timeout=10)
response.raise_for_status()
response_json = response.json()
Expand All @@ -188,9 +192,10 @@ def _oidc_get_tokens_auth_code_flow(self, params, oidc_redirect_uri=None):
client_assertion=private_key_jwt,
grant_type="authorization_code",
code=code,
code_verifier=self.code_verifier,
redirect_uri=oidc_redirect_uri,
)
if self.enable_pkce:
token_request_data["code_verifier"] = self.code_verifier
response = requests.post(self.token_endpoint, data=token_request_data, timeout=10)
response.raise_for_status()
response_json = response.json()
Expand Down Expand Up @@ -486,15 +491,12 @@ def list_providers(
)
flow = provider.get("flow")
if flow and flow.startswith("oidc"):
params.update(
dict(
nonce=secrets.token_urlsafe(),
code_challenge=base64.urlsafe_b64encode(
hashlib.sha256(provider["code_verifier"].encode("ascii")).digest()
).rstrip(b"="),
code_challenge_method="S256",
)
)
params["nonce"] = secrets.token_urlsafe()
if provider.get("enable_pkce"):
params["code_challenge"] = base64.urlsafe_b64encode(
hashlib.sha256(provider["code_verifier"].encode("ascii")).digest()
).rstrip(b"=")
params["code_challenge_method"] = "S256"
extra_auth_params = json.loads(provider.get("extra_authorize_params") or "{}")
params.update(extra_auth_params)
provider["auth_link"] = f"{provider['auth_endpoint']}?{url_encode(params)}"
Expand Down
1 change: 1 addition & 0 deletions g2p_auth_oidc/views/auth_oauth_provider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
</field>
<field name="data_endpoint" position="after">
<field name="extra_authorize_params" />
<field name="enable_pkce" />
<field name="verify_at_hash" />
<field name="date_format" />
<field name="allow_signup" />
Expand Down
2 changes: 1 addition & 1 deletion g2p_openid_vci/data/default_issuer_metadata.jq
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
"display": [
{
"name": "OpenG2P Registry Credential",
"name": .name,
"locale": "en",
"logo": {
"url": (.web_base_url + "/g2p_openid_vci/static/description/icon.png"),
Expand Down

0 comments on commit fa2af53

Please sign in to comment.