Skip to content

Commit afa97cb

Browse files
committed
Improve token handling in interceptor
1 parent 0d5b3b8 commit afa97cb

2 files changed

Lines changed: 27 additions & 38 deletions

File tree

library/src/main/kotlin/me/proxer/library/ProxerException.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ class ProxerException : Exception {
4444
*/
4545
constructor(errorType: ErrorType, serverErrorCode: Int?, message: String?) : super(message) {
4646
this.errorType = errorType
47-
this.serverErrorType =
48-
ServerErrorType.fromErrorCode(serverErrorCode)
47+
this.serverErrorType = ServerErrorType.fromErrorCode(serverErrorCode)
4948
}
5049

5150
/**
@@ -164,5 +163,23 @@ class ProxerException : Exception {
164163
return enumValues<ServerErrorType>().find { it.code == errorCode } ?: UNKNOWN
165164
}
166165
}
166+
167+
internal val isLoginError
168+
get() = when (this) {
169+
INVALID_TOKEN,
170+
NOTIFICATIONS_LOGIN_REQUIRED,
171+
UCP_LOGIN_REQUIRED,
172+
INFO_LOGIN_REQUIRED,
173+
LOGIN_ALREADY_LOGGED_IN,
174+
LOGIN_DIFFERENT_USER_ALREADY_LOGGED_IN,
175+
MESSAGES_LOGIN_REQUIRED,
176+
CHAT_LOGIN_REQUIRED,
177+
USER_2FA_SECRET_REQUIRED,
178+
ANIME_LOGIN_REQUIRED,
179+
IP_AUTHENTICATION_REQUIRED,
180+
COMMENT_LOGIN_REQUIRED -> true
181+
182+
else -> false
183+
}
167184
}
168185
}

library/src/main/kotlin/me/proxer/library/internal/interceptor/LoginTokenInterceptor.kt

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ internal class LoginTokenInterceptor(private val loginTokenManager: LoginTokenMa
1414

1515
private companion object {
1616
private const val LOGIN_TOKEN_HEADER = "proxer-api-token"
17-
private const val MAX_PEEK_BYTE_COUNT = 512L
17+
private const val MAX_PEEK_BYTE_COUNT = 1_048_576L
1818

19-
private val LOGIN_TOKEN_PATTERN = Regex("\"token\":.*?\"(.{255})\"", RegexOption.DOT_MATCHES_ALL)
19+
private val LOGIN_TOKEN_PATTERN = Regex("\"token\":.*?\"(.+?)\"", RegexOption.DOT_MATCHES_ALL)
2020
private val ERROR_PATTERN = Regex("\"code\":.*?(\\d+\b?)", RegexOption.DOT_MATCHES_ALL)
2121

2222
private val LOGIN_URL = ProxerUrls.apiBase.newBuilder()
@@ -48,31 +48,24 @@ internal class LoginTokenInterceptor(private val loginTokenManager: LoginTokenMa
4848
}
4949
}
5050

51-
val response = chain.proceed(newRequestBuilder.build())
52-
53-
if (response.isSuccessful) {
54-
handleResponse(response)
55-
}
56-
57-
return response
51+
return handleResponse(chain.proceed(newRequestBuilder.build()))
5852
} else {
5953
return chain.proceed(oldRequest)
6054
}
6155
}
6256

63-
private fun handleResponse(response: Response) {
57+
private fun handleResponse(response: Response): Response {
6458
val responseBody = peekResponseBody(response)
6559
val url = response.request.url
6660

6761
synchronized(lock) {
68-
val existingLoginToken = loginTokenManager.provide()
6962
val potentialError = ERROR_PATTERN.find(responseBody)
7063

7164
if (potentialError != null) {
7265
val errorCode = potentialError.groupValues[1].toInt()
7366
val errorType = ServerErrorType.fromErrorCode(errorCode)
7467

75-
if (errorType != ServerErrorType.UNKNOWN && existingLoginToken != null && isLoginError(errorType)) {
68+
if (errorType.isLoginError) {
7669
loginTokenManager.persist(null)
7770
}
7871
} else if (url.pathSegments == LOGIN_URL.pathSegments) {
@@ -87,32 +80,11 @@ internal class LoginTokenInterceptor(private val loginTokenManager: LoginTokenMa
8780
loginTokenManager.persist(null)
8881
}
8982
}
90-
}
9183

92-
private fun peekResponseBody(response: Response): String {
93-
return response.body?.source()
94-
?.let {
95-
it.request(MAX_PEEK_BYTE_COUNT)
96-
97-
it.buffer.snapshot().utf8()
98-
}
99-
?: ""
84+
return response
10085
}
10186

102-
private fun isLoginError(errorType: ServerErrorType): Boolean {
103-
return when (errorType) {
104-
ServerErrorType.INVALID_TOKEN,
105-
ServerErrorType.NOTIFICATIONS_LOGIN_REQUIRED,
106-
ServerErrorType.UCP_LOGIN_REQUIRED,
107-
ServerErrorType.INFO_LOGIN_REQUIRED,
108-
ServerErrorType.LOGIN_ALREADY_LOGGED_IN,
109-
ServerErrorType.LOGIN_DIFFERENT_USER_ALREADY_LOGGED_IN,
110-
ServerErrorType.MESSAGES_LOGIN_REQUIRED,
111-
ServerErrorType.CHAT_LOGIN_REQUIRED,
112-
ServerErrorType.USER_2FA_SECRET_REQUIRED,
113-
ServerErrorType.ANIME_LOGIN_REQUIRED -> true
114-
115-
else -> false
116-
}
87+
private fun peekResponseBody(response: Response): String {
88+
return if (response.body != null) response.peekBody(MAX_PEEK_BYTE_COUNT).string() else ""
11789
}
11890
}

0 commit comments

Comments
 (0)