msal_js 0.3.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new65

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(
    'your_client_id', 
    null, 
    _authCallback,
    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, 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).

Versionmsal.js
0.2.00.2.3
0.1.00.1.5

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.

example/example.dart

import 'package:msal_js/msal_js.dart';

// This example is based on
// https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-core/README.md#usage.

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>['user.read', '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
        print(ex);
      }
    }
  } on MsalException catch (ex) {
    // Login failure
    print(ex);
  }
}

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...
    print('$error:$errorDescription');
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  msal_js: ^0.3.1

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';
  
Version Uploaded Documentation Archive
0.3.1 Nov 28, 2018 Go to the documentation of msal_js 0.3.1 Download msal_js 0.3.1 archive
0.3.0 Nov 24, 2018 Go to the documentation of msal_js 0.3.0 Download msal_js 0.3.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
29
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
65
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

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.)

Dependencies

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