Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth): Add "offline_access" scope for long-lived refresh tokens #1943

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions api/v1/client/src/commonMain/kotlin/auth/AuthService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@ class AuthService(
private val clientId: String
) {
/**
* Generate a token for the given [username] and [password].
* Generate a token for the given [username] and [password] using password grant type with optional [scopes].
*/
suspend fun generateToken(username: String, password: String): TokenInfo =
suspend fun generateToken(username: String, password: String, scopes: Set<String> = emptySet()): TokenInfo =
wkl3nk marked this conversation as resolved.
Show resolved Hide resolved
client.submitForm(
url = tokenUrl,
formParameters = Parameters.build {
append("client_id", clientId)
append("username", username)
append("password", password)
append("grant_type", "password")
if (scopes.isNotEmpty()) {
append("scope", scopes.joinToString(" "))
}
}
).let { response ->
if (!response.status.isSuccess()) {
Expand All @@ -57,15 +60,18 @@ class AuthService(
}

/**
* Refresh the token for the given [refreshToken].
* Refresh the tokens for the given [refreshToken] with optional [scopes].
*/
suspend fun refreshToken(refreshToken: String): TokenInfo =
suspend fun refreshToken(refreshToken: String, scopes: Set<String> = emptySet()): TokenInfo =
client.submitForm(
url = tokenUrl,
formParameters = Parameters.build {
append("client_id", clientId)
append("refresh_token", refreshToken)
append("grant_type", "refresh_token")
if (scopes.isNotEmpty()) {
append("scope", scopes.joinToString(" "))
}
}
).let { response ->
if (!response.status.isSuccess()) {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main/kotlin/LoginCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class LoginCommand : SuspendingCliktCommand(name = "login") {
clientId = clientId
)

val tokenInfo = authService.generateToken(username, password)
val tokenInfo = authService.generateToken(username, password, setOf("offline_access"))

AuthenticationStorage.store(
HostAuthenticationDetails(
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main/kotlin/utils/AuthenticationUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private fun createOrtServerClient(authDetails: HostAuthenticationDetails): OrtSe
clientId = authDetails.clientId
)

auth.refreshToken(authDetails.tokens.refresh).also {
auth.refreshToken(authDetails.tokens.refresh, setOf("offline_access")).also {
val updatedAuthDetails = authDetails.copy(
tokens = Tokens(it.accessToken, it.refreshToken)
)
Expand Down
4 changes: 3 additions & 1 deletion cli/src/test/kotlin/AuthLoginCommandTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class AuthLoginCommandTest : StringSpec({
"Auth login command" should {
"store the authentication information in a local file" {
mockkConstructor(AuthService::class)
coEvery { anyConstructed<AuthService>().generateToken("testUser", "testPassword") } returns TokenInfo(
coEvery {
anyConstructed<AuthService>().generateToken("testUser", "testPassword", setOf("offline_access"))
} returns TokenInfo(
accessToken = "testAccessToken",
refreshToken = "testRefreshToken",
expiresInSeconds = 3600
Expand Down
Loading