2021-05-14 11:09:48 +00:00
|
|
|
import 'package:angel3_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: {
|
2017-01-13 15:50:38 +00:00
|
|
|
"foo": "bar",
|
|
|
|
"baz": {
|
|
|
|
"one": 1,
|
|
|
|
"franken": ["stein"]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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));
|
|
|
|
});
|
|
|
|
}
|