Skip to content

Commit dec4598

Browse files
committed
downgrade dart_jsonwebtoken
1 parent 4875df0 commit dec4598

File tree

4 files changed

+105
-13
lines changed

4 files changed

+105
-13
lines changed

packages/gotrue/lib/src/gotrue_client.dart

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,19 +1341,17 @@ class GoTrueClient {
13411341
return exception;
13421342
}
13431343

1344-
Future<JWTKey?> _fetchJwk(String kid, JWKSet suppliedJwks) async {
1344+
Future<JWK?> _fetchJwk(String kid, JWKSet suppliedJwks) async {
13451345
// try fetching from the supplied jwks
1346-
final jwk = suppliedJwks.keys
1347-
.firstWhereOrNull((jwk) => jwk.toJWK(keyID: kid)['kid'] == kid);
1346+
final jwk = suppliedJwks.keys.firstWhereOrNull((jwk) => jwk.kid == kid);
13481347
if (jwk != null) {
13491348
return jwk;
13501349
}
13511350

13521351
final now = DateTime.now();
13531352

13541353
// try fetching from cache
1355-
final cachedJwk = _jwks?.keys
1356-
.firstWhereOrNull((jwk) => jwk.toJWK(keyID: kid)['kid'] == kid);
1354+
final cachedJwk = _jwks?.keys.firstWhereOrNull((jwk) => jwk.kid == kid);
13571355

13581356
// jwks exists and it isn't stale
13591357
if (cachedJwk != null &&
@@ -1379,8 +1377,7 @@ class GoTrueClient {
13791377
_jwksCachedAt = now;
13801378

13811379
// find the signing key
1382-
return jwks.keys
1383-
.firstWhereOrNull((jwk) => jwk.toJWK(keyID: kid)['kid'] == kid);
1380+
return jwks.keys.firstWhereOrNull((jwk) => jwk.kid == kid);
13841381
}
13851382

13861383
/// Extracts the JWT claims present in the access token by first verifying the
@@ -1434,7 +1431,7 @@ class GoTrueClient {
14341431
}
14351432

14361433
try {
1437-
JWT.verify(token, signingKey);
1434+
JWT.verify(token, signingKey.rsaPublicKey);
14381435
return GetClaimsResponse(
14391436
claims: decoded.payload,
14401437
header: decoded.header,

packages/gotrue/lib/src/types/jwt.dart

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:convert';
2+
13
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
24

35
/// JWT Header structure
@@ -159,21 +161,114 @@ class GetClaimsOptions {
159161
}
160162

161163
class JWKSet {
162-
final List<JWTKey> keys;
164+
final List<JWK> keys;
163165

164166
JWKSet({required this.keys});
165167

166168
factory JWKSet.fromJson(Map<String, dynamic> json) {
167169
final keys = (json['keys'] as List<dynamic>?)
168-
?.map((e) => JWTKey.fromJWK(e as Map<String, dynamic>))
170+
?.map((e) => JWK.fromJson(e as Map<String, dynamic>))
169171
.toList() ??
170172
[];
171173
return JWKSet(keys: keys);
172174
}
173175

174176
Map<String, dynamic> toJson() {
175177
return {
176-
'keys': keys.map((e) => e.toJWK()).toList(),
178+
'keys': keys.map((e) => e.toJson()).toList(),
179+
};
180+
}
181+
}
182+
183+
/// {@template jwk}
184+
/// JSON Web Key (JWK) representation.
185+
/// {@endtemplate}
186+
class JWK {
187+
/// The "kty" (key type) parameter identifies the cryptographic algorithm
188+
/// family used with the key, such as "RSA" or "EC".
189+
final String kty;
190+
191+
/// The "key_ops" (key operations) parameter identifies the cryptographic
192+
/// operations for which the key is intended to be used.
193+
final List<String> keyOps;
194+
195+
/// The "alg" (algorithm) parameter identifies the algorithm intended for
196+
/// use with the key.
197+
final String? alg;
198+
199+
/// The "kid" (key ID) parameter is used to match a specific key.
200+
final String? kid;
201+
202+
/// Additional arbitrary properties of the JWK.
203+
final Map<String, dynamic> _additionalProperties;
204+
205+
/// {@macro jwk}
206+
JWK({
207+
required this.kty,
208+
required this.keyOps,
209+
this.alg,
210+
this.kid,
211+
Map<String, dynamic>? additionalProperties,
212+
}) : _additionalProperties = additionalProperties ?? {};
213+
214+
/// Creates a [JWK] from a JSON map.
215+
factory JWK.fromJson(Map<String, dynamic> json) {
216+
final kty = json['kty'] as String;
217+
final keyOps =
218+
(json['key_ops'] as List<dynamic>?)?.map((e) => e as String).toList() ??
219+
[];
220+
final alg = json['alg'] as String?;
221+
final kid = json['kid'] as String?;
222+
223+
final Map<String, dynamic> additionalProperties = Map.from(json);
224+
additionalProperties.remove('kty');
225+
additionalProperties.remove('key_ops');
226+
additionalProperties.remove('alg');
227+
additionalProperties.remove('kid');
228+
229+
return JWK(
230+
kty: kty,
231+
keyOps: keyOps,
232+
alg: alg,
233+
kid: kid,
234+
additionalProperties: additionalProperties,
235+
);
236+
}
237+
238+
/// Allows accessing additional properties using operator[].
239+
dynamic operator [](String key) {
240+
switch (key) {
241+
case 'kty':
242+
return kty;
243+
case 'key_ops':
244+
return keyOps;
245+
case 'alg':
246+
return alg;
247+
case 'kid':
248+
return kid;
249+
default:
250+
return _additionalProperties[key];
251+
}
252+
}
253+
254+
/// Converts this [JWK] to a JSON map.
255+
Map<String, dynamic> toJson() {
256+
final Map<String, dynamic> json = {
257+
'kty': kty,
258+
'key_ops': keyOps,
259+
..._additionalProperties,
177260
};
261+
if (alg != null) {
262+
json['alg'] = alg;
263+
}
264+
if (kid != null) {
265+
json['kid'] = kid;
266+
}
267+
return json;
268+
}
269+
270+
RSAPublicKey get rsaPublicKey {
271+
final bytes = utf8.encode(json.encode(toJson()));
272+
return RSAPublicKey.bytes(bytes);
178273
}
179274
}

packages/gotrue/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies:
1818
meta: ^1.7.0
1919
logging: ^1.2.0
2020
web: ">=0.5.0 <2.0.0"
21-
dart_jsonwebtoken: ^3.3.0
21+
dart_jsonwebtoken: ^2.17.0
2222

2323
dev_dependencies:
2424
dotenv: ^4.1.0

packages/supabase_flutter/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencies:
2525
web: '>=0.5.0 <2.0.0'
2626

2727
dev_dependencies:
28-
dart_jsonwebtoken: ^3.3.0
28+
dart_jsonwebtoken: ^2.17.0
2929
flutter_test:
3030
sdk: flutter
3131
flutter_lints: ^3.0.1

0 commit comments

Comments
 (0)