This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PlatformMethodHandler.swift | |
// Runner | |
// | |
// Created by Tigran Kirakosyan on 11/23/20. | |
// Copyright © 2020 The Chromium Authors. All rights reserved. | |
// | |
import Flutter | |
import MSAL | |
class PlatformMethodHandler { | |
var controller: FlutterViewController | |
var notificationData: [AnyHashable : Any]? | |
init(controller: FlutterViewController) { | |
self.controller = controller | |
} | |
func msLogin(result: @escaping FlutterResult) { | |
let kClientID = "300aafe3-a0bb-4d29-b497-683c16c87ece" | |
let kAuthority = "https://login.microsoftonline.com/organizations" | |
let kRedirectUri = "msauth.global.raiser://auth" | |
let kScopes: [String] = ["user.read","email"] | |
var applicationContext : MSALPublicClientApplication? | |
var webViewParamaters : MSALWebviewParameters? | |
guard let authorityURL = URL(string: kAuthority) else { | |
result(FlutterError(code: "FAIL", message: "Unable to create authority URL", details: nil)) | |
return | |
} | |
let authority = try? MSALAADAuthority(url: authorityURL) | |
let msalConfiguration = MSALPublicClientApplicationConfig(clientId: kClientID, | |
redirectUri: kRedirectUri, | |
authority: authority) | |
applicationContext = try? MSALPublicClientApplication(configuration: msalConfiguration) | |
webViewParamaters = MSALWebviewParameters(authPresentationViewController: controller) | |
guard let appContext = applicationContext else { | |
result(FlutterError(code: "FAIL", message: "Unable to create applicationContext", details: nil)) | |
return | |
} | |
guard let webViewParams = webViewParamaters else { | |
result(FlutterError(code: "FAIL", message: "Unable to create webViewParamaters", details: nil)) | |
return | |
} | |
let parameters = MSALInteractiveTokenParameters(scopes: kScopes, webviewParameters: webViewParams) | |
parameters.promptType = .selectAccount | |
appContext.acquireToken(with: parameters) { (res, error) in | |
if let error = error { | |
let msalError = error as NSError | |
if msalError.code == MSALError.userCanceled.rawValue { | |
result(FlutterError(code: "-50005", message: "User cancel the operation", details: nil)) | |
return | |
} | |
result(FlutterError(code: "FAIL", message: "Could not acquire token: \(error)", details: nil)) | |
return | |
} | |
guard let res = res else { | |
result(FlutterError(code: "FAIL", message: "Could not acquire token: No result returned", details: nil)) | |
return | |
} | |
let retResult: [String : String] = ["accessToken" : res.accessToken, "authenticationScheme": "Bearer", "idToken" : res.idToken!] | |
result(retResult) | |
} | |
} | |
func getNotificationData(result: FlutterResult) { | |
if let delegate = UIApplication.shared.delegate as? AppDelegate { | |
delegate.isKilled = false | |
} | |
result(notificationData) | |
} | |
func getFirebaseData(result: FlutterResult, deviceToken: String, fcmToken: String) { | |
result(["deviceToken" : deviceToken, "fcmToken" : fcmToken]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment