react-native-msal-plugin
Wrapper around microsoft-authentication-library-for-objc library and microsoft-authentication-library-for-android
Tested on React Native 0.57.1
Based on bjartebore repo
npm install react-native-msal-plugin
or
yarn add react-native-msal-plugin
Link the library
react-native link react-native-msal-plugin
IOS Setup
Requirements
Install the required Pod
Install microsoft-authentication-library-for-objc with cocoapods
Create a Podfile in the ios project and add the following
platform :ios, '10' target 'msalExample' do # Pods for msalExample pod 'MSAL', '~> 0.2'end
Open Terminal in the same directory as the Podfile and run pod install
Add Url Scheme
Open the info.plist and add a url scheme that contains the callback url.
Make sure to replace [REPLACE_WITH_YOUR_APPLICATION_ID] with your own application id
CFBundleURLTypes CFBundleTypeRole Editor CFBundleURLName $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleURLSchemes msal[REPLACE_WITH_YOUR_APPLICATION_ID]
Handle the redirection of the browser, Open the AppDelegate.m file and import msal.h
Then add this method
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options @end
The msal library uses ASWebAuthenticationsession for authentication on ios12 so you will have to add a new keychain group. to find out more read the docs https://github.com/AzureAD/microsoft-authentication-library-for-objc
Android Setup
Add Browser tab activity to your AndroidManifest.xml make sure to replace [REPLACE_WITH_YOUR_APPLICATION_ID] with your own application id
<!-- Browser tab activity -->
Usage
Common endpoint
; const authority = "https://login.microsoftonline.com/common"; const clientId = "ad04905f-6060-4bb0-9372-958afdb68574"; const scopes = "User.Read"; const extraQueryParameters = exampleParamOne: "exampleParamOneValue" exampleParamTwo: "exampleParamTwoValue"; const login_hint = "user@domain.com"; const authClient = authority clientId; const forceTokenRefresh = false; let tokenResult = {}; // acquire tokentry tokenResult = await thisauthClient; console; catch error console; // acquire token silenttry const silentTokenresult = await thisauthClient; console; catch error console; // sign outtry await thisauthClient; catch error console;
Azure B2C endpoint
; const authority = "https://{domain}.b2clogin.com/tfp/{domain}.onmicrosoft.com"; const applicationId = "{applicationId}"; const policies = signUpSignInPolicy: "B2C_1_signup-signin-policy" passwordResetPolicy: "B2C_1_Password-reset-policy"; const scopes = "https://{domain}.onmicrosoft.com/{app id}/user_impersonation"; const extraQueryParameters = exampleParamOne: "exampleParamOneValue" exampleParamTwo: "exampleParamTwoValue"; const login_hint = "user@domain.com"; const authClient = authority applicationId policies; const forceTokenRefresh = false; let tokenResult = {}; // acquire Tokentry tokenResult = await thismsalPlugin; console; catch error console; // acquire Token Silenttry const silentTokenresult = await thismsalPlugin; console; catch error console; // sign outtry await thisauthClient; catch error console;