···141141 }
142142 }
143143144144+ HttpStatusCode.BadRequest -> throw RefreshTokenInvalidException()
145145+144146 else -> {
145147 TODO("Handle failures")
146148 }
···242244 /** Create `AuthTokens` from an `AuthSession`. */
243245 fun into(): AuthTokens = AuthTokens(this.accessJwt, this.refreshJwt, this.did)
244246}
247247+248248+/**
249249+ * Base class for errors specific to the [AuthManager].
250250+ *
251251+ * @param message Description of the error.
252252+ * @param cause Underlying exception that caused the error, if any.
253253+ */
254254+open class AuthManagerException(
255255+ message: String,
256256+ cause: Throwable? = null,
257257+) : RuntimeException(message, cause)
258258+259259+/**
260260+ * Stored refresh token is invalid.
261261+ */
262262+class RefreshTokenInvalidException() : AuthManagerException(message = "Refresh token is invalid")
+7-1
darkfeed/src/main/kotlin/api/AuthPlugin.kt
···142142 // other requests will block when they try to get the
143143 // tokens.
144144 authMutex.withLock {
145145- authManager.refreshSession()
145145+ try {
146146+ authManager.refreshSession()
147147+ } catch (error: RefreshTokenInvalidException) {
148148+ // Create new session using username and password.
149149+ log.debug("Refresh token is invalid. Creating session from username and password.")
150150+ authManager.createSession()
151151+ }
146152 authManager.authTokens?.accessToken
147153 }
148154 }