forked from fossasia/open-event-attendee-android
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement JWT refresh token for authorization (fossasia#2230)
- Loading branch information
1 parent
f0d8f01
commit 67c2390
Showing
7 changed files
with
121 additions
and
13 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
app/src/fdroid/java/org/fossasia/openevent/general/auth/TokenAuthenticator.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,42 @@ | ||
package org.fossasia.openevent.general.auth | ||
|
||
import okhttp3.Authenticator | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import okhttp3.Route | ||
import org.koin.core.KoinComponent | ||
import org.koin.core.inject | ||
|
||
class TokenAuthenticator : Authenticator, KoinComponent { | ||
|
||
val tokenService: RefreshTokenService by inject() | ||
val authHolder: AuthHolder by inject() | ||
|
||
/** | ||
* Authenticator for when the authToken need to be refresh and updated | ||
* everytime we get a 401 error code | ||
*/ | ||
|
||
override fun authenticate(route: Route?, response: Response): Request? { | ||
|
||
val loginResponse = tokenService.refreshToken() | ||
|
||
return if (loginResponse.isSuccessful) { | ||
/** | ||
* Replace the existing tokens with the new tokens | ||
**/ | ||
loginResponse.body()?.let { | ||
authHolder.accessToken = it.accessToken | ||
authHolder.refreshToken = it.refreshToken | ||
|
||
val newToken = "JWT ${it.accessToken}" | ||
|
||
response.request.newBuilder() | ||
.addHeader("Authorization", newToken) | ||
.build() | ||
} | ||
} else { | ||
response.request | ||
} | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
app/src/main/java/org/fossasia/openevent/general/auth/RefreshTokenService.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,26 @@ | ||
package org.fossasia.openevent.general.auth | ||
|
||
import io.reactivex.Single | ||
import org.fossasia.openevent.general.BuildConfig | ||
import retrofit2.Response | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
|
||
class RefreshTokenService( | ||
private val authHolder: AuthHolder | ||
) { | ||
|
||
private val retrofit = Retrofit.Builder() | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.baseUrl(BuildConfig.DEFAULT_BASE_URL) | ||
.build() | ||
|
||
|
||
private val authApi: AuthApi = retrofit.create(AuthApi::class.java) | ||
|
||
fun refreshToken(): Response<LoginResponse> { | ||
return authApi.refreshToken() | ||
} | ||
|
||
|
||
} |
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