angular2-jwt icon indicating copy to clipboard operation
angular2-jwt copied to clipboard

Error: JWT must have 3 parts

Open kotmatpockuh opened this issue 9 years ago • 12 comments

Any ideas how to use token without 3-part key, splitted by dots?

Now i get error: JWT must have 3 parts How can i use such tokens (for example): 833ec1ca1a47b128634a4744343a6215

kotmatpockuh avatar Apr 17 '17 11:04 kotmatpockuh

Your token must be in the format of JWT. You are probably using incorrect token format. Check this out: https://jwt.io/

PavelPikat avatar Apr 20 '17 21:04 PavelPikat

this does happen when you try to send the AccessToken from Auth0 as a bearer authentication header. Does this mean we can't use this library to send Auth0 access tokens as bearer? cfr https://auth0.com/docs/api-auth/why-use-access-tokens-to-secure-apis

DavidDecraene avatar Apr 26 '17 09:04 DavidDecraene

@DavidDecraene From the article you referenced:

The id_token is a JWT and is meant for the client only. The access_token can be any type of token (not necessarily a JWT) and is meant for the API

Based on that, I'd assume that you are not supposed to use your access_token in Angular application

PavelPikat avatar Apr 26 '17 09:04 PavelPikat

Actually they are saying I should NOT be using the JWT id_token but rather need to use the opaque non JWT access_token for all requests

For example, when retrieving the user profile you pass the access_token as a bearer

E;g; Authorization: Bearer 833ec1ca1a47b128634a4744343a6215

DavidDecraene avatar Apr 26 '17 09:04 DavidDecraene

I would wait for @chenkie to chime in, but it seems to me that in this case client is your server and API/server is Google or Auth0. The access token is what your server sends to Google to read your appointments, and the id_token is what you send to your server to say who you are.

You might also use the access token to refresh your id_token, but I'm not that familiar with Oauth2 or OpenID. I know JWT, SAML and OAuth1

escardin avatar Apr 26 '17 12:04 escardin

@DavidDecraene your access token needs to be a JWT for use with your API. Here's a sample (with a Node backed) that shows how to do this with Angular: https://github.com/auth0-samples/auth0-angular-samples/tree/master/04-Calling-API

Please note that access tokens are not meant to be decoded on the client side and thus you shouldn't use tokenNotExpired for that purpose. The sample linked above uses the expiresIn value that comes back after authentication is complete to set a time at which the user should be considered unauthenticated.

chenkie avatar Apr 26 '17 13:04 chenkie

The problem arises due to an improper storage of the token in the localStorage. While saving the token in the localStorage as localStorage.setItem('id_token', token); , now the latest change in angular has modified that to accept a default value of "token" instead of "id_token", so just change "id_token" to "token" everywhere in the setItem and getItem commands (eg. localStorage.setItem('token', token); ), and that should most probably solve your problem.

CaptainDroid avatar May 29 '17 06:05 CaptainDroid

rename - id_token to token in all places

sameerparab355 avatar Jun 21 '17 06:06 sameerparab355

I have same error. I solve this this error. Create new API in auth0. give Unique Identifier name. I took "NodeAPI" I mention below config code. auth0 = new auth0.WebAuth({ clientID: 'Hg3EhAWKgrPrX5UNGqFQA5vTbVGWF', domain: 'xyz.auth0.com', responseType: 'token id_token', audience: 'NodeAPI', redirectUri: 'http://localhost:4200/callback', scope: 'openid' }); Happy to help you

prakashbariya avatar Aug 06 '17 05:08 prakashbariya

@DavidDecraene I am stuck in the same scenario just like you have mentioned above, were you able to resolve and find an answer. I have to send access_token to my backend (where my backend uses auth0 client library to get my profile information. Since I cannot use the id_token to retrieve profile information and access_token cannot be sent through header of my request due to JWT must have 3 parts I am clueless of what need to be done to resolve this issue. Can anyone help me on this, Thanks!!

keshann93 avatar Aug 08 '17 04:08 keshann93

@KeshShan it sounds like you are getting an opaque access token whereas what you need is a JWT access token. Have you followed the Calling an API quickstart?

chenkie avatar Aug 08 '17 13:08 chenkie

@chenkie Thank you for that quick response, that helped!!

keshann93 avatar Aug 10 '17 16:08 keshann93