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
package global.raiser.app.plugins.msal | |
import android.app.Activity | |
import com.microsoft.identity.client.AuthenticationCallback | |
import com.microsoft.identity.client.IAuthenticationResult | |
import com.microsoft.identity.client.IPublicClientApplication | |
import com.microsoft.identity.client.PublicClientApplication | |
import com.microsoft.identity.client.exception.MsalException | |
import global.raiser.app.R | |
import io.flutter.embedding.engine.plugins.FlutterPlugin | |
import io.flutter.plugin.common.MethodCall | |
import io.flutter.plugin.common.MethodChannel | |
class MsalPlugin(private val activity: Activity) : FlutterPlugin, MethodChannel.MethodCallHandler { | |
companion object { | |
const val NAME = "global.raiser/msal/methods" | |
const val CANCELLATION_ERROR_CODE = "-50005" | |
private val SCOPES = arrayOf("User.Read", "email") | |
} | |
private var methodChannel: MethodChannel? = null | |
private var _client: IPublicClientApplication? = null | |
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { | |
methodChannel = MethodChannel(binding.binaryMessenger, NAME) | |
methodChannel?.setMethodCallHandler(this) | |
} | |
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { | |
methodChannel?.setMethodCallHandler(null) | |
methodChannel = null | |
} | |
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { | |
when (call.method) { | |
"login" -> login(result) | |
} | |
} | |
private fun login(result: MethodChannel.Result) { | |
getMsalClient({ client -> | |
client.acquireToken(activity, SCOPES, object : AuthenticationCallback { | |
override fun onSuccess(authenticationResult: IAuthenticationResult) { | |
val data: Map<*, *> = mapOf("accessToken" to authenticationResult.accessToken, | |
"authenticationScheme" to authenticationResult.authenticationScheme, | |
"idToken" to authenticationResult.account.idToken) | |
result.success(data) | |
} | |
override fun onCancel() { | |
result.error(CANCELLATION_ERROR_CODE, "onCancel", null) | |
} | |
override fun onError(exception: MsalException) { | |
result.error(exception.errorCode, exception.message, exception.exceptionName) | |
} | |
}) | |
}, { exception -> | |
result.error(exception.errorCode, exception.message, exception.exceptionName) | |
}) | |
} | |
private fun getMsalClient(onCreated: (IPublicClientApplication) -> Unit, onError: (MsalException) -> Unit) { | |
_client?.let { | |
onCreated(it) | |
return | |
} | |
PublicClientApplication.create(activity, R.raw.auth_config_single_account, | |
object : IPublicClientApplication.ApplicationCreatedListener { | |
override fun onCreated(application: IPublicClientApplication) { | |
onCreated(application) | |
} | |
override fun onError(exception: MsalException) { | |
onError(exception) | |
} | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment