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

ACCESS_GRANTED check for the JWT #146

Merged
merged 1 commit into from
Feb 29, 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
3 changes: 2 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ android {
buildConfigField("String", "SIGNUP_URL", "\"https://auth.opendatahub.com/auth/realms/noi/protocol/openid-connect/registrations?client_id=it.bz.noi.community&redirect_uri=https://noi.bz.it&response_type=code&scope=openid\"")
buildConfigField("String", "COMMUNITY_API_URL", "\"https://api.community.noi.opendatahub.com\"")
buildConfigField("String", "OPENDATAHUB_API_BASE_URL", "\"https://tourism.opendatahub.com/\"")
buildConfigField("Boolean", "CHECK_ACCESS_GRANTED_TOKEN", "false")
}

buildTypes {
Expand Down
34 changes: 14 additions & 20 deletions app/src/main/java/it/bz/noi/community/oauth/AuthManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -393,25 +393,20 @@ object AuthManager {
val state = userState.first()
state.authState.update(response, exception)
writeAuthState(state)
val authResponse = state.authState.lastAuthorizationResponse
if (authResponse != null) {
try {
val tokenResponse = obtainToken(authResponse)
tokenResponse.accessToken?.let { jwtToken ->
decode(jwtToken)?.let { decodedToken ->
if (verifyToken(decodedToken)) {
Log.d(TAG, "Access granted role check: true")
onTokenObtained(tokenResponse, null, true)
} else {
Log.d(TAG, "Access granted role check: false")
onTokenObtained(tokenResponse, null, false)
}
}
}
} catch (ex: AuthorizationException) {
onTokenObtained(null, ex, state.validRole)
val authResponse = state.authState.lastAuthorizationResponse ?: return@launch
try {
val tokenResponse = obtainToken(authResponse)
val jwtToken = tokenResponse.accessToken ?: return@launch
val decodedToken = decode(jwtToken) ?: return@launch
if (decodedToken.isValid()) {
Log.d(TAG, "Access granted role check: true")
onTokenObtained(tokenResponse, null, true)
} else {
Log.d(TAG, "Access granted role check: false")
onTokenObtained(tokenResponse, null, false)
}

} catch (ex: AuthorizationException) {
onTokenObtained(null, ex, state.validRole)
}
}
}
Expand All @@ -434,8 +429,7 @@ object AuthManager {
}
}

private fun verifyToken(token: NOIJwtAccessToken) =
token.checkResourceAccessRoles(CLIENT_ID, listOf(ACCESS_GRANTED_ROLE))
private fun NOIJwtAccessToken.isValid() = if (BuildConfig.CHECK_ACCESS_GRANTED_TOKEN) checkResourceAccessRoles(CLIENT_ID, listOf(ACCESS_GRANTED_ROLE)) else true

private suspend fun onTokenObtained(
tokenResponse: TokenResponse?,
Expand Down
2 changes: 1 addition & 1 deletion app/version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Tue Sep 28 07:39:37 UTC 2021
VERSION_CODE=19
VERSION_CODE=20
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.9.20'
ext.nav_version = '2.7.5'
ext.nav_version = '2.7.7'
repositories {
mavenCentral()
google()
}
dependencies {
classpath "com.android.tools.build:gradle:8.2.0"
classpath "com.android.tools.build:gradle:8.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
Expand Down
Loading