Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JWT - is it fine to authenticate user with the subject being their email?

I'm new to authentication, and just trying out JWT authentication on a small express app.

I've got a user authentication setup using JWTs, and I'm using the subject as the user's email.

Is this a good practice?

If I decode the JWT on jwt.io, I see:

{
  "sub": "[email protected]",
  "iat": 1489963760,
  "exp": 1490568560
}

Is that how it is supposed to work?

like image 509
user1354934 Avatar asked Mar 19 '17 22:03

user1354934


People also ask

Can I use JWT for email verification?

1. Generate a unique code specific to user and save it in your database corresponding to that user. Send the code in the email link sent to user. When the email is clicked, recognise the user based on the code in link and mark it as verified.

What should be the subject in JWT?

The “sub” (subject) claim identifies the principal that is the subject of the JWT. The claims in a JWT are normally statements about the subject. The subject value MUST either be scoped to be locally unique in the context of the issuer or be globally unique.

When should you not use JWT?

Although JWT does eliminate the database lookup, it introduces security issues and other complexities while doing so. Security is binary—either it's secure or it's not. Thus making it dangerous to use JWT for user sessions.

How do I authenticate a user with JWT?

To authenticate a user, a client application must send a JSON Web Token (JWT) in the authorization header of the HTTP request to your backend API. API Gateway validates the token on behalf of your API, so you don't have to add any code in your API to process the authentication.


1 Answers

The sub claim must be unique. Since email addresses are unique, it is a reasonable choice for the claim.

See RFC7519

4.1.2. "sub" (Subject) Claim

The "sub" (subject) claim identifies the principal that is the subject of the JWT. The claims in a JWT are normally statements about the subject. The subject value MUST either be scoped to be locally unique in the context of the issuer or be globally unique. The processing of this claim is generally application specific.

Ensure two users do not register theirselves with the same email address, for example using a generic email like [email protected]

like image 73
pedrofb Avatar answered Nov 15 '22 23:11

pedrofb