Skip to content

Commit bff22a4

Browse files
committed
chore: respond to banner code review
1 parent d0ebd20 commit bff22a4

2 files changed

Lines changed: 31 additions & 40 deletions

File tree

sshlib/src/main/kotlin/org/connectbot/sshlib/client/SshConnection.kt

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -744,11 +744,7 @@ class SshConnection(
744744
setMethodSpecificFields(noneAuth)
745745
}
746746

747-
var noneResult = channel.receive()
748-
while (noneResult is InternalAuthResult.Banner) {
749-
handler.onBanner(noneResult.message)
750-
noneResult = channel.receive()
751-
}
747+
val noneResult = receiveAuthResult(channel, handler)
752748
if (noneResult is InternalAuthResult.Success) return PublicAuthResult.Success
753749
if (noneResult !is InternalAuthResult.Failure) return PublicAuthResult.Error("Unexpected response to 'none' auth: $noneResult")
754750

@@ -823,12 +819,7 @@ class SshConnection(
823819
}
824820
setMethodSpecificFields(pubkeyAuth)
825821
}
826-
var response = channel.receive()
827-
while (response is InternalAuthResult.Banner) {
828-
handler.onBanner(response.message)
829-
response = channel.receive()
830-
}
831-
return response
822+
return receiveAuthResult(channel, handler)
832823
}
833824

834825
private suspend fun signPublicKey(
@@ -881,11 +872,7 @@ class SshConnection(
881872
}
882873
}
883874

884-
var response = channel.receive()
885-
while (response is InternalAuthResult.Banner) {
886-
handler.onBanner(response.message)
887-
response = channel.receive()
888-
}
875+
val response = receiveAuthResult(channel, handler)
889876
return when (response) {
890877
is InternalAuthResult.Success -> true
891878
else -> false
@@ -965,11 +952,7 @@ class SshConnection(
965952
setMethodSpecificFields(passAuth)
966953
}
967954

968-
var response = channel.receive()
969-
while (response is InternalAuthResult.Banner) {
970-
handler.onBanner(response.message)
971-
response = channel.receive()
972-
}
955+
val response = receiveAuthResult(channel, handler)
973956
return when (val result = response) {
974957
is InternalAuthResult.Success -> true
975958

@@ -982,6 +965,18 @@ class SshConnection(
982965
}
983966
}
984967

968+
private suspend fun receiveAuthResult(
969+
channel: Channel<InternalAuthResult>,
970+
handler: AuthHandler,
971+
): InternalAuthResult {
972+
var result = channel.receive()
973+
while (result is InternalAuthResult.Banner) {
974+
handler.onBanner(result.message)
975+
result = channel.receive()
976+
}
977+
return result
978+
}
979+
985980
private suspend fun sendAuthRequest(
986981
username: String,
987982
method: String,

sshlib/src/test/kotlin/org/connectbot/sshlib/client/FakeSshServer.kt

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -549,28 +549,24 @@ class FakeSshServer(
549549
}
550550
}
551551

552-
fun sendUserauthBanner(message: String) {
553-
scope.launch(coroutineContext) {
554-
val banner = SshMsgUserauthBanner()
555-
val utf8 = createUtf8String(message)
556-
banner.setMessage(utf8)
557-
banner.setLanguageTag(createByteString(ByteArray(0)))
558-
banner._check()
559-
writeMutex.withLock {
560-
serverIo.writePacket(SshEnums.MessageType.SSH_MSG_USERAUTH_BANNER.id().toInt(), banner.toByteArray())
561-
}
552+
suspend fun sendUserauthBanner(message: String) {
553+
val banner = SshMsgUserauthBanner()
554+
val utf8 = createUtf8String(message)
555+
banner.setMessage(utf8)
556+
banner.setLanguageTag(createByteString(ByteArray(0)))
557+
banner._check()
558+
writeMutex.withLock {
559+
serverIo.writePacket(SshEnums.MessageType.SSH_MSG_USERAUTH_BANNER.id().toInt(), banner.toByteArray())
562560
}
563561
}
564562

565-
fun sendUserauthFailure(allowedMethods: Set<String>, partialSuccess: Boolean) {
566-
scope.launch(coroutineContext) {
567-
val failure = SshMsgUserauthFailure()
568-
failure.setValidAuthentications(createNameList(allowedMethods.joinToString(",")))
569-
failure.setPartialSuccess(if (partialSuccess) 1 else 0)
570-
failure._check()
571-
writeMutex.withLock {
572-
serverIo.writePacket(SshEnums.MessageType.SSH_MSG_USERAUTH_FAILURE.id().toInt(), failure.toByteArray())
573-
}
563+
suspend fun sendUserauthFailure(allowedMethods: Set<String>, partialSuccess: Boolean) {
564+
val failure = SshMsgUserauthFailure()
565+
failure.setValidAuthentications(createNameList(allowedMethods.joinToString(",")))
566+
failure.setPartialSuccess(if (partialSuccess) 1 else 0)
567+
failure._check()
568+
writeMutex.withLock {
569+
serverIo.writePacket(SshEnums.MessageType.SSH_MSG_USERAUTH_FAILURE.id().toInt(), failure.toByteArray())
574570
}
575571
}
576572

0 commit comments

Comments
 (0)