Skip to content

Commit

Permalink
Sign custom token with RS256 algorithm
Browse files Browse the repository at this point in the history
PR with fix in upstream: invertase#23
  • Loading branch information
blaugold committed Mar 19, 2024
1 parent bcb3846 commit 7bdb5e7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
32 changes: 27 additions & 5 deletions packages/dart_firebase_admin/lib/src/utils/crypto_signer.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'dart:convert';
import 'dart:typed_data';

import 'package:crypto/crypto.dart';
import 'package:googleapis_auth/googleapis_auth.dart' as auth;
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
import 'package:pointycastle/pointycastle.dart';

import '../../dart_firebase_admin.dart';

Expand Down Expand Up @@ -107,11 +107,33 @@ class _ServiceAccountSigner implements CryptoSigner {

@override
Future<Uint8List> sign(Uint8List buffer) async {
final key = utf8.encode(credential.privateKey);
final hmac = Hmac(sha256, key);
final digest = hmac.convert(buffer);
final rsaPrivateKey = _parsePrivateKeyFromPem();
final signer = Signer('SHA-256/RSA')
..init(true, PrivateKeyParameter<RSAPrivateKey>(rsaPrivateKey));
final signature = signer.generateSignature(buffer) as RSASignature;
return signature.bytes;
}

RSAPrivateKey _parsePrivateKeyFromPem() {
final privateKeyString = credential.privateKey
.replaceAll('-----BEGIN PRIVATE KEY-----', '')
.replaceAll('-----END PRIVATE KEY-----', '')
.replaceAll('\n', '');
final privateKeyDER = base64Decode(privateKeyString);

final asn1Parser = ASN1Parser(Uint8List.fromList(privateKeyDER));
final topLevelSequence = asn1Parser.nextObject() as ASN1Sequence;
final privateKeyOctet = topLevelSequence.elements![2] as ASN1OctetString;

final privateKeyParser = ASN1Parser(privateKeyOctet.valueBytes);
final privatekeySequence = privateKeyParser.nextObject() as ASN1Sequence;

final modulus = (privatekeySequence.elements![1] as ASN1Integer).integer!;
final exponent = (privatekeySequence.elements![3] as ASN1Integer).integer!;
final p = (privatekeySequence.elements![4] as ASN1Integer).integer;
final q = (privatekeySequence.elements![5] as ASN1Integer).integer;

return Uint8List.fromList(digest.bytes);
return RSAPrivateKey(modulus, exponent, p, q);
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/dart_firebase_admin/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
name: dart_firebase_admin
description: A Firebase Admin SDK implementation for Dart.
version: 0.3.0
homepage: "https://github.com/invertase/dart_firebase_admin"
repository: "https://github.com/invertase/dart_firebase_admin"
homepage: 'https://github.com/invertase/dart_firebase_admin'
repository: 'https://github.com/invertase/dart_firebase_admin'

environment:
sdk: ">=3.2.0 <4.0.0"
sdk: '>=3.2.0 <4.0.0'

dependencies:
collection: ^1.18.0
crypto: ^3.0.3
dart_jsonwebtoken: ^2.11.0
firebaseapis: ^0.2.0
freezed_annotation: ^2.4.1
googleapis_auth: ^1.3.0
http: ^1.0.0
intl: ^0.19.0
meta: ^1.9.1
pointycastle: ^3.7.4

dev_dependencies:
build_runner: ^2.4.7
Expand Down

0 comments on commit 7bdb5e7

Please sign in to comment.