# Should I be validating JWT on my back-end with or without secret?

**URL:** <https://community.auth0.com/t/should-i-be-validating-jwt-on-my-back-end-with-or-without-secret/107021>\
**Category:** Get Help\
**Tags:** login-experience, new-universal-login-experience\
**Created:** [May 16, 2023, 11:10am UTC](https://community.auth0.com/t/should-i-be-validating-jwt-on-my-back-end-with-or-without-secret/107021 "2023-05-16T11:10:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kkrp1](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@kkrp1](https://community.auth0.com/u/kkrp1)\
**Post date:** [May 16, 2023, 11:10am UTC](https://community.auth0.com/t/should-i-be-validating-jwt-on-my-back-end-with-or-without-secret/107021/1 "2023-05-16T11:10:29Z")

</div>

I’m using the `express-oauth2-jwt-bearer` Node.js package on my back end to validate an Auth0 JWT passed to it from the (separate, SPA) front-end, like so:

```auto
const { auth } = require('express-oauth2-jwt-bearer');
auth({
	audience: process.env.AUTH0_API_AUDIENCE,
	issuerBaseURL: process.env.AUTH0_API_DOMAIN
});

```

This is how the package’s docs suggest you do it. However, the same docs then give a further example, for tokens signed with " symmetric algorithms", where a _secret_ is used in the validation, like so:

```auto
  auth({
    issuer: 'https://YOUR_ISSUER_DOMAIN',
    audience: 'https://my-api.com',
    secret: 'YOUR SECRET',
    tokenSigningAlg: 'HS256',
  })

```

So do I need the first approach, which doesn’t use secret, or the second, which does? I’m quite new to JWT. I just want to know that I’m doing this securely.

Thank you!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [May 17, 2023, 12:30am UTC](https://community.auth0.com/t/should-i-be-validating-jwt-on-my-back-end-with-or-without-secret/107021/3 "2023-05-17T00:30:56Z")

</div>

Hey @kkrp1 !

> [@kkrp1](#):
>
> So do I need the first approach, which doesn’t use secret, or the second, which does? I’m quite new to JWT. I just want to know that I’m doing this securely.

Great question! It depends on which [algorithm you’re using to sign tokens](https://auth0.com/docs/get-started/applications/signing-algorithms) (you can check in your application settings → advanced → oauth tab as well as in your API settings)- _Typically_ this will be RS256 which is asymmetric, won’t include using a secret, and is generally considered more secure. HS256 is symmetric and requires a secret. Some further resources on this topic:

[https://community.auth0.com/t/rs256-vs-hs256-jwt-signing-algorithms/58609](https://community.auth0.com/t/rs256-vs-hs256-jwt-signing-algorithms/58609)

> **[RS256 vs HS256 What's the difference? | Auth0](https://auth0.com/blog/rs256-vs-hs256-whats-the-difference/)**
>
> Learn the difference between RS256 and HS256 JWT signing algorithms and which one is recommended to use in applications.

Hope this helps to clarify!

---

<div class="post-metadata">

**Author:** ![kkrp1](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@kkrp1](https://community.auth0.com/u/kkrp1)\
**Post date:** [May 17, 2023, 4:43pm UTC](https://community.auth0.com/t/should-i-be-validating-jwt-on-my-back-end-with-or-without-secret/107021/4 "2023-05-17T16:43:13Z")

</div>

Aha yes it is set to RS256 (actually, it won’t let me change this, even if I wanted to) so I’m all good and secure, then, by the sounds of it.

I’m still getting my head around JWTs! Trying to understand how it can be secure, if secrets aren’t involved. Just to double-double-check, this is what I’m doing:

Front-end, using JS SDK:

```auto
const auth0Client = await createAuth0Client({
	domain: '<my-auth0-domain>',
	clientId: '<my-auth0-client>',
	... //bunch of other config here
})

```

Back-end, to validate the JWT passed to it by the front-end:

```auto
const { auth } = require('express-oauth2-jwt-bearer');
module.exports.validateJwt = auth({
	audience: '<auth0-api-identifier>',
	issuerBaseURL: '<auth0-domain>'
});

```

And everything works, I’m just a little alarmed at how there’s no secrets involved! But everything I read (not least on this forum) suggests I’m on the right lines?

Thanks for the continued help!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [May 17, 2023, 6:31pm UTC](https://community.auth0.com/t/should-i-be-validating-jwt-on-my-back-end-with-or-without-secret/107021/5 "2023-05-17T18:31:03Z")

</div>

Hey @kkrp1 thanks for following up here and happy to help!

Everything looks good to me! Aside from validating the tokens, the `express-oauth2-jwt-bearer` library provides some helpful functions for more granular authorization as outlined here:

> **[node-oauth2-jwt-bearer/packages/express-oauth2-jwt-bearer at main ·... - API Reference](https://github.com/auth0/node-oauth2-jwt-bearer/tree/main/packages/express-oauth2-jwt-bearer#api-reference)**
>
> main/packages/express-oauth2-jwt-bearer

Cheers!

---

<div class="post-metadata">

**Author:** ![kkrp1](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@kkrp1](https://community.auth0.com/u/kkrp1)\
**Post date:** [May 19, 2023, 10:30am UTC](https://community.auth0.com/t/should-i-be-validating-jwt-on-my-back-end-with-or-without-secret/107021/6 "2023-05-19T10:30:26Z")

</div>

Thanks for this - I just had a read of it. I notice there’s a method for checking scope. Would you mind briefly explaining how this might work with/relate to Auth0? Right now I’m merely validating the JWT, as shown, and I’m handling all permissions app-side i.e. I have a custom, proprietary permissions system that I’m managing outside of Auth0. With this in mind, would I be right in thinking I don’t really need to get involved in Auth0-side scopes and roles (not currently sure of the difference)?

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [May 19, 2023, 11:34pm UTC](https://community.auth0.com/t/should-i-be-validating-jwt-on-my-back-end-with-or-without-secret/107021/7 "2023-05-19T23:34:35Z")

</div>

Hey @kkrp1 no problem!

> [@kkrp1](#):
>
> With this in mind, would I be right in thinking I don’t really need to get involved in Auth0-side scopes and roles (not currently sure of the difference)?

If you’re already handling this on your own then you probably don’t need to worry about the other functions provided for checking scopes, claims, etc. They exist for more granular authorization beyond just checking to make sure the access token is valid - For example, you might check to make sure a user has a specific scope, claim, role, etc. before they are able to access a given endpoint. Some examples here:

> <https://github.com/auth0/node-oauth2-jwt-bearer/blob/main/packages/express-oauth2-jwt-bearer/EXAMPLES.md>

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [June 2, 2023, 11:35pm UTC](https://community.auth0.com/t/should-i-be-validating-jwt-on-my-back-end-with-or-without-secret/107021/8 "2023-06-02T23:35:34Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
