-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 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
48 changes: 48 additions & 0 deletions
48
lib/src/main/java/com/kirkbushman/araw/helpers/NoAuthHelper.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,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) | ||
} |