Skip to content

Commit

Permalink
fix: fixed indentation exception in core-api
Browse files Browse the repository at this point in the history
  • Loading branch information
ful1e5 committed Nov 21, 2023
1 parent 1025a84 commit fb67632
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
26 changes: 10 additions & 16 deletions core/utils/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def as_token(data) -> Union[None, AuthToken]:


def decode_token(token: str, logger: Union[Logger, None] = None):
def log_error(e):
logger.error(e) if logger else None

try:
payload = jwt.decode(token, SECRET, algorithms=["HS256"], leeway=10)
auth = as_token(payload)
Expand All @@ -54,26 +57,20 @@ def decode_token(token: str, logger: Union[Logger, None] = None):
else:
return "invalid"
except jwt.ExpiredSignatureError as e:
if logger:
logger.error(
f"Expired Token: {e}\n token:{token}",
)
log_error(e)
return "expired"
except jwt.InvalidTokenError as e:
if logger:
logger.error(
f"Inavlid Token: {e}\n token:{token}",
)
log_error(e)
return "invalid"
except Exception as e:
if logger:
logger.error(
f"Exception on decode: {e}\n token:{token}",
)
log_error(e)
return "invalid"


def decode_auth_header(logger: Union[Logger, None] = None):
def log_error(e):
logger.error(e) if logger else None

unauth = jsonify({"status": 401, "error": ["Unauthorized"]})
invalid = jsonify({"status": 401, "error": ["Invalid Token"]})
expired = jsonify({"status": 401, "error": ["Expired Token"]})
Expand All @@ -91,10 +88,7 @@ def decode_auth_header(logger: Union[Logger, None] = None):
else:
return auth
except Exception as e:
if logger:
logger.error(
f"Exception on parsing: {e}\n token:{token}",
)
log_error(f"Exception on parsing: {e}\n token:{token}")
return internal_error, 500

else:
Expand Down
6 changes: 3 additions & 3 deletions src/utils/auth/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { JWTToken } from 'bibata/misc';
const SECRET_KEY = process.env.NEXT_PUBLIC_JWT_SECRET;
export const genAccessToken = (user?: User) => {
const token_id = v4();

let payload = { token_id, role: 'ANONYMOUS' };
if (user) payload = { token_id, ...user };
const iat = Math.floor(Date.now() / 1000);
let payload = { iat, token_id, role: 'ANONYMOUS' };
if (user) payload = { iat, token_id, ...user };

const token = jwt.sign(payload, process.env.NEXT_PUBLIC_JWT_SECRET, {
algorithm: 'HS256'
Expand Down

0 comments on commit fb67632

Please sign in to comment.