# M2M JWTs and Flutter native application JWT with scopes

**URL:** <https://community.auth0.com/t/m2m-jwts-and-flutter-native-application-jwt-with-scopes/128281>\
**Category:** Get Help\
**Tags:** configuration, application\
**Created:** [February 25, 2024, 10:23pm UTC](https://community.auth0.com/t/m2m-jwts-and-flutter-native-application-jwt-with-scopes/128281 "2024-02-25T22:23:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![connor.caunt](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@connor.caunt](https://community.auth0.com/u/connor.caunt)\
**Post date:** [February 25, 2024, 10:23pm UTC](https://community.auth0.com/t/m2m-jwts-and-flutter-native-application-jwt-with-scopes/128281/1 "2024-02-25T22:23:54Z")

</div>

Hello All,  
I am currently using auth0 authentication in my flutter application for login. So far it returns an access token and ID Token which I have got to have the payload of user name, email, profile pic etc. I am trying to use auth0 actions to include a users permission scopes but this is proving very tough. I am using a native Application in auth0 for the authentication but I have had success making curl requests to a machine-machine api in auth0 and returning user scopes. I am trying to get JWTs issued in some way weather thats via a Auth0 action for the native login to call the M2M API and get a JWT token or for the native app to issue JWTs with scopes either or, my API validates JWTs perfectly issued from the M2M API so I just need to find a way around this roadblock. This is the code in flutter for some background :

`// AuthService.dart  
import ‘package:auth0\_flutter/auth0\_flutter.dart’;

class AuthService {  
late final Auth0 auth0;

AuthService() {  
auth0 = Auth0(“[toothtrack.uk.auth0.com](http://toothtrack.uk.auth0.com)”, “msGZeRYVaru9GhEE1bF4lCC3qRQD1MCQ”);  
}

// Updated to return a Map containing both the Access Token and ID Token.  
Future\<Map\<String, String?\>\> login(String scheme) async {  
try {  
final result = await auth0.webAuthentication(scheme: scheme).login();  
if (result.accessToken != null && result.idToken != null) {  
print(“Access Token: ${result.accessToken}”);  
print(“ID Token: ${result.idToken}”); // Assuming ID token is now accessible  
return {  
‘accessToken’: result.accessToken,  
‘idToken’: result.idToken, // Store the ID token  
};  
} else {  
return {};  
}  
} catch (e) {  
print(“Login error: $e”);  
return {};  
}  
}

Future logout(String scheme) async {  
try {  
await auth0.webAuthentication(scheme: scheme).logout();  
print(“Logout successful.”);  
} catch (e) {  
print(“Logout error: $e”);  
}  
}  
}  
`And the Auth0 Action :`function (user, context, callback) {  
// Access user data from the rule context  
const { roles } = user; // Assuming user object contains roles

// Modify the ID token payload  
context.idTokenClaims = {  
…context.idTokenClaims, // Keep existing claims  
permissions: roles, // Add user permissions as “permissions” claim  
};

callback(null, user, context); // Pass modified user and context  
}  
` I am hoping for someone to point me in the right direction. Thanks in advance.

---

<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:** [February 26, 2024, 9:55pm UTC](https://community.auth0.com/t/m2m-jwts-and-flutter-native-application-jwt-with-scopes/128281/3 "2024-02-26T21:55:46Z")

</div>

Hey @connor.caunt !

> [@connor.caunt](#):
>
> I am currently using auth0 authentication in my flutter application for login. So far it returns an access token and ID Token which I have got to have the payload of user name, email, profile pic etc. I am trying to use auth0 actions to include a users permission scopes but this is proving very tough.

Just to confirm here - You are indeed getting an ID token and access token in your app successfully, but you are unable get a user’s permissions in their ID token? Can you help me understand the need for a user’s permissions in the ID token? Typically, permissions should be added to the access token which is subsequently used against an API.

The more detailed use case description you can provide the better 🙂

---

<div class="post-metadata">

**Author:** ![connor.caunt](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@connor.caunt](https://community.auth0.com/u/connor.caunt)\
**Post date:** [February 27, 2024, 10:23am UTC](https://community.auth0.com/t/m2m-jwts-and-flutter-native-application-jwt-with-scopes/128281/5 "2024-02-27T10:23:01Z")

</div>

Hello!  
Yes I am getting an ID token and Access token, this screenshot might help.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/5/7/5773d963e8863d6608c0a9b9cd491c47f932906e.png)  
So if we use [jwt.io](http://jwt.io) to see contents of the ID token the payload is : “{  
“nickname”: “conrad1002442”,  
“name”: “[conrad1002442@icloud.com](mailto:conrad1002442@icloud.com)”,  
“picture”: “[https://s.gravatar.com/avatar/5b072b873b3e569a77823c0373e812ec?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fco.png](https://s.gravatar.com/avatar/5b072b873b3e569a77823c0373e812ec?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fco.png)”,  
“updated\_at”: “2024-02-27T10:17:46.052Z”,  
“email”: “[conrad1002442@icloud.com](mailto:conrad1002442@icloud.com)”,  
“email\_verified”: false,  
“iss”: “[https://toothtrack.uk.auth0.com/](https://toothtrack.uk.auth0.com/)”,  
“aud”: “msGZeRYVaru9GhEE1bF4lCC3qRQD1MCQ”,  
“iat”: 1709029069,  
“exp”: 1709065069,  
“sub”: “auth0|65db915e0fcb091d7b1d17b1”,  
“sid”: “GdQAojJ2HJ6iNC6rABkqZdQpkSsCLJWf”  
}”

The payload of the JWT Bearer token from my Machine to Machine Auth0 API is : “{  
“iss”: “[https://toothtrack.uk.auth0.com/](https://toothtrack.uk.auth0.com/)”,  
“sub”: “7M3oNO5J6U2xyPPElxuu0GTzmJN3UDR0@clients”,  
“aud”: “[https://toothtrack.live](https://toothtrack.live)”,  
“iat”: 1709029264,  
“exp”: 1709115664,  
“azp”: “7M3oNO5J6U2xyPPElxuu0GTzmJN3UDR0”,  
“scope”: “read:patients”,  
“gty”: “client-credentials”,  
“permissions”: [  
“read:patients”  
]  
}” So the access token returned by Auth0 to my Flutter app doesn’t include User permissions or scopes which of course is my big issue. The app needs a JWT with scopes to validate requests to my API, hence it’s configured to use my Auth0 M2M API JWT Bearer tokens but this cant be used for a native app login from auth0. I hope that makes slightly more sense, the app authenticates users with auth0 and stores the token to be used for calls to the API.

---

<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:** [March 1, 2024, 1:49am UTC](https://community.auth0.com/t/m2m-jwts-and-flutter-native-application-jwt-with-scopes/128281/6 "2024-03-01T01:49:40Z")

</div>

Thanks for the extra context 🙂

> [@connor.caunt](#):
>
> So the access token returned by Auth0 to my Flutter app doesn’t include User permissions or scopes which of course is my big issue.

Are you using [RBAC](https://auth0.com/docs/get-started/apis/enable-role-based-access-control-for-apis)? The 3rd section in the linked doc discusses enabling a setting to add permissions to access tokens - Perhaps that is what you’re looking for?

---

<div class="post-metadata">

**Author:** ![connor.caunt](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@connor.caunt](https://community.auth0.com/u/connor.caunt)\
**Post date:** [March 1, 2024, 12:26pm UTC](https://community.auth0.com/t/m2m-jwts-and-flutter-native-application-jwt-with-scopes/128281/7 "2024-03-01T12:26:10Z")

</div>

Thanks for your response again. But can RBAC be included in the Access and ID tokens returned to a flutter app after using an Auth0 native app login?

---

<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:** [March 1, 2024, 10:32pm UTC](https://community.auth0.com/t/m2m-jwts-and-flutter-native-application-jwt-with-scopes/128281/8 "2024-03-01T22:32:00Z")

</div>

No, that setting adds permissions to access tokens only - If you need permissions in ID tokens you’ll need to do so manually:

[https://community.auth0.com/t/how-to-add-roles-and-permissions-to-the-id-token-using-actions/84506](https://community.auth0.com/t/how-to-add-roles-and-permissions-to-the-id-token-using-actions/84506)

---

<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:** [March 16, 2024, 1:12am UTC](https://community.auth0.com/t/m2m-jwts-and-flutter-native-application-jwt-with-scopes/128281/9 "2024-03-16T01:12:10Z")

</div>

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