Skip to content

Commit

Permalink
Manually deserialize jwt token on windows in CodelesslyAuthManager
Browse files Browse the repository at this point in the history
  • Loading branch information
SaadArdati authored and rayliverified committed Jul 14, 2024
1 parent 513df8d commit c475af3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/src/auth/codelessly_auth_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'dart:convert';
import 'dart:io';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart';
import 'package:jwt_decoder/jwt_decoder.dart';

import '../../codelessly_sdk.dart';

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

if (cacheManager.isCached(authCacheKey)) {
try {
Expand All @@ -114,11 +115,7 @@ class CodelesslyAuthManager extends AuthManager {
// also need to invalidate the cache and fetch auth data from the
// server and revalidate.
if (cachedAuthData.authToken == config.authToken &&
claims != null &&
claims.containsKey('project_ids') &&
claims['project_ids'] is List &&
(claims['project_ids'] as List)
.contains(cachedAuthData.projectId)) {
checkClaimsForProject(_idTokenResult, cachedAuthData.projectId)) {
_authData = cachedAuthData;
_authStreamController.add(_authData);
} else {
Expand Down Expand Up @@ -201,10 +198,18 @@ class CodelesslyAuthManager extends AuthManager {
return false;
}

final claims = result.claims;
final Map<String, dynamic> claims;

// https://github.com/firebase/flutterfire/issues/11768#issuecomment-1888363494
// A temporary fix for windows platform until the issue is resolved.
if (!kIsWeb && defaultTargetPlatform == TargetPlatform.windows) {
claims = JwtDecoder.decode(result.toString());
} else {
claims = result.claims ?? {};
}

log('User claims: $claims', largePrint: true);
if (claims == null) {
if (claims.isEmpty) {
log('User claims is null. Cannot check claims for project id [$projectId]');
return false;
}
Expand Down Expand Up @@ -297,7 +302,7 @@ class CodelesslyAuthManager extends AuthManager {
try {
log('Authenticating token...');

final authData = await verifyProjectAuthToken(
final AuthData? authData = await verifyProjectAuthToken(
userToken: _idTokenResult!.token!,
config: config,
postSuccess: postAuthSuccess,
Expand Down Expand Up @@ -384,8 +389,10 @@ class CodelesslyAuthManager extends AuthManager {
// successful.
if (result.statusCode == 200) {
logger.log(
label, 'Successful auth token verification response received.',
largePrint: true);
label,
'Successful auth token verification response received.',
largePrint: true,
);

// Parse the body of the response to JSON.
final jsonBody = jsonDecode(result.body);
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies:
flutter_svg: ^2.0.10+1
cached_network_image: ^3.3.1
rfc_6901: ^0.2.0 # json pointer spec
jwt_decoder: ^2.0.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit c475af3

Please sign in to comment.