Skip to content

Commit c475af3

Browse files
SaadArdatirayliverified
authored andcommitted
Manually deserialize jwt token on windows in CodelesslyAuthManager
1 parent 513df8d commit c475af3

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

lib/src/auth/codelessly_auth_manager.dart

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import 'dart:convert';
33
import 'dart:io';
44

55
import 'package:firebase_auth/firebase_auth.dart';
6+
import 'package:flutter/foundation.dart';
67
import 'package:http/http.dart';
8+
import 'package:jwt_decoder/jwt_decoder.dart';
79

810
import '../../codelessly_sdk.dart';
911

@@ -96,7 +98,6 @@ class CodelesslyAuthManager extends AuthManager {
9698
// the server attached a custom claim to the user's token to allow
9799
// Codelessly Cloud Data to work.
98100
_idTokenResult = await firebaseAuth.currentUser!.getIdTokenResult(true);
99-
final claims = _idTokenResult!.claims;
100101

101102
if (cacheManager.isCached(authCacheKey)) {
102103
try {
@@ -114,11 +115,7 @@ class CodelesslyAuthManager extends AuthManager {
114115
// also need to invalidate the cache and fetch auth data from the
115116
// server and revalidate.
116117
if (cachedAuthData.authToken == config.authToken &&
117-
claims != null &&
118-
claims.containsKey('project_ids') &&
119-
claims['project_ids'] is List &&
120-
(claims['project_ids'] as List)
121-
.contains(cachedAuthData.projectId)) {
118+
checkClaimsForProject(_idTokenResult, cachedAuthData.projectId)) {
122119
_authData = cachedAuthData;
123120
_authStreamController.add(_authData);
124121
} else {
@@ -201,10 +198,18 @@ class CodelesslyAuthManager extends AuthManager {
201198
return false;
202199
}
203200

204-
final claims = result.claims;
201+
final Map<String, dynamic> claims;
202+
203+
// https://github.com/firebase/flutterfire/issues/11768#issuecomment-1888363494
204+
// A temporary fix for windows platform until the issue is resolved.
205+
if (!kIsWeb && defaultTargetPlatform == TargetPlatform.windows) {
206+
claims = JwtDecoder.decode(result.toString());
207+
} else {
208+
claims = result.claims ?? {};
209+
}
205210

206211
log('User claims: $claims', largePrint: true);
207-
if (claims == null) {
212+
if (claims.isEmpty) {
208213
log('User claims is null. Cannot check claims for project id [$projectId]');
209214
return false;
210215
}
@@ -297,7 +302,7 @@ class CodelesslyAuthManager extends AuthManager {
297302
try {
298303
log('Authenticating token...');
299304

300-
final authData = await verifyProjectAuthToken(
305+
final AuthData? authData = await verifyProjectAuthToken(
301306
userToken: _idTokenResult!.token!,
302307
config: config,
303308
postSuccess: postAuthSuccess,
@@ -384,8 +389,10 @@ class CodelesslyAuthManager extends AuthManager {
384389
// successful.
385390
if (result.statusCode == 200) {
386391
logger.log(
387-
label, 'Successful auth token verification response received.',
388-
largePrint: true);
392+
label,
393+
'Successful auth token verification response received.',
394+
largePrint: true,
395+
);
389396

390397
// Parse the body of the response to JSON.
391398
final jsonBody = jsonDecode(result.body);

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ dependencies:
4848
flutter_svg: ^2.0.10+1
4949
cached_network_image: ^3.3.1
5050
rfc_6901: ^0.2.0 # json pointer spec
51+
jwt_decoder: ^2.0.1
5152

5253
dev_dependencies:
5354
flutter_test:

0 commit comments

Comments
 (0)