Skip to content

Commit

Permalink
Add required scopes to token and renewAuth requests
Browse files Browse the repository at this point in the history
  • Loading branch information
poovamraj committed May 2, 2023
1 parent 9da22b9 commit 0f1a3cd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe
* @return a request to start
*/
public fun renewAuth(refreshToken: String): Request<Credentials, AuthenticationException> {
val parameters = ParameterBuilder.newBuilder()
val parameters = ParameterBuilder.newBuilderWithRequiredScope()
.setClientId(clientId)
.setRefreshToken(refreshToken)
.setGrantType(ParameterBuilder.GRANT_TYPE_REFRESH_TOKEN)
Expand Down Expand Up @@ -690,7 +690,7 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe
codeVerifier: String,
redirectUri: String
): Request<Credentials, AuthenticationException> {
val parameters = ParameterBuilder.newBuilder()
val parameters = ParameterBuilder.newBuilderWithRequiredScope()
.setClientId(clientId)
.setGrantType(ParameterBuilder.GRANT_TYPE_AUTHORIZATION_CODE)
.set(OAUTH_CODE_KEY, authorizationCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ public class ParameterBuilder private constructor(parameters: Map<String, String
.setScope(OidcUtils.DEFAULT_SCOPE)
}

@JvmStatic
public fun newBuilderWithRequiredScope(): ParameterBuilder {
return newBuilder()
.setScope(OidcUtils.REQUIRED_SCOPE)
}

/**
* Creates a new instance of the builder.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.auth0.android.request.internal

import androidx.annotation.VisibleForTesting
import java.util.*

/**
Expand All @@ -8,7 +9,8 @@ import java.util.*
internal object OidcUtils {
internal const val KEY_SCOPE = "scope"
internal const val DEFAULT_SCOPE = "openid profile email"
private const val REQUIRED_SCOPE = "openid"
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal const val REQUIRED_SCOPE = "openid"

/**
* Given a string, it will check if it contains the scope of "openid".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.auth0.android.request.HttpMethod
import com.auth0.android.request.NetworkingClient
import com.auth0.android.request.RequestOptions
import com.auth0.android.request.ServerResponse
import com.auth0.android.request.internal.OidcUtils
import com.auth0.android.request.internal.RequestFactory
import com.auth0.android.request.internal.ThreadSwitcherShadow
import com.auth0.android.result.*
Expand Down Expand Up @@ -2200,7 +2201,7 @@ public class AuthenticationAPIClientTest {
)
assertThat(request.path, Matchers.equalTo("/oauth/token"))
val body = bodyFromRequest<String>(request)
assertThat(body, Matchers.not(Matchers.hasKey("scope")))
assertThat(body, Matchers.hasEntry("scope", OidcUtils.REQUIRED_SCOPE))
assertThat(body, Matchers.hasEntry("client_id", CLIENT_ID))
assertThat(body, Matchers.hasEntry("refresh_token", "refreshToken"))
assertThat(body, Matchers.hasEntry("grant_type", "refresh_token"))
Expand Down Expand Up @@ -2229,7 +2230,7 @@ public class AuthenticationAPIClientTest {
assertThat(body, Matchers.hasEntry("client_id", CLIENT_ID))
assertThat(body, Matchers.hasEntry("refresh_token", "refreshToken"))
assertThat(body, Matchers.hasEntry("grant_type", "refresh_token"))
assertThat(body, Matchers.not(Matchers.hasKey("scope")))
assertThat(body, Matchers.hasEntry("scope", OidcUtils.REQUIRED_SCOPE))
assertThat(credentials, Matchers.`is`(Matchers.notNullValue()))
}

Expand All @@ -2252,7 +2253,7 @@ public class AuthenticationAPIClientTest {
assertThat(body, Matchers.hasEntry("client_id", CLIENT_ID))
assertThat(body, Matchers.hasEntry("refresh_token", "refreshToken"))
assertThat(body, Matchers.hasEntry("grant_type", "refresh_token"))
assertThat(body, Matchers.not(Matchers.hasKey("scope")))
assertThat(body, Matchers.hasEntry("scope", OidcUtils.REQUIRED_SCOPE))
assertThat(credentials, Matchers.`is`(Matchers.notNullValue()))
}

Expand Down Expand Up @@ -2363,6 +2364,7 @@ public class AuthenticationAPIClientTest {
assertThat(body, Matchers.hasEntry("code", "code"))
assertThat(body, Matchers.hasEntry("code_verifier", "codeVerifier"))
assertThat(body, Matchers.hasEntry("redirect_uri", "http://redirect.uri"))
assertThat(body, Matchers.hasEntry("scope", OidcUtils.REQUIRED_SCOPE))
assertThat(
callback, AuthenticationCallbackMatcher.hasPayloadOfType(
Credentials::class.java
Expand All @@ -2388,6 +2390,7 @@ public class AuthenticationAPIClientTest {
assertThat(body, Matchers.hasEntry("code", "code"))
assertThat(body, Matchers.hasEntry("code_verifier", "codeVerifier"))
assertThat(body, Matchers.hasEntry("redirect_uri", "http://redirect.uri"))
assertThat(body, Matchers.hasEntry("scope", OidcUtils.REQUIRED_SCOPE))
assertThat(
callback, AuthenticationCallbackMatcher.hasError(
Credentials::class.java
Expand Down

0 comments on commit 0f1a3cd

Please sign in to comment.