Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

python - How do I know when to ask for refreshed JWT tokens served from an outside source (Signal Wire in this case)

To begin with, please understand that I have little formal training in Front Stack development. I've had to learn a lot on the job, and our only experienced developer left about a year ago, so I very well might not know something I should.

My current cunumdrum is that I don't know how to check when a JWT token I have has expired, that I did not create. How would I know? Since I don't know the secret used to generate it. I could possibly just record when I made the request for the token(s), and how long I made the timeout, but I wonder if there will be a disconnect between when I made the request and time the token acttualy expires.

I ask, because I don't want to randomly call to refresh any of these tokens. Only when they expire.

If it helps, we are using Python 3.6 in the backend and making a request to Signal Wire for the tokens

Thanks in advance


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A JWT token usually has three parts, each of them being base64-encoded. The payload part usually holds an expiration date as seconds since 1970-01-01.

This could be a token:

eyJhbGciOiAiSFMyNTYiLCJ0eXAiOiAiSldUIn0.eyJleHAiOiAxNTM2MzYxNDExLCJ1c2VybmFtZSI6ICJBYmUgTGluY29sbiIsInByZWZlcnJlZF91c2VybmFtZSI6ICJBYmVMaUBtaWNyb3NvZnQuY29tIn0.c2Rmc2FkZmFzZmRzYWZkYXNmZA

Each of the parts (separated by a dot) can be base64-decoded (as https://jwt.io/ does):

  1. Header - usually algorithm and token type, e.g.:
    {
      "alg": "HS256",
      "typ": "JWT"
    }
  1. Payload - whatever content the token has, usually some information on the user ID and the expiration date of the token, e.g:
    {
      "exp": 1536361411,
      "username": "Abe Lincoln",
      "preferred_username": "[email protected]"
    }
  1. Signature: some binary value

A token usually even gives you enough information to retrieve the signing key. See the answer on How does JWT.io already know my public key? for more details or dig into the OpenID Connect Discovery Protocol for full understanding.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...