Hi, I have a React app that built using your sample code that talks to a secure api running express and the React app returns a access_token but it’s too short, e.g.:
It might be possible that your access_token is not a JWT instead it is a string.
You can ask for a JWT token by setting appropriate audience.
Here are some helpful links -
There are two sections to look into in this flow, the SPA and the API. When you make a request to the /authorize endpoint and you use an audience like https://YOUR_DOMAIN.auth0.com/userinfo with a response_type='token id_token' you’re asking Auth0 to provide you with an access_token that will have the /userinfo audience and with an id_token that has some information about the user.
When you use YOUR_DOMAIN.auth0.com/userinfo as an audience parameter, you get a short access token (like the one you received) that is not designed to call your own API, as @priya.sharma.9362 said. It’s only meant to be used to call Auth0’s userinfo endpoint. If you want an access_token that can be verified and consumed by your API you need to do the following:
Create an API in Auth0 as explained in this document. The API identifier will be audience that we will use later. It’s highly recommended that you use RS256 as a signing algorithm
Use the identifier of the API you created as the audience parameter in the request to /authorize either manually or through auth0.WebAuth({}). Use response_type='token id_token to get both an access and an id token back.
Verify that you get back an access_token with the proper audience. You can use jwt.io to inspect it.
Make a request to your API with this access_token as part of the Authorization header ( Authorization: Bearer <access_token> )
Verify your access_token in your API. This process is explained in this document in greater detail. In particular, you should verify the audience ( aud claim), the issuer ( iss ), the algorithm should be RS256 and you should use https://YOUR_DOMAIN.auth0.com/.well-known/jwks.json to get the proper keys. You may also need to check the scopes depending on your use case.
This is helpful @anny thanks, but I still need help.
I have a single page app, I do this:
auth0.WebAuth.authorize(…)
I receive a short access_token and a token_id, like you said.
I pass access_token to webAuth.client.userInfo to get the userId.
Now I want my backend (express/mongodb) to operate on this userId.
For example, my backend has an /api/createProject endpoint, I want a user to request. Am I supposed to send the userId and short access_token with this request to my backend? If yes, how will my backend validate that the short access_token is valid and handle the request?
@anny So, if someone has the acces token that’s a string (not a JWT), here are some simple yes/no questions, supposing when does a call to /authorize and an audience https://YOUR_DOMAIN.auth0.com/userinfo (and default response types), and using RS256:
one can a call the /userinfo endpoint?
that call returns a result if the access token is set as Bearer in the Authorization header
if the call returns the user info, can I assume that it means that the user is authenticated properly? (So, instead of validating a JWT, on could do a call to /userinfo to validate the access token and get some claims about the user (like email etc)?)
@amine.foo If you’d like to authorize APIs with access tokens, you have to create an API in Auth0 and request this API’s access with the audience parameter when you’re authenticating. This will give you a JWT access token that you can send to the API, and the API can validate the JWT.
I won’t go into more detail, but there are a lot of docs that explain the process:
If you come across any specific problems, feel free to create a new discussion.
@kurt.sys It’s yes for all the questions. If you use Auth0.js, all these (validating ID token, calling /userinfo, etc) will be done automatically for you.
If I add ‘audience=’ to the URL, then it no longer prompts for login, and redirects to the URL without the #… Token at all WTF?
I’m attempting to switch an app that uses ADFS over to Auth0 for the Dev/QA environments (and maybe later just in general), so I’m using the Angular OAuthService, and not any Auth0 objects. To add the Audience I simply added:
And now nothing works at all, before it was returning the access_token as garbage (not a JWT at all), and the id_token as what I’m looking for (except the oAuthService is only looking at the Access_Token and injects that into further requests and gets “Invalid Token”) . It doesn’t seem to matter how I configure Auth0 for Single page app, or for normal web app, nothing seems to work.
Any idea why it does something crazy if you specify the Audience?
I’d ask customer support, but they aren’t interested in helping people become paying customers, only helping people who already are.
Also, if I could get OAuthService to just use the Id_Token and inject that (it is requesting an Id_Token, but not using it) I could do that, because ADFS returns both tokens correctly, and I could use either one of them. However, OAuthService doesn’t let you set the token (makes sense, kind of invalidates the security if you can just set it’s AccessToken = IdToken)… Would be happy with that solution too… I was looking for a way to just have Auth0 return the AccessToken. (I’ve not setup any APIs on Auth0 - maybe that is what I need to do?)
OMFG!! - I changed it to ‘Audience’ and it started prompting for login, is it case sensitive? However, it still gives a short access token that is useless, so any other ideas people?
If you want access token as JWT then you must have to set audience.
Set APIs https://<your.auth0.com>/api/v2/ in audience as given above example. Userinfo endpoint not working for me.
In the URL / is required at last. https://<your.auth0.com>/api/v2/
Node side JWT verification
const authCheck = jwt({
secret: jwks.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: 'https://<your.auth0.com>/.well-known/jwks.json'
}),
// This is the identifier we set when we created the API
audience: 'https://<your.auth0.com>/api/v2/',
issuer: 'https://<your.auth0.com>/',
algorithms: ['RS256']
});
In the URL / is required at last. https://<your.auth0.com>/api/v2/ and https://<your.auth0.com>/