Bluesky feed server - NSFW Likes

feat: Create session with password if refresh token is invalid

bladee.bsky.social 780deef3 6a0e5f6e

verified
Changed files
+27 -1
darkfeed
src
+2
darkfeed/src/main/kotlin/Main.kt
··· 129 129 verifyAndUpdateFeedGeneratorRecord(bskyApi) 130 130 } catch (error: Exception) { 131 131 log.warn("Failed to update feed generator record: {}", error.message) 132 + } catch (error: NotImplementedError) { 133 + log.warn("Unimplemented path hit while updating feed generator record:\n{}", error.stackTraceToString()) 132 134 } 133 135 134 136 // Serve the feed generator.
+18
darkfeed/src/main/kotlin/api/AuthManager.kt
··· 141 141 } 142 142 } 143 143 144 + HttpStatusCode.BadRequest -> throw RefreshTokenInvalidException() 145 + 144 146 else -> { 145 147 TODO("Handle failures") 146 148 } ··· 242 244 /** Create `AuthTokens` from an `AuthSession`. */ 243 245 fun into(): AuthTokens = AuthTokens(this.accessJwt, this.refreshJwt, this.did) 244 246 } 247 + 248 + /** 249 + * Base class for errors specific to the [AuthManager]. 250 + * 251 + * @param message Description of the error. 252 + * @param cause Underlying exception that caused the error, if any. 253 + */ 254 + open class AuthManagerException( 255 + message: String, 256 + cause: Throwable? = null, 257 + ) : RuntimeException(message, cause) 258 + 259 + /** 260 + * Stored refresh token is invalid. 261 + */ 262 + class RefreshTokenInvalidException() : AuthManagerException(message = "Refresh token is invalid")
+7 -1
darkfeed/src/main/kotlin/api/AuthPlugin.kt
··· 142 142 // other requests will block when they try to get the 143 143 // tokens. 144 144 authMutex.withLock { 145 - authManager.refreshSession() 145 + try { 146 + authManager.refreshSession() 147 + } catch (error: RefreshTokenInvalidException) { 148 + // Create new session using username and password. 149 + log.debug("Refresh token is invalid. Creating session from username and password.") 150 + authManager.createSession() 151 + } 146 152 authManager.authTokens?.accessToken 147 153 } 148 154 }