msal_js 0.3.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 68

MSAL.js for Dart #

A Dart wrapper for the Microsoft Authentication Library Preview for JavaScript (MSAL.js).

Install msal.js #

This package does not come with msal.js. Please refer to the msal.js documentation for installation.

See the msal.js support table below to determine which version of msal.js should be used with each version of this package.

Usage #

See example/example.dart for a more complete example.

Also see the msal.js documentation. Class, function, and parameter names are all the same between this wrapper and msal.js, so following msal.js examples should be mostly straight-forward.

Short getting started example:

import 'package:msal_js/msal_js.dart';

void main() {
  // Important note about the below 'options' objects:
  // To set an option to `undefined`, simply don't call
  // the setter for that option at all. This will let
  // msal.js use the option's default value. Setting an
  // option to `null` will not use its default value.

  // Create an MSAL logger
  var logger = new Logger(_loggerCallback,
    new LoggerOptions()
      ..level = LogLevel.verbose // log everything

  // Create an MSAL authentication context
  var userAgentApplication = new UserAgentApplication(
    new UserAgentApplicationOptions()
      ..cacheLocation = CacheLocation.localStorage

  // Login by calling either:
  // - userAgentApplication.loginRedirect
  // - userAgentApplication.loginPopup

  // After login, acquire an access token by calling:
  // userAgentApplication.acquireTokenSilent

  // If acquiring the token silently fails, use either:
  // - userAgentApplication.acquireTokenRedirect
  // - userAgentApplication.acquireTokenPopup

  // See the msal.js documentation for more information.

void _loggerCallback(LogLevel level, String message, bool containsPii) {
  print('[$level] $message');

void _authCallback(String errorDescription, String token, String error, String tokenType, String userState) {
  // ...

msal.js Support Table #

Each package version (on the left) specifies which version of msal.js it supports (on the right). Only changes in support are added to this table.

If the package version you are looking for is not listed, use the row corresponding to the next listed version going down. For example, the package version 0.3.0 is not listed, so the version of msal.js that 0.3.0 supports is 0.2.3 since the next package entry down is 0.2.0 (which supports msal.js 0.2.3).


v0.3.2 #

  • Support for msal.js v0.2.4.
  • UserAgentApplicationOptions changes:
    • redirectUri may now be either a String or RedirectUriCallback.
    • postLogoutRedirectUri may now be either a String or RedirectUriCallback.
  • UserAgentApplication changes:
    • The constructor parameter tokenReceivedCallback may now be null.

v0.3.1 #

  • Fix crash which occurred when using the logger callback in code compiled with dart2js.

v0.3.0 #

  • Rewrote entire library using package:js.
  • The constructor for UserAgentApplication now semantically matches the JavaScript version.
  • The constructor for Logger now semantically matches the JavaScript version.
  • No longer contains a build of msal.js. A valid version of msal.js must be installed separately.

Versions listed below are not available on pub as they were made before this package was published.

v0.2.1 #

  • Fix MsalException not correctly decoding the error code/description.

v0.2.0 #

  • Support for msal.js v0.2.3.
  • All UserAgentApplication constructor parameters are now named. Parameters that used to be positional are now marked as @required.

v0.1.0 #

  • Support for msal.js v0.1.5.


import 'dart:async';
import 'package:msal_js/msal_js.dart';

// This example is based on

String idToken;
String accessToken;

Future<void> main() async {
  // Instantiate a UserAgentApplication with a client ID and callback
  final userAgentApplication = new UserAgentApplication('your_client_id', null, authCallback);

  // Login the user via a popup and get an access token
  final graphScopes = <String>['', 'mail.send'];

  try {
    idToken = await userAgentApplication.loginPopup(graphScopes);

    print('Successfully received ID token from login.');

    try {
      accessToken = await userAgentApplication.acquireTokenSilent(graphScopes);

      print('Acquired access token silently.');
    } on MsalException {
      // Failed to acquire token silently, send an interactive request instead
      try {
        accessToken = await userAgentApplication.acquireTokenPopup(graphScopes);

        print('Acquired access token after interactive prompt.');
      } on MsalException catch (ex) {
        // Acquire token failure
  } on MsalException catch (ex) {
    // Login failure

void authCallback(String errorDescription, String token, String error, 
  String tokenType, String userState
) {
  if (token != null) {
    // Login was successful!
    print('Success in auth calback!');
  } else {
    // An error occurred while logging in...

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  msal_js: ^0.3.2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:msal_js/msal_js.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Oct 11, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21


Detected platforms: web, other

Primary library: package:msal_js/msal_js.dart with components: js.

Health suggestions

Format lib/msal_js.dart.

Run dartfmt to format lib/msal_js.dart.

Format lib/src/interop/interop.dart.

Run dartfmt to format lib/src/interop/interop.dart.

Format lib/src/interop/logger_interop.dart.

Run dartfmt to format lib/src/interop/logger_interop.dart.

Fix additional 7 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/interop/user_agent_application_interop.dart (Run dartfmt to format lib/src/interop/user_agent_application_interop.dart.)
  • lib/src/interop/user_interop.dart (Run dartfmt to format lib/src/interop/user_interop.dart.)
  • lib/src/interop/utils/promise_interop.dart (Run dartfmt to format lib/src/interop/utils/promise_interop.dart.)
  • lib/src/logger.dart (Run dartfmt to format lib/src/logger.dart.)
  • lib/src/msal_exception.dart (Run dartfmt to format lib/src/msal_exception.dart.)
  • lib/src/user_agent_application.dart (Run dartfmt to format lib/src/user_agent_application.dart.)
  • lib/src/utils/promise_utils.dart (Run dartfmt to format lib/src/utils/promise_utils.dart.)


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.23.0 <3.0.0
js ^0.6.1 0.6.1+1