0

I've come across this block of code where the model script is handling token verification:

def encode_auth_token(self, user_id): try: payload = { 'exp': datetime.datetime.utcnow() + datetime.timedelta( days=current_app.config.get('TOKEN_EXPIRATION_DAYS'), \ seconds=current_app.config.get('TOKEN_EXPIRATION_SECONDS')), 'iat': datetime.datetime.utcnow(), 'sub': user_id } return jwt.encode(payload, current_app.config.get('SECRET_KEY'), algorithm='HS256') except Exception as e: return e @staticmethod def decode_auth_token(token): try: return jwt.decode(token, current_app.config.get('SECRET_KEY')) except jwt.ExpiredSignatureError: return 'Signature expired. Please log in again.' except jwt.InvalidTokenError: return 'Invalid token. Please log in again.' 

My question to this block of code is why decode_auth_token method needs to be a static method whereas encode_auth_token doesn't need to be ?

8
  • 2
    Does this answer your question? What is the advantage of using static methods in Python? Commented Jul 24, 2020 at 9:17
  • Neither of these need to be either. Honestly, you'd have to ask whoever wrote the code. But, really all you can say is because that is what whoever designed the API of that class decided it to be Commented Jul 24, 2020 at 9:18
  • 2
    Both can be a staticmethod since they do not use self. Commented Jul 24, 2020 at 9:19
  • Actually, both of them could be static since they don't use self. At the same time, there's nothing wrong to make them both plain instance methods. Commented Jul 24, 2020 at 9:20
  • @KlausD. the encode_auth_token method does. It needs access to model's ID attribute. Commented Jul 24, 2020 at 9:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.