Skip to content

Commit 761a8e1

Browse files
committed
Streamlined a load of auth error issues
1 parent 09a121b commit 761a8e1

6 files changed

Lines changed: 48 additions & 35 deletions

File tree

build.gradle.kts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = "com.kruthers"
8-
version = "2.2.1"
8+
version = "2.3.0"
99
description = "Allows you to ingrate github support into your datapack folder in game"
1010

1111
repositories {
@@ -32,11 +32,9 @@ dependencies {
3232
tasks {
3333
shadowJar {
3434
archiveClassifier.set("")
35-
3635
minimize {
37-
exclude(dependency("org.eclipse.jgit:org.eclipse.jgit:.*"))
36+
exclude(dependency("org.eclipse.jgit:org.eclipse.jgit.ssh.jsch:*"))
3837
}
39-
minimize()
4038
}
4139
build {
4240
dependsOn(shadowJar)

src/main/kotlin/com/kruthers/datapackmanager/actions/Clone.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class Clone(private val sender: CommandSender, private val plugin: DatapackManag
5252
sender.sendMessage(parse("<red>Failed to clone from github, invalid link given, check the console for more details."))
5353
log.warning("Failed to complete clone, an invalid remove exception occurred: \n${e.message}")
5454
return
55+
} catch (e: NotSshRemote) {
56+
sender.sendMessage(parse("<red>Failed to clone from github, invalid link: The provided git remote is not a ssh clone remote. These start with git@ not https://"))
57+
log.warning("Failed to complete clone, the provided remote was no an ssh remove")
58+
return
5559
} catch (e: Exception) {
5660
sender.sendMessage(parse("<red>Failed to clone from github, a unknown Exception occurred, check the console for more details."))
5761
log.warning("Failed to complete clone, an unknown exception occurred: \n${e.stackTraceToString()}")

src/main/kotlin/com/kruthers/datapackmanager/actions/Pull.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import org.eclipse.jgit.api.ResetCommand
1515
import org.eclipse.jgit.lib.Repository
1616
import org.eclipse.jgit.lib.SubmoduleConfig
1717
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
18-
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider
1918
import java.io.File
2019
import java.util.logging.Logger
2120

@@ -84,11 +83,4 @@ class Pull(private val sender: CommandSender, private val plugin: DatapackManage
8483

8584
this.runTaskAsynchronously(plugin)
8685
}
87-
88-
override fun triggerWithAuth(auth: UsernamePasswordCredentialsProvider) {
89-
command.setCredentialsProvider(auth)
90-
this.runTaskAsynchronously(plugin)
91-
92-
super.triggerWithAuth(auth)
93-
}
9486
}

src/main/kotlin/com/kruthers/datapackmanager/commands/GitCommand.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import com.kruthers.datapackmanager.utils.AuthType
88
import com.kruthers.datapackmanager.utils.MultipleAuthType
99
import com.kruthers.datapackmanager.utils.NoActionFound
1010
import com.kruthers.datapackmanager.utils.parse
11+
import net.kyori.adventure.text.Component
12+
import net.kyori.adventure.text.format.NamedTextColor
1113
import org.bukkit.command.CommandSender
1214

1315
class GitCommand(pl: DatapackManager) {
@@ -17,8 +19,12 @@ class GitCommand(pl: DatapackManager) {
1719
@CommandDescription("Pull data from the github")
1820
@CommandPermission("datapackmanager.git")
1921
fun onGitPullCommand(sender: CommandSender, @Flag("force", description = "Will force from master", aliases = ["f"]) force: Boolean) {
20-
val command = Pull(sender, this.plugin, force)
21-
command.trigger()
22+
try {
23+
val command = Pull(sender, this.plugin, force)
24+
command.trigger()
25+
} catch (err: Exception) {
26+
sender.sendMessage(Component.text("Failed to pull from github: \n${err.message}", NamedTextColor.RED))
27+
}
2228
}
2329

2430
@CommandMethod("git clone <repo> [branch]")
@@ -55,14 +61,16 @@ class GitCommand(pl: DatapackManager) {
5561
@CommandDescription("Confirm a github action")
5662
@CommandPermission("datapackmanager.git")
5763
fun onGitConfirmCommand(sender: CommandSender) {
58-
if (DatapackManager.confirmation.containsKey(sender)) {
59-
val action = DatapackManager.confirmation[sender]
60-
action!!.trigger()
61-
64+
try {
65+
if (DatapackManager.confirmation.containsKey(sender)) {
66+
DatapackManager.confirmation[sender]!!.trigger()
67+
} else {
68+
throw NoActionFound()
69+
}
70+
} catch (err: Exception) {
71+
sender.sendMessage(Component.text(err.message!!, NamedTextColor.RED))
72+
} finally {
6273
DatapackManager.confirmation.remove(sender)
63-
64-
} else {
65-
throw NoActionFound()
6674
}
6775
}
6876
}

src/main/kotlin/com/kruthers/datapackmanager/utils/AuthUtils.kt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,44 @@ import com.jcraft.jsch.Session
55
import com.kruthers.datapackmanager.DatapackManager
66
import org.bukkit.configuration.file.FileConfiguration
77
import org.eclipse.jgit.api.TransportConfigCallback
8-
import org.eclipse.jgit.transport.*
8+
import org.eclipse.jgit.transport.SshTransport
9+
import org.eclipse.jgit.transport.Transport
10+
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider
911
import org.eclipse.jgit.transport.ssh.jsch.JschConfigSessionFactory
1012
import org.eclipse.jgit.transport.ssh.jsch.OpenSshConfig
1113
import org.eclipse.jgit.util.FS
1214

15+
1316
class SshCallback(plugin: DatapackManager): TransportConfigCallback {
14-
val config: FileConfiguration = plugin.config
15-
val passphase: String = config.getString("auth.ssh.password") ?: ""
17+
private val config: FileConfiguration = plugin.config
18+
private val passphrase: String = config.getString("auth.ssh.password") ?: ""
19+
private val key = "${plugin.dataFolder}/key.txt"
1620

17-
val sshSessionFactory: SshSessionFactory = object : JschConfigSessionFactory() {
21+
private class SshFactory(private val key: String, private val passphrase: String): JschConfigSessionFactory() {
1822
override fun configure(hc: OpenSshConfig.Host?, session: Session?) {
1923
session?.setConfig("StrictHostKeyChecking","no")
2024
super.configure(hc, session)
2125
}
2226

2327
override fun createDefaultJSch(fs: FS?): JSch {
2428
val jSch: JSch = super.createDefaultJSch(fs)
25-
jSch.addIdentity("${plugin.dataFolder}/key.txt",passphase)
29+
jSch.addIdentity(key ,passphrase)
2630

2731
return jSch
2832
}
2933
}
3034

31-
override fun configure(p0: Transport?) {
32-
if (p0 is SshTransport) {
33-
val sshTransport: SshTransport = p0
34-
sshTransport.sshSessionFactory = sshSessionFactory
35-
}
35+
/**
36+
* Add any additional transport-specific configuration required.
37+
*
38+
* @param transport
39+
* a [org.eclipse.jgit.transport.Transport] object.
40+
*/
41+
override fun configure(transport: Transport?) {
42+
if (transport is SshTransport) {
43+
transport.setSshSessionFactory(SshFactory(key, passphrase))
44+
} else throw NotSshRemote()
3645
}
37-
38-
3946
}
4047

4148
fun getUsernamePasswordAuth(plugin: DatapackManager): UsernamePasswordCredentialsProvider {
@@ -48,3 +55,4 @@ fun getTokenAuth(plugin: DatapackManager): UsernamePasswordCredentialsProvider {
4855
val token: String = plugin.config.getString("auth.token.token")?: "token"
4956
return UsernamePasswordCredentialsProvider(token,"")
5057
}
58+
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package com.kruthers.datapackmanager.utils
22

3-
class MultipleAuthType: Error("Multiple methods of authentication provided, provide only one")
4-
class NoActionFound: Error ("No action found await your confirmation")
3+
class MultipleAuthType: Exception("Multiple methods of authentication provided, provide only one")
4+
class NoActionFound: Exception("No action found await your confirmation")
5+
6+
class NotSshRemote: Exception("Unable to close using ssh, the provided git remote is not a ssh clone remote. These " +
7+
"start with git@ not https://")

0 commit comments

Comments
 (0)