MSAL


Source link: https://github.com/AzureAD/microsoft-authentication-library-for-android

Microsoft Authentication Library (MSAL) Preview for Android

Getting Started Sample Code Library Reference Support

The MSAL library for Android gives your app the ability to begin using the Microsoft Cloud by supporting Microsoft Azure Active Directory and Microsoft Accounts in a converged experience using industry standard OAuth2 and OpenID Connect. The library also supports Azure AD B2C.

Important Note about the MSAL Preview

This library is suitable for use in a production environment. We provide the same production level support for this library as we do our current production libraries. During the preview we may make changes to the API, internal cache format, and other mechanisms of this library, which you will be required to take along with bug fixes or feature improvements. This may impact your application. For instance, a change to the cache format may impact your users, such as requiring them to sign in again. An API change may require you to update your code. When we provide the General Availability release we will require you to update to the General Availability version within six months, as applications written using a preview version of library may no longer work.

Example

 // Instantiates MSAL Public Client App
  PublicClientApplication myApp = new PublicClientApplication(

this.getApplicationContext(),

CLIENT_ID);

// Acquires a token from AzureAD 
  myApp.acquireToken(this, SCOPES, getAuthInteractiveCallback());

// ...

// The access token can now be used to access a protected service!
  String accessToken = authenticationResult.getAccessToken();

For a full example, checkout the full code sample.

Installation

Binaries via Gradle (Recommended way)

Add to your app's Build.Gradle:

 repositories {

mavenCentral()
  
}

  dependencies {

compile('com.microsoft.identity.client:msal:0.1.1') {

 // if your app includes android support

 // libraries or GSON in its dependencies

 // uncomment below

  // exclude group: 'com.android.support', module: 'appcompat-v7'

 // exclude group: 'com.google.code.gson'

}

  
}

AAR package inside libs folder

You can get the AAR file from maven central and drop into libs folder in your project.

Community Help and Support

We use Stack Overflow with the community to provide support. We highly recommend you ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before.

If you find and bug or have a feature request, please raise the issue on GitHub Issues.

To provide a recommendation, visit our User Voice page.

Contribute

We enthusiastically welcome contributions and feedback. You can clone the repo and start contributing now.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Security Library

This library controls how users sign-in and access services. We recommend you always take the latest version of our library in your app when possible. We use semantic versioning so you can control the risk associated with updating your app. As an example, always downloading the latest minor version number (e.g. x. y.x) ensures you get the latest security and feature enhanements but our API surface remains the same. You can always see the latest version and release notes under the Releases tab of GitHub.

Security Reporting

If you find a security issue with our libraries or services please report it to [email protected] with as much detail as possible. Your submission may be eligible for a bounty through the Microsoft Bounty program. Please do not post security issues to GitHub Issues or any other public site. We will contact you shortly upon receiving the information. We encourage you to get notifications of when security incidents occur by visiting this page and subscribing to Security Advisory Alerts.

Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License (the "License");

Using MSAL

Requirements

Step 1: Configure the AndroidManifest.xml

  1. Give your app Internet permissions
 <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  1. Configure your Intent filter, make sure you add your App/Client ID
 <!--Intent filter to capture System Browser calling back to our app after Sign In-->
  <activity

android:name="com.microsoft.identity.client.BrowserTabActivity">

<intent-filter>

 <action android:name="android.intent.action.VIEW" />

 <category android:name="android.intent.category.DEFAULT" />

 <category android:name="android.intent.category.BROWSABLE" />

 <data android:scheme="msal<YOUR_CLIENT_ID>"

  android:host="auth" />

</intent-filter>
  </activity>

Step 2: Instantiate MSAL and Acquire a Token

  1. Create a new PublicClientApplication instance. Make sure to fill in your app/client id
 PublicClientApplication myApp = new PublicClientApplication(

this.getApplicationContext(),

CLIENT_ID);
  1. Acquire a token
 myApp.acquireToken(this, "User.Read", getAuthInteractiveCallback());

Step 3: Configure the Auth helpers

  1. Create an onActivityResult method
 /* Handles the redirect from the System Browser */
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

sampleApp.handleInteractiveRequestRedirect(requestCode, resultCode, data);

  
}
  1. Create the getAuthInteractiveCallback method
 private AuthenticationCallback getAuthInteractiveCallback() {

return new AuthenticationCallback() {

 @Override

 public void onSuccess(AuthenticationResult authenticationResult) {

  /* Successfully got a token, use it to call a protected resource */

String accessToken = authenticationResult.getAccessToken();

 
}

 @Override

 public void onError(MsalException exception) {

/* Failed to acquireToken */

if (exception instanceof MsalClientException) {

/* Exception inside MSAL, more info inside MsalError.java */

  
}
 else if (exception instanceof MsalServiceException) {

/* Exception when communicating with the STS, likely config issue */

  
}

 
}

 @Override

 public void onCancel() {

  /* User canceled the authentication */

 
}

}
;
  
}

Step 4: Use the token!

The access token can now be used in an HTTP Bearer request.

Resources

UltimateRecyclerView is a RecyclerView (advanced and flexible version of ListView) with pulling to refresh, loading more, swiping to dismiss, drag and drop, animations, and many other features.

Android Shadow Layout.

Android version of Windows Phone Datepicker.

SlideSwitch is a button that you can slide on or off.

A preferences framework for Android Wear apps. Equivalent to Android's PreferenceActivity, but for Android Wear.

A delivery service for Android Wear. Courier uses Wearable.DataApi and Wearable.MessageApi to deliver objects between devices simply and cleanly.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes