-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df63674
commit 96468c9
Showing
13 changed files
with
211 additions
and
201 deletions.
There are no files selected for viewing
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
104 changes: 45 additions & 59 deletions
104
connector/src/main/scala/com/microsoft/kusto/spark/authentication/DeviceAuthentication.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,45 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.kusto.spark.authentication | ||
|
||
import java.util.concurrent.{CompletableFuture, TimeUnit} | ||
import java.util.function.Consumer | ||
import com.microsoft.aad.msal4j.{DeviceCode, DeviceCodeFlowParameters, IAuthenticationResult} | ||
import com.microsoft.azure.kusto.data.auth | ||
import scala.concurrent.TimeoutException | ||
|
||
class DeviceAuthentication(val cluster: String, val authority: String) | ||
extends auth.DeviceAuthTokenProvider(cluster, authority, null) { | ||
var currentDeviceCode: Option[DeviceCode] = None | ||
var expiresAt: Option[Long] = None | ||
val NewDeviceCodeFetchTimeout = 60L * 1000L | ||
var currentToken: Option[String] = None | ||
|
||
override def acquireNewAccessToken(): IAuthenticationResult = { | ||
acquireNewAccessTokenAsync().get(NewDeviceCodeFetchTimeout, TimeUnit.MILLISECONDS) | ||
} | ||
|
||
def acquireNewAccessTokenAsync(): CompletableFuture[IAuthenticationResult] = { | ||
val deviceCodeConsumer: Consumer[DeviceCode] = toJavaConsumer((deviceCode: DeviceCode) => { | ||
this.currentDeviceCode = Some(deviceCode) | ||
this.expiresAt = Some(System.currentTimeMillis + (deviceCode.expiresIn() * 1000)) | ||
println(deviceCode.message()) | ||
}) | ||
|
||
val deviceCodeFlowParams: DeviceCodeFlowParameters = | ||
DeviceCodeFlowParameters.builder(scopes, deviceCodeConsumer).build | ||
clientApplication.acquireToken(deviceCodeFlowParams) | ||
} | ||
|
||
implicit def toJavaConsumer[T](f: Function1[T, Unit]): Consumer[T] = new Consumer[T] { | ||
override def accept(t: T) = f(t) | ||
} | ||
|
||
def refreshIfNeeded(): Unit = { | ||
if (currentDeviceCode.isEmpty || expiresAt.get <= System.currentTimeMillis) { | ||
currentToken = Some(acquireAccessToken()) | ||
} | ||
} | ||
|
||
def getDeviceCodeMessage: String = { | ||
refreshIfNeeded() | ||
this.currentDeviceCode.get.message() | ||
} | ||
|
||
def getDeviceCode: DeviceCode = { | ||
refreshIfNeeded() | ||
this.currentDeviceCode.get | ||
} | ||
|
||
def acquireToken(): String = { | ||
refreshIfNeeded() | ||
currentToken.get | ||
} | ||
} | ||
//// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//// Licensed under the MIT License. | ||
// | ||
//package com.microsoft.kusto.spark.authentication | ||
// | ||
//import com.azure.core.credential.TokenRequestContext | ||
//import com.azure.identity.DeviceCodeCredentialBuilder | ||
//import com.microsoft.azure.kusto.data.auth | ||
// | ||
//import java.time.Duration | ||
//import scala.collection.convert.ImplicitConversions.`collection AsScalaIterable` | ||
// | ||
//class DeviceAuthentication(val cluster: String, val authority: String) | ||
// extends auth.DeviceAuthTokenProvider(cluster, authority, null) { | ||
// private val newDeviceCodeFetchTimeout = 60L * 1000L | ||
// private var expiresAt: Option[Long] = Some(0L) | ||
// private var currentToken: Option[String] = None | ||
// | ||
// def acquireToken(): String = { | ||
// refreshIfNeeded() | ||
// currentToken.get | ||
// } | ||
// | ||
// private def refreshIfNeeded(): Unit = { | ||
// if (isRefreshNeeded) { | ||
// val tokenCredential = | ||
// acquireNewAccessToken.getToken(new TokenRequestContext().addScopes(scopes.toSeq: _*)) | ||
// val tokenCredentialValue = | ||
// tokenCredential.blockOptional(Duration.ofMillis(newDeviceCodeFetchTimeout)) | ||
// tokenCredentialValue.ifPresent(token => { | ||
// currentToken = Some(token.getToken) | ||
// expiresAt = Some(token.getExpiresAt.toEpochSecond * 1000) | ||
// }) | ||
// } | ||
// } | ||
// | ||
// private def acquireNewAccessToken = { | ||
// val deviceCodeFlowParams = new DeviceCodeCredentialBuilder() | ||
// super.createTokenCredential(deviceCodeFlowParams) | ||
// } | ||
// | ||
// private def isRefreshNeeded: Boolean = { | ||
// expiresAt.isEmpty || expiresAt.get < System.currentTimeMillis() | ||
// } | ||
//} |
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
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
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
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
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
Oops, something went wrong.