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

[RFR-1046] Add logout and userId to Auth interface #301

Merged
merged 1 commit into from
May 30, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
/*
* Copyright 2023-2024 Cyface GmbH
*
* This file is part of the Cyface SDK for Android.
*
* The Cyface SDK for Android is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Cyface SDK for Android is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Cyface SDK for Android. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cyface.synchronization

import androidx.fragment.app.FragmentActivity

/**
* The mock implementation of the [Auth] interface.
*
* @author Armin Schnabel
*/
class MockAuth: Auth {
override fun performActionWithFreshTokens(action: (accessToken: String?, idToken: String?, ex: Exception?) -> Unit) {
action("eyTestToken", null, null)
}

override fun userId(): String {
return "testUserId"
}

override fun endSession(activity: FragmentActivity) {
// Nothing to do
}
}
36 changes: 36 additions & 0 deletions synchronization/src/main/kotlin/de/cyface/synchronization/Auth.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
/*
* Copyright 2023-2024 Cyface GmbH
*
* This file is part of the Cyface SDK for Android.
*
* The Cyface SDK for Android is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Cyface SDK for Android is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Cyface SDK for Android. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cyface.synchronization

import androidx.fragment.app.FragmentActivity
import net.openid.appauth.IdToken

/**
* The interface to authenticate the user and get the user id.
*
* @author Armin Schnabel
*/
interface Auth {

/**
Expand All @@ -14,4 +40,14 @@ interface Auth {
ex: Exception?
) -> Unit
)

/**
* @return The `userId` or `null` if not available.
*/
fun userId(): String?

/**
* Sends the end session request to the auth service to sign out the user.
*/
fun endSession(activity: FragmentActivity)
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ class OAuth2(context: Context, settings: SynchronizationSettings, caller: String
}
}

override fun userId(): String? {
return stateManager.current.parsedIdToken?.subject
}

@MainThread
fun performTokenRequest(
request: TokenRequest,
Expand Down Expand Up @@ -273,10 +277,7 @@ class OAuth2(context: Context, settings: SynchronizationSettings, caller: String
dispose()
}

/**
* Sends the end session request to the auth service to sign out the user.
*/
fun endSession(activity: FragmentActivity) {
override fun endSession(activity: FragmentActivity) {
val currentState = stateManager.current
val config = currentState.authorizationServiceConfiguration!!
if (config.endSessionEndpoint != null) {
Expand Down
Loading