Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement/audience can accept list of string #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 8 additions & 4 deletions lib/corsac_jwt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class JWT {
String get issuer => _claims['iss'];

/// The audience of this token (value of standard `aud` claim).
String get audience => _claims['aud'];
List<String> get audience => _claims['aud'].cast<String>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to spec audience can either be a list or a single string value.

It seems that we should preserve this, which means having audience type as Object + updating dartdoc for the property to describe possible return values.

WDYT?


/// The time this token was issued (value of standard `iat` claim).
int get issuedAt => _claims['iat'];
Expand Down Expand Up @@ -185,8 +185,12 @@ class JWTBuilder {
}

/// Token audience (standard `aud` claim).
void set audience(String audience) {
_claims['aud'] = audience;
void set audience(dynamic audience) {
if (audience is String) {
_claims['aud'] = [audience];
} else if (audience is List<String>) {
_claims['aud'] = audience;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here, if someone wants to set aud as a single string we shouldn't force it to be an array.

}

/// Token issued at timestamp in seconds (standard `iat` claim).
Expand Down Expand Up @@ -333,7 +337,7 @@ class JWTValidator {
errors.add('The token issuer is invalid.');
}

if (audience is String && audience != token.audience) {
if (audience is String && !token.audience.contains(audience)) {
errors.add('The token audience is invalid.');
}

Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ environment:
dependencies:
asn1lib: "^0.5.0"
logging: "^0.11.3"
# pointycastle: "1.0.0-rc2"
# rsa_pkcs: "^0.1.2"
# pointycastle: "1.0.0-rc2"
# rsa_pkcs: "^0.1.2"
crypto: "^2.0.0"

dev_dependencies:
Expand Down
18 changes: 16 additions & 2 deletions test/jwt_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void main() {
builder = new JWTBuilder();
builder
..issuer = 'https://mycompany.com'
..audience = 'people'
..audience = ['people']
..issuedAt = now
..expiresAt = now.add(new Duration(seconds: 10))
..notBefore = now.add(new Duration(seconds: 5))
Expand Down Expand Up @@ -86,7 +86,21 @@ void main() {
var token = builder.getToken();
expect(token, const TypeMatcher<JWT>());
expect(token.issuer, equals('https://mycompany.com'));
expect(token.audience, equals('people'));
expect(token.audience.contains('people'), equals(true));
expect(token.issuedAt, equals(_secondsSinceEpoch(now)));
expect(token.expiresAt, equals(_secondsSinceEpoch(now) + 10));
expect(token.notBefore, equals(_secondsSinceEpoch(now) + 5));
expect(token.id, equals('identifier'));
expect(token.subject, equals('subj'));
expect(token.algorithm, equals('none'));
});

test('it supports all standard claims when the aud is string', () {
builder.audience = 'people';
var token = builder.getToken();
expect(token, const TypeMatcher<JWT>());
expect(token.issuer, equals('https://mycompany.com'));
expect(token.audience, contains('people'));
expect(token.issuedAt, equals(_secondsSinceEpoch(now)));
expect(token.expiresAt, equals(_secondsSinceEpoch(now) + 10));
expect(token.notBefore, equals(_secondsSinceEpoch(now) + 5));
Expand Down