Skip to content

Instantly share code, notes, and snippets.

@RobertApikyan
Created May 24, 2023 11:54
Show Gist options
  • Save RobertApikyan/e56522acb29788e650c291df87a43014 to your computer and use it in GitHub Desktop.
Save RobertApikyan/e56522acb29788e650c291df87a43014 to your computer and use it in GitHub Desktop.
//
// 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