-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from onelogin/add_signout_route
Initial code for sign out
- Loading branch information
Showing
11 changed files
with
202 additions
and
7 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
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
3 changes: 3 additions & 0 deletions
3
oneloginoidc/src/main/java/com/onelogin/oidc/logout/SignOutError.kt
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.onelogin.oidc.logout | ||
|
||
class SignOutError(message: String? = null, cause: Throwable? = null) : Exception(message, cause) |
54 changes: 54 additions & 0 deletions
54
oneloginoidc/src/main/java/com/onelogin/oidc/logout/SignOutFragment.kt
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.onelogin.oidc.logout | ||
|
||
import android.content.Intent | ||
import androidx.fragment.app.Fragment | ||
import com.onelogin.oidc.data.AuthorizationServiceProvider | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.channels.ClosedSendChannelException | ||
import net.openid.appauth.AuthorizationException | ||
import net.openid.appauth.EndSessionRequest | ||
import net.openid.appauth.EndSessionResponse | ||
import timber.log.Timber | ||
|
||
class SignOutFragment : Fragment() { | ||
|
||
internal val resultChannel = Channel<Pair<EndSessionResponse?, AuthorizationException?>>() | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
val authorizationRequestString = arguments?.getString(ARG_END_SESSION_REQUEST) | ||
val authorizationRequest = authorizationRequestString?.let { EndSessionRequest.jsonDeserialize(authorizationRequestString) } | ||
authorizationRequest?.let { | ||
val authIntent = AuthorizationServiceProvider.authorizationService.getEndSessionRequestIntent(it) | ||
startActivityForResult(authIntent, END_SESSION_REQUEST_CODE) | ||
arguments?.putString(ARG_END_SESSION_REQUEST, null) | ||
} | ||
} | ||
|
||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | ||
super.onActivityResult(requestCode, resultCode, data) | ||
if (requestCode == END_SESSION_REQUEST_CODE) { | ||
data?.let { | ||
val authorizationResponse = EndSessionResponse.fromIntent(data) | ||
val exception = AuthorizationException.fromIntent(data) | ||
try { | ||
resultChannel.offer(authorizationResponse to exception) | ||
resultChannel.close() | ||
} catch (e: ClosedSendChannelException) { | ||
Timber.d("Could not deliver logout result") | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
resultChannel.close() | ||
} | ||
|
||
companion object { | ||
internal const val END_SESSION_REQUEST_CODE = 34001 | ||
internal const val ARG_END_SESSION_REQUEST = "end_session_request" | ||
internal const val LOGOUT_FRAGMENT_TAG = "logout_fragment" | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
oneloginoidc/src/main/java/com/onelogin/oidc/logout/SignOutManager.kt
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.onelogin.oidc.logout | ||
|
||
import android.app.Activity | ||
import com.onelogin.oidc.Callback | ||
|
||
interface SignOutManager { | ||
suspend fun signOut( | ||
idToken: String, | ||
activity: Activity, | ||
signOutCallback: Callback<SignOutSuccess, SignOutError> | ||
) | ||
} |
75 changes: 75 additions & 0 deletions
75
oneloginoidc/src/main/java/com/onelogin/oidc/logout/SignOutManagerImpl.kt
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.onelogin.oidc.logout | ||
|
||
import android.app.Activity | ||
import android.net.Uri | ||
import androidx.fragment.app.FragmentActivity | ||
import com.onelogin.oidc.Callback | ||
import com.onelogin.oidc.OIDCConfiguration | ||
import com.onelogin.oidc.data.repository.OIDCRepository | ||
import kotlinx.coroutines.channels.consumeEach | ||
import net.openid.appauth.AuthorizationServiceConfiguration | ||
import net.openid.appauth.EndSessionRequest | ||
|
||
internal class SignOutManagerImpl( | ||
private val configuration: OIDCConfiguration, | ||
private val repository: OIDCRepository, | ||
private val signOutFragmentProvider: (EndSessionRequest) -> SignOutFragment | ||
) : SignOutManager { | ||
|
||
override suspend fun signOut( | ||
idToken: String, | ||
activity: Activity, | ||
signOutCallback: Callback<SignOutSuccess, SignOutError> | ||
) { | ||
val authConfiguration = repository.getConfigurations() | ||
val endSessionRequest = getEndSessionRequest(idToken, configuration, authConfiguration) | ||
|
||
if (activity is FragmentActivity) { | ||
removeFragmentIfAttached(activity) | ||
val logoutFragment = signOutFragmentProvider(endSessionRequest) | ||
attachLogoutFragment(activity, logoutFragment) | ||
logoutFragment.resultChannel.consumeEach { (response, exception) -> | ||
if (exception != null) { | ||
signOutCallback.onError(SignOutError(exception.message, exception)) | ||
} else if (response != null) { | ||
repository.clearAuthState() | ||
signOutCallback.onSuccess(SignOutSuccess("Success")) | ||
} | ||
} | ||
removeFragmentIfAttached(activity) | ||
} else { | ||
throw IllegalStateException("Your activity should extend FragmentActivity or AppCompatActivity") | ||
} | ||
} | ||
|
||
private fun attachLogoutFragment( | ||
activity: FragmentActivity, | ||
loginFragment: SignOutFragment | ||
) { | ||
activity.supportFragmentManager.beginTransaction() | ||
.add(loginFragment, SignOutFragment.LOGOUT_FRAGMENT_TAG) | ||
.commit() | ||
} | ||
|
||
private fun removeFragmentIfAttached(activity: FragmentActivity) { | ||
activity.supportFragmentManager.findFragmentByTag(SignOutFragment.LOGOUT_FRAGMENT_TAG)?.let { | ||
activity.supportFragmentManager.beginTransaction() | ||
.remove(it) | ||
.commit() | ||
} | ||
} | ||
|
||
private fun getEndSessionRequest( | ||
idToken: String, | ||
configuration: OIDCConfiguration, | ||
authConfiguration: AuthorizationServiceConfiguration | ||
): EndSessionRequest { | ||
val issuerUrl = Uri.parse(configuration.issuer) | ||
|
||
val endSessionReqBuilder = EndSessionRequest.Builder(authConfiguration) | ||
.setIdTokenHint(idToken) | ||
.setPostLogoutRedirectUri(Uri.parse(configuration.redirectUrl)) | ||
|
||
return endSessionReqBuilder.build() | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
oneloginoidc/src/main/java/com/onelogin/oidc/logout/SignOutSuccess.kt
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.onelogin.oidc.logout | ||
|
||
data class SignOutSuccess( | ||
val status: String | ||
) |