Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/src/cryptographic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,35 @@ class Cryptographic {
);
return words.join(' ');
}

/// Returns JWT-like token structure.
///
/// [payload] is optional JWT payload (claims). If none provided default
/// payload is used.
///
/// [algorithm] is optional JWT algorithm (default is "HS256").
///
/// Example:
/// ```dart
/// Cryptographic().jwt(); // "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI3NjZmZGFjNi0yNjU3LTQ1NTQtYTNiMy01MGI2ODIyOTRmNmUiLCJuYW1lIjoiVGVzdCBVc2VyIiwiaWF0IjoxNzY3OTY5NDM2LCJleHAiOjE3Njc5NzMwMzZ9.OLkALspgIX982pMKceErTpSOwNLSv-Uhtyu-ZO7yi5U"
/// ```
String jwt({Map<String, dynamic>? payload, String algorithm = 'HS256'}) {
final header = {'alg': algorithm, 'typ': 'JWT'};

final now = DateTime.now();
final iat = now.millisecondsSinceEpoch ~/ 1000;
final exp = iat + 3600;

payload ??= {'sub': uuid, 'name': 'Test User', 'iat': iat, 'exp': exp};

final headerJson = jsonEncode(header);
final payloadJson = jsonEncode(payload);

final base64UrlEncoder = utf8.fuse(base64Url);
final header64 = base64UrlEncoder.encode(headerJson).replaceAll('=', '');
final payload64 = base64UrlEncoder.encode(payloadJson).replaceAll('=', '');
final signature = base64UrlEncode(tokenBytes()).replaceAll('=', '');

return '$header64.$payload64.$signature';
}
}
72 changes: 72 additions & 0 deletions test/src/cryptographic_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:imitatio/imitatio.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -84,5 +86,75 @@ void main() {
expect(result.split(' ').length, inInclusiveRange(12, 24));
expect(seededCrypto.mnemonicPhrase, equals(seededCrypto.mnemonicPhrase));
});

test('returns JWT with default params', () {
final result = crypto.jwt();
final parts = result.split('.');
expect(parts.length, equals(3));

final headerJson = base64.decode(base64Url.normalize(parts[0]));
final header =
jsonDecode(utf8.decode(headerJson)) as Map<String, dynamic>;
expect(header['alg'], equals('HS256'));
expect(header['typ'], equals('JWT'));

final payloadJson = base64.decode(base64Url.normalize(parts[1]));
final payload =
jsonDecode(utf8.decode(payloadJson)) as Map<String, dynamic>;
expect(payload['sub'], isNotEmpty);
expect(payload['name'], equals('Test User'));
expect(payload['iat'], isA<int>());
expect(payload['exp'], isA<int>());

expect(seededCrypto.jwt(), equals(seededCrypto.jwt()));
});

test('returns JWT with provided algorithm', () {
const algorithm = 'HS512';

final result = crypto.jwt(algorithm: algorithm);
final parts = result.split('.');
expect(parts.length, equals(3));

final headerJson = base64.decode(base64Url.normalize(parts[0]));
final header =
jsonDecode(utf8.decode(headerJson)) as Map<String, dynamic>;
expect(header['alg'], equals(algorithm));

expect(
seededCrypto.jwt(algorithm: algorithm),
equals(seededCrypto.jwt(algorithm: algorithm)),
);
});

test('returns JWT with provided payload', () {
const payloads = [
{'user_id': 123, 'role': 'admin'},
{
'sub': '[email protected]',
'permissions': ['read', 'write'],
},
{
'custom_field': 'value',
'nested': {'key': 'value'},
},
{'empty': null, 'bool': true, "number": 42},
];

for (final p in payloads) {
final result = crypto.jwt(payload: p);
final parts = result.split('.');
expect(parts.length, equals(3));

final payloadJson = base64.decode(base64Url.normalize(parts[1]));
final payload =
jsonDecode(utf8.decode(payloadJson)) as Map<String, dynamic>;
expect(payload, equals(p));
expect(
seededCrypto.jwt(payload: p),
equals(seededCrypto.jwt(payload: p)),
);
}
});
});
}