Skip to content

Commit

Permalink
Update API to support MSAL 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Francessco121 committed Nov 17, 2019
1 parent a777051 commit 3289794
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v1.1.0
- Support for msal.js v1.1.x
- Added `Account.idTokenClaims`
- Added `AuthRequest.forceRefresh`

## v1.0.1
- Fixed `AuthRequest.extraQueryParameters` not working.
- Removed dependency on `package:js`.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ If the package version you are looking for is not listed, use the row correspond

| Version | msal.js |
| ----------- | ------- |
| **1.1.0** | 1.1.0 |
| **1.0.0** | 1.0.0 |
| **0.3.2** | 0.2.4 |
| **0.2.0** | 0.2.3 |
Expand Down
13 changes: 13 additions & 0 deletions lib/src/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ part of '../msal_js.dart';

/// An authenticated user account.
class Account {
static const _claimsConverter = JsObjectChainedConverter([
JsObjectListConverter(),
JsObjectMapConverter(),
JsObjectConverter.identity
], recursive: true);

/// The ID of this user's account.
String get accountIdentifier => _jsObject['accountIdentifier'];

Expand All @@ -16,8 +22,15 @@ class Account {
String get name => _jsObject['name'];

/// The ID token associated with this account.
///
/// Note: It is recommended to use [idTokenClaims] instead if you
/// just need the claims.
dynamic get idToken => _jsObject['idToken'];

/// A map of all claims in the [idToken].
Map<String, dynamic> get idTokenClaims =>
_claimsConverter.decode(_jsObject['idTokenClaims']);

/// The account's session identifier (`idToken.sid`).
String get sid => _jsObject['sid'];

Expand Down
5 changes: 5 additions & 0 deletions lib/src/auth_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,10 @@ class AuthRequest {
/// for more information.
set loginHint(String value) => _jsObject['loginHint'] = value;

bool get forceRefresh => _jsObject['forceRefresh'];
/// Sets whether the cache should be skipped and a token request to the
/// authorization server should be made.
set forceRefresh(bool value) => _jsObject['forceRefresh'] = value;

final _jsObject = new JsObject(context['Object']);
}
7 changes: 4 additions & 3 deletions lib/src/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,10 @@ class SystemOptions {
''')
class FrameworkOptions {
static const _unprotectedResourcesConverter = JsObjectListConverter<String>();
static const _protectedResourceConverter = JsObjectMapConverter<List<String>>(
JsObjectListConverter<String>()
);
static const _protectedResourceConverter = JsObjectChainedConverter([
JsObjectListConverter<String>(),
JsObjectMapConverter<List<String>>()
], recursive: true);

List<String> get unprotectedResources =>
_unprotectedResourcesConverter.decode(_jsObject['unprotectedResources']);
Expand Down
85 changes: 75 additions & 10 deletions lib/src/utils/js_object_converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,89 @@ abstract class JsObjectConverter {

const JsObjectConverter();

bool canEncode(dynamic value);
bool canDecode(dynamic value);

dynamic encode(dynamic value);
dynamic decode(dynamic value);
dynamic decode(dynamic value, [JsObjectConverter recursiveConverter]);
}

class JsObjectIdentityConverter extends JsObjectConverter {
const JsObjectIdentityConverter();

@override
dynamic decode(dynamic value) => value;
bool canDecode(value) => true;

@override
bool canEncode(value) => true;

@override
dynamic decode(dynamic value, [JsObjectConverter recursiveConverter]) => value;

@override
dynamic encode(dynamic value) => value;
}

class JsObjectChainedConverter extends JsObjectConverter {
/// Whether decoded values should also use this converter where applicable.
///
/// If false (default), decoded values which take converters will use the identity converter.
final bool recursive;
final List<JsObjectConverter> converters;

const JsObjectChainedConverter(this.converters, {this.recursive = false});

@override
bool canDecode(value) =>
converters.any((c) => c.canDecode(value));

@override
bool canEncode(value) =>
converters.any((c) => c.canEncode(value));

@override
dynamic decode(dynamic value, [JsObjectConverter recursiveConverter]) {
if (recursive) {
if (recursiveConverter == null) {
recursiveConverter = this;
} else {
recursiveConverter = JsObjectChainedConverter([recursiveConverter, this]);
}
}

for (final converter in converters) {
if (converter.canDecode(value)) {
return converter.decode(value, recursiveConverter);
}
}

return value;
}

@override
dynamic encode(dynamic value) {
for (final converter in converters) {
if (converter.canEncode(value)) {
return converter.encode(value);
}
}

return value;
}
}

class JsObjectMapConverter<V> extends JsObjectConverter {
final JsObjectConverter _nested;
const JsObjectMapConverter();

@override
bool canDecode(value) => value == null || value is JsObject;

const JsObjectMapConverter([this._nested]);
@override
bool canEncode(value) => value == null || value is Map<String, V>;

@override
dynamic decode(dynamic value) =>
value == null ? null : JsObjectAsMap<V>.created(value, _nested);
dynamic decode(dynamic value, [JsObjectConverter recursiveConverter]) =>
value == null ? null : JsObjectAsMap<V>.created(value, recursiveConverter);

@override
dynamic encode(dynamic value) {
Expand All @@ -47,13 +108,17 @@ class JsObjectMapConverter<V> extends JsObjectConverter {
}

class JsObjectListConverter<E> extends JsObjectConverter {
final JsObjectConverter _nested;
const JsObjectListConverter();

const JsObjectListConverter([this._nested]);
@override
bool canDecode(value) => value == null || value is JsArray;

@override
bool canEncode(value) => value == null || value is List<E>;

@override
dynamic decode(dynamic value) =>
value == null ? null : JsObjectAsList<E>.created(value, _nested);
dynamic decode(dynamic value, [JsObjectConverter recursiveConverter]) =>
value == null ? null : JsObjectAsList<E>.created(value, recursiveConverter);

@override
dynamic encode(dynamic value) {
Expand Down
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
name: msal_js
description: "A Dart wrapper for the 'Microsoft Authentication Library for JavaScript (MSAL.js)'."
version: 1.0.1
version: 1.1.0
homepage: https://github.com/milestonetg/msal-js-dart

authors:
- Ethan Lafrenais <ethan.lafrenais@milestonetg.com>
- Jason Penniman <jason.penniman@milestonetg.com>

environment:
sdk: ">=2.0.0 <3.0.0"
Expand Down
2 changes: 1 addition & 1 deletion test/auth_request_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>AuthRequest Test</title>
<link rel="x-dart-test" href="auth_request_test.dart">
<script src="https://alcdn.msauth.net/lib/1.0.2/js/msal.js"></script>
<script src="https://alcdn.msauth.net/lib/1.1.3/js/msal.js"></script>
<script src="packages/test/dart.js"></script>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion test/configuration_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>Configuration Test</title>
<link rel="x-dart-test" href="configuration_test.dart">
<script src="https://alcdn.msauth.net/lib/1.0.2/js/msal.js"></script>
<script src="https://alcdn.msauth.net/lib/1.1.3/js/msal.js"></script>
<script src="packages/test/dart.js"></script>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion test/user_agent_application_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>UserAgentApplication Test</title>
<link rel="x-dart-test" href="user_agent_application_test.dart">
<script src="https://alcdn.msauth.net/lib/1.0.2/js/msal.js"></script>
<script src="https://alcdn.msauth.net/lib/1.1.3/js/msal.js"></script>
<script src="packages/test/dart.js"></script>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion test/utils/js_object_as_list_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>JsObjectAsList Test</title>
<link rel="x-dart-test" href="js_object_as_list_test.dart">
<script src="https://alcdn.msauth.net/lib/1.0.2/js/msal.js"></script>
<script src="https://alcdn.msauth.net/lib/1.1.3/js/msal.js"></script>
<script src="packages/test/dart.js"></script>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion test/utils/js_object_as_map_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>JsObjectAsMap Test</title>
<link rel="x-dart-test" href="js_object_as_map_test.dart">
<script src="https://alcdn.msauth.net/lib/1.0.2/js/msal.js"></script>
<script src="https://alcdn.msauth.net/lib/1.1.3/js/msal.js"></script>
<script src="packages/test/dart.js"></script>
</head>
<body>
Expand Down

0 comments on commit 3289794

Please sign in to comment.