I am learning about Token-based authentication with JSON Web Tokens and here is how I see it now for a mobile app, built with, e.g. Swift:
I can create an object inside the app using user input, like
{ username: "patrickbateman", password: "ismyknifesharp", role: "regular", ... }
Then I can generate a JWT token from it with a library.
/api/contacts/list
. Or do I have to send login/password as they are to authenticate?Here are my conclusions:
Are these conclusions correct? What's the way to check token that client sends?
My opinions:
We should not keep password of user on client. Client should post password to server when sign up/ sign in, and don't save it anywhere in client. Request should be https
, and password should not be encrypted. We will encrypt password later at server side.
Server will generate a token
for this user after user login successfully. The token
will contain the expired date in itself. We will use the token to authenticate permission with server.
I think every request to API should provide the token, except the sign up / sign in/ forgot password requests.
Token should be put inside the header of request.
Server should allow client request a new token with the old token (maybe be expired)
And the answer for "How to server check the token from client?". There are many ways to do that. The way below is my current approach:
Server side generate a token, which is a encrypted string of a user info
(such as token expired time, userid, role... of user) and a password (keep only on server side) with HMAC or RSA algorithms. When user submit a token
, server can decrypt and get the user info
, expired time without querying from database.
Anyway, this question does not relate with Swift
tag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With