참고소스 수정본
This commit is contained in:
51
참고/guardrails-main/guardrails/hub_token/token.py
Normal file
51
참고/guardrails-main/guardrails/hub_token/token.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import os
|
||||
import jwt
|
||||
from jwt import ExpiredSignatureError, DecodeError
|
||||
from typing import Optional
|
||||
|
||||
from guardrails.classes.rc import RC
|
||||
|
||||
FIND_NEW_TOKEN = "You can find a new token at https://guardrailsai.com/hub/keys"
|
||||
|
||||
TOKEN_EXPIRED_MESSAGE = f"""Your token has expired. Please run `guardrails configure`\
|
||||
to update your token.
|
||||
{FIND_NEW_TOKEN}"""
|
||||
TOKEN_INVALID_MESSAGE = f"""Your token is invalid. Please run `guardrails configure`\
|
||||
to update your token.
|
||||
{FIND_NEW_TOKEN}"""
|
||||
|
||||
|
||||
class AuthenticationError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ExpiredTokenError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidTokenError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class HttpError(Exception):
|
||||
status: int
|
||||
message: str
|
||||
|
||||
|
||||
VALIDATOR_HUB_SERVICE = os.getenv(
|
||||
"GR_VALIDATOR_HUB_SERVICE", "https://hub.api.guardrailsai.com"
|
||||
)
|
||||
|
||||
|
||||
def get_jwt_token(rc: RC) -> Optional[str]:
|
||||
token = rc.token
|
||||
|
||||
# check for jwt expiration
|
||||
if token:
|
||||
try:
|
||||
jwt.decode(token, options={"verify_signature": False, "verify_exp": True})
|
||||
except ExpiredSignatureError:
|
||||
raise ExpiredTokenError(TOKEN_EXPIRED_MESSAGE)
|
||||
except DecodeError:
|
||||
raise InvalidTokenError(TOKEN_INVALID_MESSAGE)
|
||||
return token
|
||||
Reference in New Issue
Block a user