Coder Social home page Coder Social logo

ru-alxr / microsoft-authentication-library-for-android Goto Github PK

View Code? Open in Web Editor NEW

This project forked from azuread/microsoft-authentication-library-for-android

0.0 1.0 0.0 2.83 MB

Microsoft Authentication Library (MSAL) Preview for Android

Home Page: http://aka.ms/aadv2

License: MIT License

Java 100.00%

microsoft-authentication-library-for-android's Introduction

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

Version Badge Build Status

Important Note about the MSAL Preview

This library can be used 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 (GA) release we will require you to update to the GA version within 6 months. Applications written using a preview version of library may no longer work.

Example

    // Instantiates MSAL Public Client App
    PublicClientApplication myApp = new PublicClientApplication(
                    this.getApplicationContext(),
                    R.raw.auth_config);

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

    // ...

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

For a full example, checkout the code sample.

Installation

Binaries via Gradle (Recommended way)

Add to your app's build.gradle:

    dependencies {
        implementation 'com.microsoft.identity.client:msal:0.2.+'
        }
    }

AAR package inside libs folder

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

Community Help and Support

We use StackOverflow with the community to provide support. You should browse existing issues to see if someone has asked about your issue before. If there are workable solutions to your issue then try out those solutions. If not, ask your question and let the community help you out. We're part of the community too and watch for new questions. We help with answers when the community cannot give you a solution.

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

Contribute

We enthusiastically welcome contributions and feedback. You should 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 you can. We use semantic versioning so you can control the risk of updating your app. For example, always downloading the latest minor version number (e.g. x.y.x) ensures you get the latest security and feature enhanements with the assurance that our API surface area has not changed. 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 the issue to [email protected] with as much detail as you can provide. 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 after receiving your issue report. We encourage you to get new security incident notifications by visiting Microsoft technical security notifications to subscribe to Security Advisory Alerts.

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

Using MSAL

  • Make sure you've included MSAL in your app's build.gradle.
  • Before you can get a token from Azure AD v2.0 or Azure AD B2C, you'll need to register an application. To register your app, use the Azure portal. For Azure AD B2C, checkout how to register your app with B2C.

Requirements

  • Android 21+

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(),
                    R.raw.auth_config);
  1. Acquire a token
    myApp.acquireToken(this, SCOPES, 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 to Call an API).

microsoft-authentication-library-for-android's People

Contributors

brandwe avatar danieldobalian avatar guperrot avatar heidijinxujia avatar iambmelt avatar imablanco avatar jaredsburrows avatar johnmaustin78 avatar kreedula avatar nazukj avatar rpdome avatar sangonzal avatar shoatman avatar titorodi avatar weijjia avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.