Skip to content

Commit

Permalink
backends(ping): coding style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Sep 12, 2023
1 parent 6f3c59f commit 6a2734e
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions social_core/backends/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,29 @@
Ping Auth OpenID Connect backend
"""

from os import stat
from social_core.backends.open_id_connect import OpenIdConnectAuth

from jose import jwk, jwt
from jose.jwt import JWTError, JWTClaimsError, ExpiredSignatureError
from jose.jwt import ExpiredSignatureError, JWTClaimsError, JWTError
from jose.utils import base64url_decode

from social_core.backends.open_id_connect import OpenIdConnectAuth
from social_core.exceptions import AuthTokenError


class PingOpenIdConnect(OpenIdConnectAuth):
name = 'ping'
name = "ping"
# OIDC_ENDPOINT has the form 'https://auth.pingone.com/<APP ID>/as'
OIDC_ENDPOINT = ''
OIDC_ENDPOINT = ""
REDIRECT_STATE = False
ACCESS_TOKEN_METHOD = 'POST'
RESPONSE_TYPE = 'code'
USERNAME_KEY = 'preferred_username'
ACCESS_TOKEN_METHOD = "POST"
RESPONSE_TYPE = "code"
USERNAME_KEY = "preferred_username"

def find_valid_key(self, id_token):
for key in self.get_jwks_keys():
rsakey = jwk.construct(key, algorithm='RS256')
message, encoded_sig = id_token.rsplit('.', 1)
decoded_sig = base64url_decode(encoded_sig.encode('utf-8'))
if rsakey.verify(message.encode('utf-8'), decoded_sig):
rsakey = jwk.construct(key, algorithm="RS256")
message, encoded_sig = id_token.rsplit(".", 1)
decoded_sig = base64url_decode(encoded_sig.encode("utf-8"))
if rsakey.verify(message.encode("utf-8"), decoded_sig):
return key

def validate_and_return_id_token(self, id_token, access_token):
Expand All @@ -39,38 +37,40 @@ def validate_and_return_id_token(self, id_token, access_token):
key = self.find_valid_key(id_token)

if not key:
raise AuthTokenError(self, 'Signature verification failed')
raise AuthTokenError(self, "Signature verification failed")

rsakey = jwk.construct(key, algorithm='RS256')
rsakey = jwk.construct(key, algorithm="RS256")

try:
claims = jwt.decode(
id_token,
rsakey.to_pem().decode('utf-8'),
rsakey.to_pem().decode("utf-8"),
algorithms=self.JWT_ALGORITHMS,
audience=client_id,
issuer=self.id_token_issuer(),
access_token=access_token,
options=self.JWT_DECODE_OPTIONS,
)
except ExpiredSignatureError:
raise AuthTokenError(self, 'Signature has expired')
raise AuthTokenError(self, "Signature has expired")
except JWTClaimsError as error:
raise AuthTokenError(self, str(error))
except JWTError:
raise AuthTokenError(self, 'Invalid signature')
raise AuthTokenError(self, "Invalid signature")

self.validate_claims(claims)

return claims

def get_user_details(self, response):
username_key = self.setting('USERNAME_KEY', default=self.USERNAME_KEY)
username_key = self.setting("USERNAME_KEY", default=self.USERNAME_KEY)
fullname, first_name, last_name = self.get_user_names(
first_name=response.get('given_name'),
last_name=response.get('family_name'))
return {'username': response.get(username_key),
'email': response.get('email'),
'fullname': fullname,
'first_name': first_name,
'last_name': last_name}
first_name=response.get("given_name"), last_name=response.get("family_name")
)
return {
"username": response.get(username_key),
"email": response.get("email"),
"fullname": fullname,
"first_name": first_name,
"last_name": last_name,
}

0 comments on commit 6a2734e

Please sign in to comment.