Skip to content

Commit

Permalink
Add no-auth helper
Browse files Browse the repository at this point in the history
  • Loading branch information
TBog committed Dec 6, 2023
1 parent aeae071 commit 5de30d1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/src/main/java/com/kirkbushman/araw/RedditClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.kirkbushman.araw.clients.WikisClient
import com.kirkbushman.araw.models.Me
import com.kirkbushman.araw.utils.Utils.buildRetrofit
import com.kirkbushman.auth.models.bearers.TokenBearer
import com.kirkbushman.auth.models.enums.AuthType
import retrofit2.Retrofit

class RedditClient @JvmOverloads constructor(
Expand Down Expand Up @@ -68,6 +69,8 @@ class RedditClient @JvmOverloads constructor(
}

private fun getHeaderMap(): Map<String, String> {
if (bearer.getAuthType() == AuthType.NONE)
return emptyMap()
return mapOf("Authorization" to "bearer ".plus(bearer.getAccessToken()))
}
}
48 changes: 48 additions & 0 deletions lib/src/main/java/com/kirkbushman/araw/helpers/NoAuthHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.kirkbushman.araw.helpers

import com.kirkbushman.araw.RedditClient
import com.kirkbushman.araw.RedditClient.Companion.getRetrofit
import com.kirkbushman.auth.RedditAuth
import com.kirkbushman.auth.managers.StorageManager
import com.kirkbushman.auth.models.Token
import com.kirkbushman.auth.models.bearers.TokenBearer
import com.kirkbushman.auth.models.creds.UserlessCredentials
import com.kirkbushman.auth.models.enums.AuthType
import com.kirkbushman.auth.utils.Utils.buildDefaultClient

class NoAuthHelper(logging: Boolean, disableLegacyEncoding: Boolean) :
AuthHelper(logging, disableLegacyEncoding) {
private val tokenBearer: TokenBearer
private val storageManager: StorageManager

init {
storageManager = object : StorageManager {
override fun isAuthed(): Boolean = true
override fun authType(): AuthType = AuthType.NONE
override fun hasToken(): Boolean = false
override fun getToken(): Token? = null
override fun clearAll() = Unit
override fun saveToken(token: Token, authType: AuthType): Unit =
throw IllegalStateException("NoAuth doesn't use tokens")
}
tokenBearer = object : TokenBearer(storageManager) {
override fun renewToken(token: Token): Token? = token
override fun revokeToken(token: Token): Boolean = false
}
}

override val auth: RedditAuth
get() {
val client = buildDefaultClient(getRetrofit(logging), logging)
val cred = UserlessCredentials("", "DO_NOT_TRACK_THIS_DEVICE")
return object : RedditAuth(client, cred, storageManager, storageManager.authType()) {
override fun renewToken(token: Token): Token? = tokenBearer.renewToken(token)
override fun revokeToken(token: Token): Boolean = tokenBearer.revokeToken(token)
override fun retrieveSavedBearer(): TokenBearer? = tokenBearer
override fun fetchToken(): Token? = tokenBearer.getToken()
}
}

override fun getRedditClient(): RedditClient? =
RedditClient(tokenBearer, disableLegacyEncoding, logging)
}

0 comments on commit 5de30d1

Please sign in to comment.