Skip to content

Commit

Permalink
Throw error on empty scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
SaurabhMSFT committed May 13, 2024
1 parent 5f601a0 commit 443b48e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import com.microsoft.identity.common.internal.controllers.LocalMSALController
import com.microsoft.identity.common.java.AuthenticationConstants
import com.microsoft.identity.common.java.commands.CommandCallback
import com.microsoft.identity.common.java.commands.SilentTokenCommand
import com.microsoft.identity.common.java.controllers.BaseController
import com.microsoft.identity.common.java.controllers.CommandDispatcher
import com.microsoft.identity.common.java.controllers.ExceptionAdapter
import com.microsoft.identity.common.java.dto.AccountRecord
Expand Down Expand Up @@ -204,7 +203,6 @@ class AccountState private constructor(
* @throws [MsalClientException] If the the account doesn't exist in the cache.
* @throws [ServiceException] If the refresh token doesn't exist in the cache/is expired, or the refreshing fails.
*/
@Deprecated("Use the getAccessToken(forceRefresh: Boolean = false, scopes: List<String>, callback: GetAccessTokenCallback) method")
fun getAccessToken(forceRefresh: Boolean = false, callback: GetAccessTokenCallback) {
LogSession.logMethodCall(
tag = TAG,
Expand All @@ -229,7 +227,6 @@ class AccountState private constructor(
*
* @return [com.microsoft.identity.nativeauth.statemachine.results.GetAccessTokenResult] The result of the getAccessToken action
*/
@Deprecated("Use the getAccessToken(forceRefresh: Boolean = false, scopes: List<String>) method")
suspend fun getAccessToken(forceRefresh: Boolean = false): GetAccessTokenResult {
return getAccessTokenInternal(forceRefresh, AuthenticationConstants.DEFAULT_SCOPES.toList());
}
Expand All @@ -244,8 +241,10 @@ class AccountState private constructor(
* @return [com.microsoft.identity.nativeauth.statemachine.results.GetAccessTokenResult] The result of the getAccessToken action
*/
suspend fun getAccessToken(forceRefresh: Boolean = false, scopes: List<String>): GetAccessTokenResult {
return getAccessTokenInternal(forceRefresh,
if (!scopes.isEmpty()) scopes else AuthenticationConstants.DEFAULT_SCOPES.toList() )
if (scopes.isEmpty()) {
throw MsalClientException(MsalClientException.INVALID_PARAMETER)
}
return getAccessTokenInternal(forceRefresh, scopes)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,14 +653,13 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr

assertEquals(accessToken, accessTokenTwo)

val accessTokenResultThree = (getAccountResult as GetAccountResult.AccountFound).resultValue.getAccessToken(false, emptyList())
assertTrue(accessTokenResultThree is GetAccessTokenResult.Complete)

val accessTokenThree = (accessTokenResultThree as GetAccessTokenResult.Complete).resultValue.accessToken
assertNotNull(accessTokenThree)

assertEquals(accessTokenTwo, accessTokenThree)
assertEquals(accessToken, accessTokenThree)
try {
var accessTokenState = (getAccountResult as GetAccountResult.AccountFound).resultValue.getAccessToken(false, emptyList())
} catch (exception: MsalClientException) {
assertEquals(MsalClientException.INVALID_PARAMETER, exception.errorCode)
return@runTest
}
fail()
}

/**
Expand Down Expand Up @@ -732,13 +731,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr
val signOutResult = accountState.signOut()
assertTrue(signOutResult is SignOutResult.Complete)

var accessTokenState = accountState.getAccessToken(false, ArrayList<String>(AuthenticationConstants.DEFAULT_SCOPES))
assertTrue(accessTokenState is GetAccessTokenError)
assertTrue((accessTokenState as GetAccessTokenError).isNoAccountFound())


accessTokenState = accountState.getAccessToken(false, Arrays.asList(
AuthenticationConstants.OAuth2Scopes.EMAIL_SCOPE))
val accessTokenState = accountState.getAccessToken(false, ArrayList<String>(AuthenticationConstants.DEFAULT_SCOPES))
assertTrue(accessTokenState is GetAccessTokenError)
assertTrue((accessTokenState as GetAccessTokenError).isNoAccountFound())
}
Expand Down Expand Up @@ -775,15 +768,13 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr
val signOutResult = accountState.signOut()
assertTrue(signOutResult is SignOutResult.Complete)

var accessTokenState = accountState.getAccessToken(false, emptyList())
assertTrue(accessTokenState is GetAccessTokenError)
assertTrue((accessTokenState as GetAccessTokenError).isNoAccountFound())


accessTokenState = accountState.getAccessToken(false, Arrays.asList(
AuthenticationConstants.OAuth2Scopes.EMAIL_SCOPE))
assertTrue(accessTokenState is GetAccessTokenError)
assertTrue((accessTokenState as GetAccessTokenError).isNoAccountFound())
try {
var accessTokenState = accountState.getAccessToken(false, emptyList())
} catch (exception: MsalClientException) {
assertEquals(MsalClientException.INVALID_PARAMETER, exception.errorCode)
return@runTest
}
fail()
}

/**
Expand Down

0 comments on commit 443b48e

Please sign in to comment.