Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to get encoded access token #5023

Open
3 of 14 tasks
Glydric opened this issue Jun 13, 2024 · 5 comments
Open
3 of 14 tasks

Document how to get encoded access token #5023

Glydric opened this issue Jun 13, 2024 · 5 comments
Labels
auth Issues related to the Auth Category Documentation Improvements or fixes to public documentation (docs.amplify.aws, pub.dev, readmes). feature-request A request for a new feature or an enhancement to an existing API or category.

Comments

@Glydric
Copy link

Glydric commented Jun 13, 2024

Description

I was developing an application in dart when I found out that the jwt value i was getting printing directly
let ses = await Amplify.Auth.fetchAuthSession() safePrint(ses);
is different that the one obtained using
final tokens = ses.toJson()["userPoolTokens"] as CognitoUserPoolTokens; safePrint(tokens.accessToken.encode().toString());
that should be used to extract the user pool token.

I found out that the signature is equal, but using the second method it was someway reformatting the token and obtain a different base 64 value. that is instead wrong
Indeed the first can be verified with jwt.io, while the latter is impossible to verify.
This is a real problem as I seen from previous issues that I'm not the only one that founds out this problem. but maybe I just discovered this difference.
I just discovered this on an iPhone 15 with ios 17.5, build using flutter 3.19.5 and dart 3.3.3 on macos 14.5

Categories

  • Analytics
  • API (REST)
  • API (GraphQL)
  • Auth
  • Authenticator
  • DataStore
  • Notifications (Push)
  • Storage

Steps to Reproduce

let ses = await Amplify.Auth.fetchAuthSession()
safePrint(ses); // Correct accessToken jwt
final tokens = ses.toJson()["userPoolTokens"] as CognitoUserPoolTokens;
safePrint(tokens.accessToken.encode().toString()); // Wrong accessToken jwt

Screenshots

No response

Platforms

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Flutter Version

3.19.5

Amplify Flutter Version

2.1.0

Deployment Method

Custom Pipeline

Schema

No response

@Glydric
Copy link
Author

Glydric commented Jun 13, 2024

Correcting myself the problem is not about the two methods but is about using encode function, just don't use it and instead use toJson(), so use the following implementation

final cognitoPlugin = Amplify.Auth.getPlugin(AmplifyAuthCognito.pluginKey);
final result = await cognitoPlugin.fetchAuthSession();
final token = result.userPoolTokensResult.value.accessToken.toJson();

the token then can be used to sign any request (I can now verify in the backend)

@Jordan-Nelson
Copy link
Member

Hi @Glydric I think the difference you are noticing is due to the difference between the .json(), .encode(), and .toString() methods on JsonWebToken. You can see those three functions below.

@override
String toJson() => raw;
@override
String toString() => prettyPrintJson({
'header': header.toJson(),
'claims': claims.toJson(),
'signature': base64Encode(signature),
});
/// Encodes the JWT to a `.`-delimited string.
String encode() => '${header.encodeBase64()}.'
'${claims.encodeBase64()}.'
'${base64RawUrl.encode(signature)}';

Note that toJson calls .raw which in turn calls .encode()

/// The raw, encoded JWT string.
String get raw => _raw ?? encode();

In the first example (let ses = await Amplify.Auth.fetchAuthSession() safePrint(ses);) .toString() will be used where in the other examples .json() or .encode() is used.

It sounds like you may have already found a solution to the issue you were facing, but let me know if that is not the case.

@Jordan-Nelson Jordan-Nelson added question A question about the Amplify Flutter libraries auth Issues related to the Auth Category pending-community-response Pending response from the issue opener or other community members labels Jun 13, 2024
@Glydric
Copy link
Author

Glydric commented Jun 13, 2024

Hi @Jordan-Nelson, yes, I already found a solution, but I think that this can be a bit confusing for any new developer here and like me will lost a lot of time trying to get the correct JWT. Also I didn't read anything about this important difference on the amplify website, so I created this issue not only to find a final solution but also to help others.

@Jordan-Nelson
Copy link
Member

There are API docs for .raw and .encode(). .toString() is only overridden for debugging purposes. It really is not intended to be used in other contexts.

Were you expecting .toString() to return the encoded token?

@NikaHsn NikaHsn added the Documentation Improvements or fixes to public documentation (docs.amplify.aws, pub.dev, readmes). label Jun 13, 2024
@Jordan-Nelson
Copy link
Member

We have labeled this as a docs issue. We will look to add some info to the main docs site to make this more clear.

@Jordan-Nelson Jordan-Nelson removed question A question about the Amplify Flutter libraries pending-community-response Pending response from the issue opener or other community members labels Jun 14, 2024
@Jordan-Nelson Jordan-Nelson changed the title JWT wrong encode leads to invalid signature Document hot to get encoded access token Jun 14, 2024
@Jordan-Nelson Jordan-Nelson changed the title Document hot to get encoded access token Document how to get encoded access token Jun 14, 2024
@Jordan-Nelson Jordan-Nelson mentioned this issue Jun 21, 2024
13 tasks
@Jordan-Nelson Jordan-Nelson added the feature-request A request for a new feature or an enhancement to an existing API or category. label Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth Issues related to the Auth Category Documentation Improvements or fixes to public documentation (docs.amplify.aws, pub.dev, readmes). feature-request A request for a new feature or an enhancement to an existing API or category.
Projects
None yet
Development

No branches or pull requests

3 participants