platform/packages/auth/test/auth_token_test.dart

35 lines
966 B
Dart
Raw Permalink Normal View History

import 'package:protevus_auth/src/auth_token.dart';
2021-05-09 11:16:15 +00:00
import 'package:crypto/crypto.dart';
import 'package:test/test.dart';
2016-09-21 23:09:23 +00:00
2021-05-09 11:16:15 +00:00
void main() async {
final hmac = Hmac(sha256, 'angel_auth'.codeUnits);
2016-09-21 23:09:23 +00:00
2021-05-09 11:16:15 +00:00
test('sample serialization', () {
var token = AuthToken(ipAddress: 'localhost', userId: 'thosakwe');
2016-09-21 23:09:23 +00:00
var jwt = token.serialize(hmac);
print(jwt);
2019-04-19 07:50:04 +00:00
var parsed = AuthToken.validate(jwt, hmac);
2016-09-21 23:09:23 +00:00
print(parsed.toJson());
2017-01-13 15:50:38 +00:00
expect(parsed.toJson()['aud'], equals(token.ipAddress));
expect(parsed.toJson()['sub'], equals(token.userId));
2016-09-21 23:09:23 +00:00
});
2017-01-13 15:50:38 +00:00
test('custom payload', () {
2021-05-09 11:16:15 +00:00
var token = AuthToken(ipAddress: 'localhost', userId: 'thosakwe', payload: {
2021-06-07 00:50:39 +00:00
'foo': 'bar',
'baz': {
'one': 1,
'franken': ['stein']
2017-01-13 15:50:38 +00:00
}
});
var jwt = token.serialize(hmac);
print(jwt);
2019-04-19 07:50:04 +00:00
var parsed = AuthToken.validate(jwt, hmac);
2017-01-13 15:50:38 +00:00
print(parsed.toJson());
expect(parsed.toJson()['pld'], equals(token.payload));
});
}