Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
Migrate to MSAL 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Woodruff committed Feb 10, 2020
1 parent c180178 commit c045bb1
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 64 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 1.1.0
* Updated to MSAL 1.2.0+
## 1.0.0+2
* Updates to readme in regards to kotlin static field issues.
## 1.0.0+1
Expand Down
8 changes: 4 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ group 'uk.co.moodio.msal_flutter'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.3.30'
ext.kotlin_version = '1.3.61'
repositories {
google()
jcenter()
Expand Down Expand Up @@ -42,6 +42,6 @@ android {
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.microsoft.identity.client:msal:1.0.+'
}
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${kotlin_version}"
implementation 'com.microsoft.identity.client:msal:1.2.+'
}
2 changes: 1 addition & 1 deletion android/settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = 'msal_flutter'
rootProject.name = 'msal_flutter'
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@ class MsalFlutterPlugin: MethodCallHandler {
val scopes: Array<String>? = scopesArg?.toTypedArray()
val clientId : String? = call.argument("clientId")
val authority : String? = call.argument("authority")
val redirectUri : String? = call.argument("redirectUri");

Log.d("MsalFlutter","Got scopes: $scopes")
Log.d("MsalFlutter","Got cleintId: $clientId")
Log.d("MsalFlutter","Got clientId: $clientId")
Log.d("MsalFlutter","Got authority: $authority")

when(call.method){
"logout" -> Thread(Runnable{logout(result)}).start()
"initialize" -> initialize(clientId, authority, result)
"initialize" -> initialize(clientId, authority, redirectUri.orEmpty(), result)
"acquireToken" -> Thread(Runnable {acquireToken(scopes, result)}).start()
"acquireTokenSilent" -> Thread(Runnable {acquireTokenSilent(scopes, result)}).start()
else -> result.notImplemented()
Expand Down Expand Up @@ -168,7 +169,7 @@ class MsalFlutterPlugin: MethodCallHandler {
}
}

private fun initialize(clientId: String?, authority: String?, result: Result)
private fun initialize(clientId: String?, authority: String?, redirectUri: String, result: Result)
{
//ensure clientid provided
if(clientId == null){
Expand All @@ -177,6 +178,13 @@ class MsalFlutterPlugin: MethodCallHandler {
return
}

//ensure redirectUri provided
if (redirectUri.isEmpty()){
Log.d("MsalFlutter","error no redirectUri")
result.error("NO_REDIRECTURI", "Call must include a redirectUri", null)
return
}

//if already initialized, ensure clientid hasn't changed
if(isClientInitialized()){
Log.d("MsalFlutter","Client already initialized.")
Expand All @@ -192,11 +200,10 @@ class MsalFlutterPlugin: MethodCallHandler {
if(authority != null){
Log.d("MsalFlutter", "Authority not null")
Log.d("MsalFlutter", "Creating with: $clientId - $authority")
PublicClientApplication.create(mainActivity.applicationContext, clientId, authority, getApplicationCreatedListener(result))
}else{
Log.d("MsalFlutter", "Authority null")
PublicClientApplication.create(mainActivity.applicationContext, clientId, getApplicationCreatedListener(result))
}
PublicClientApplication.create(mainActivity.applicationContext, clientId, null, redirectUri, getApplicationCreatedListener(result));
}


Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
10 changes: 0 additions & 10 deletions example/ios/Flutter/flutter_export_environment.sh

This file was deleted.

4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PODS:
- MSAL (1.0.3):
- MSAL/app-lib (= 1.0.3)
- MSAL/app-lib (1.0.3)
- msal_flutter (1.0.0):
- msal_flutter (1.1.0):
- Flutter
- MSAL (~> 1.0.3)

Expand All @@ -24,7 +24,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
MSAL: 351711995bd31170a2e887941115a3ed17b178e8
msal_flutter: 72ba12187cce21c6a39282189431964c71033c80
msal_flutter: b44608232bd7269959c4b5770afdd103eff12617

PODFILE CHECKSUM: 58760ca3c5878cb71ae11bd6eeff33fd4ce06e4f

Expand Down
15 changes: 8 additions & 7 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {

static const String _authority = "https://msalfluttertest.b2clogin.com/tfp/msalfluttertest.onmicrosoft.com/B2C_1_sisu";
static const String _clientId = "5913dfb1-7576-451c-a7ea-a7c5a3f8682a";

static const String _redirectUrl = "msauth://com.example.msal/PbN3nCasHqLUVarghPLaQWerTYU%3D";

String _output = 'NONE';

PublicClientApplication pca;

Future<void> _acquireToken() async{
if(pca == null){
pca = await PublicClientApplication.createPublicClientApplication(_clientId, authority: _authority);
pca = await PublicClientApplication.createPublicClientApplication(_clientId, _redirectUrl, authority: _authority);
}

String res;
Expand All @@ -47,9 +48,9 @@ class _MyAppState extends State<MyApp> {

Future<void> _acquireTokenSilently() async {
if(pca == null){
pca = await PublicClientApplication.createPublicClientApplication(_clientId, authority: _authority);
pca = await PublicClientApplication.createPublicClientApplication(_clientId, _redirectUrl, authority: _authority);
}

String res;
try
{
Expand All @@ -74,7 +75,7 @@ class _MyAppState extends State<MyApp> {
Future _logout() async {
print("called logout");
if(pca == null){
pca = await PublicClientApplication.createPublicClientApplication(_clientId, authority: _authority);
pca = await PublicClientApplication.createPublicClientApplication(_clientId, _redirectUrl, authority: _authority);
}

print("pca is not null");
Expand Down Expand Up @@ -104,7 +105,7 @@ class _MyAppState extends State<MyApp> {
body: Center(
child: Column(
children: <Widget>[
RaisedButton( onPressed: _acquireToken,
RaisedButton( onPressed: _acquireToken,
child: Text('AcquireToken()'),),
RaisedButton( onPressed: _acquireTokenSilently,
child: Text('AcquireTokenSilently()')),
Expand Down
48 changes: 26 additions & 22 deletions ios/Classes/SwiftMsalFlutterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,34 @@ import UIKit
import MSAL

public class SwiftMsalFlutterPlugin: NSObject, FlutterPlugin {

//static fields as initialization isn't really required
static var clientId : String = ""
static var redirectUri : String? = ""
static var authority : String = ""

public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "msal_flutter", binaryMessenger: registrar.messenger())
let instance = SwiftMsalFlutterPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult)
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult)
{
//get the arguments as a dictionary
let dict = call.arguments! as! NSDictionary
let scopes = dict["scopes"] as? [String] ?? [String]()
let clientId = dict["clientId"] as? String ?? ""
let redirectUri = dict["redirectUri"] as? String ?? ""
let authority = dict["authority"] as? String ?? ""

switch( call.method ){
case "initialize": initialize(clientId: clientId, authority: authority, result: result)
case "initialize": initialize(clientId: clientId, redirectUri: redirectUri, authority: authority, result: result)
case "acquireToken": acquireToken(scopes: scopes, result: result)
case "acquireTokenSilent": acquireTokenSilent(scopes: scopes, result: result)
case "logout": logout(result: result)
default: result(FlutterError(code:"INVALID_METHOD", message: "The method called is invalid", details: nil))
}
}
}


Expand All @@ -52,7 +54,7 @@ public class SwiftMsalFlutterPlugin: NSObject, FlutterPlugin {
result(FlutterError(code: "AUTH_ERROR", message: "Authentication error", details: error!.localizedDescription))
return
}

// Get access token from result
let accessToken = authResult.accessToken

Expand All @@ -68,7 +70,7 @@ public class SwiftMsalFlutterPlugin: NSObject, FlutterPlugin {
{
if let application = getApplication(result: result){
var account : MSALAccount!

do{
let cachedAccounts = try application.allAccounts()
if cachedAccounts.isEmpty {
Expand All @@ -78,20 +80,20 @@ public class SwiftMsalFlutterPlugin: NSObject, FlutterPlugin {
}
//set account as the first account
account = cachedAccounts.first!
}
}
catch{
result(FlutterError(code: "NO_ACCOUNT", message: "Error retrieving an existing account", details: nil))
}

let silentParameters = MSALSilentTokenParameters(scopes: scopes, account: account)

application.acquireTokenSilent(with: silentParameters, completionBlock: { (msalresult, error) in

guard let authResult = msalresult, error == nil else {
result(FlutterError(code: "AUTH_ERROR", message: "Authentication error", details: nil))
return
}

// Get access token from result
let accessToken = authResult.accessToken

Expand All @@ -114,7 +116,7 @@ public class SwiftMsalFlutterPlugin: NSObject, FlutterPlugin {

//setup the config, using authority if it is set, or defaulting to msal's own implementation if it's not
if !SwiftMsalFlutterPlugin.authority.isEmpty
{
{
//try creating the msal aad authority object
do{
//create authority url
Expand All @@ -125,7 +127,8 @@ public class SwiftMsalFlutterPlugin: NSObject, FlutterPlugin {

//create the msal authority and configuration
let msalAuthority = try MSALAuthority(url: authorityUrl)
config = MSALPublicClientApplicationConfig(clientId: SwiftMsalFlutterPlugin.clientId, redirectUri: nil, authority: msalAuthority)
config = MSALPublicClientApplicationConfig(clientId: SwiftMsalFlutterPlugin.clientId, redirectUri: SwiftMsalFlutterPlugin.redirectUri, authority: msalAuthority)
config.knownAuthorities = [msalAuthority];
} catch {
//return error if exception occurs
result(FlutterError(code: "INVALID_AUTHORITY", message: "invalid authority", details: nil))
Expand All @@ -134,21 +137,21 @@ public class SwiftMsalFlutterPlugin: NSObject, FlutterPlugin {
}
else
{
config = MSALPublicClientApplicationConfig(clientId: SwiftMsalFlutterPlugin.clientId)
config = MSALPublicClientApplicationConfig(clientId: SwiftMsalFlutterPlugin.clientId, redirectUri: SwiftMsalFlutterPlugin.redirectUri, authority: nil);
}

//create the application and return it
if let application = try? MSALPublicClientApplication(configuration: config)
do
{
application.validateAuthority = false
return application
}else{
result(FlutterError(code: "CONFIG_ERROR", message: "Unable to create MSALPublicClientApplication", details: nil))
return nil
let application = try MSALPublicClientApplication(configuration: config)
return application
} catch {
result(FlutterError(code: "CONFIG_ERROR", message: "Unable to create MSALPublicClientApplication", details: error))
return nil
}
}

private func initialize(clientId: String, authority: String, result: @escaping FlutterResult)
private func initialize(clientId: String, redirectUri: String, authority: String, result: @escaping FlutterResult)
{
//validate clientid exists
if(clientId.isEmpty){
Expand All @@ -157,6 +160,7 @@ public class SwiftMsalFlutterPlugin: NSObject, FlutterPlugin {
}

SwiftMsalFlutterPlugin.clientId = clientId;
SwiftMsalFlutterPlugin.redirectUri = redirectUri;
SwiftMsalFlutterPlugin.authority = authority;
result(true)
}
Expand All @@ -174,7 +178,7 @@ public class SwiftMsalFlutterPlugin: NSObject, FlutterPlugin {

let account = cachedAccounts.first!
try application.remove(account)
}
}
catch {
result(FlutterError(code: "CONFIG_ERROR", message: "Unable get remove accounts", details: nil))
return
Expand Down
6 changes: 3 additions & 3 deletions ios/msal_flutter.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#
Pod::Spec.new do |s|
s.name = 'msal_flutter'
s.version = '1.0.0'
s.version = '1.1.0'
s.summary = 'MSAL Flutter Wrapper'
s.description = <<-DESC
A new flutter plugin project.
A flutter wrapper for MSAL.
DESC
s.homepage = 'https://www.moodio.co.uk'
s.license = { :file => '../LICENSE' }
Expand All @@ -18,6 +18,6 @@ A new flutter plugin project.
s.dependency 'MSAL', '~> 1.0.3'

s.swift_version = '4.0'
s.ios.deployment_target = '10.0'
s.ios.deployment_target = '12.4'
end

19 changes: 13 additions & 6 deletions lib/src/public_client_application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'msal_exception.dart';
class PublicClientApplication {
static const MethodChannel _channel = const MethodChannel('msal_flutter');

String _clientId, _authority;
String _clientId, _authority, _redirectUri;

/// Create a new PublicClientApplication authenticating as the given [clientId],
/// optionally against the selected [authority], defaulting to the common
Expand All @@ -15,15 +15,17 @@ class PublicClientApplication {
"Direct call is no longer supported in v1.0, please use static method createPublicClientApplication");
}

PublicClientApplication._create(String clientId, {String authority}) {
PublicClientApplication._create(String clientId, String redirectUri, {String authority}) {
_clientId = clientId;
_authority = authority;
_redirectUri = redirectUri;
}

static Future<PublicClientApplication> createPublicClientApplication(
String clientId,
String redirectUri,
{String authority}) async {
var res = PublicClientApplication._create(clientId, authority: authority);
var res = PublicClientApplication._create(clientId, redirectUri, authority: authority);
await res._initialize();
return res;
}
Expand Down Expand Up @@ -74,7 +76,9 @@ class PublicClientApplication {
case "NO_ACCOUNT":
return MsalNoAccountException();
case "NO_CLIENTID":
return MsalInvalidConfigurationException("Client Id not set");
return MsalInvalidConfigurationException("Client Id not set.");
case "NO_REDIRECTURI":
return MsalInvalidConfigurationException("Redirect URI is not set.");
case "INVALID_AUTHORITY":
return MsalInvalidConfigurationException("Invalid authroity set.");
case "CONFIG_ERROR":
Expand All @@ -94,8 +98,11 @@ class PublicClientApplication {

//initialize the main client platform side
Future _initialize() async {
var res = <String, dynamic>{'clientId': this._clientId};
//if authority has been set, add it aswell
var res = <String, dynamic>{
'clientId': this._clientId,
'redirectUri': this._redirectUri
};
//if authority has been set, add it as well
if (this._authority != null) {
res["authority"] = this._authority;
}
Expand Down
Loading

0 comments on commit c045bb1

Please sign in to comment.