mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-10 15:35:59 +01:00
(refs #115)Display finger print on the ssh key setting page
This commit is contained in:
@@ -1,23 +1,18 @@
|
|||||||
package ssh
|
package ssh
|
||||||
|
|
||||||
import org.apache.sshd.server.PublickeyAuthenticator
|
import org.apache.sshd.server.PublickeyAuthenticator
|
||||||
import org.slf4j.LoggerFactory
|
|
||||||
import org.apache.sshd.server.session.ServerSession
|
import org.apache.sshd.server.session.ServerSession
|
||||||
import java.security.PublicKey
|
import java.security.PublicKey
|
||||||
import org.apache.commons.codec.binary.Base64
|
|
||||||
import org.apache.sshd.common.util.Buffer
|
|
||||||
import org.eclipse.jgit.lib.Constants
|
|
||||||
import service.SshKeyService
|
import service.SshKeyService
|
||||||
import servlet.Database
|
import servlet.Database
|
||||||
import javax.servlet.ServletContext
|
import javax.servlet.ServletContext
|
||||||
|
|
||||||
class PublicKeyAuthenticator(context: ServletContext) extends PublickeyAuthenticator with SshKeyService {
|
class PublicKeyAuthenticator(context: ServletContext) extends PublickeyAuthenticator with SshKeyService {
|
||||||
private val logger = LoggerFactory.getLogger(classOf[PublicKeyAuthenticator])
|
|
||||||
|
|
||||||
override def authenticate(username: String, key: PublicKey, session: ServerSession): Boolean = {
|
override def authenticate(username: String, key: PublicKey, session: ServerSession): Boolean = {
|
||||||
Database(context) withTransaction {
|
Database(context) withTransaction {
|
||||||
getPublicKeys(username).exists { sshKey =>
|
getPublicKeys(username).exists { sshKey =>
|
||||||
str2PublicKey(sshKey.publicKey) match {
|
SshUtil.str2PublicKey(sshKey.publicKey) match {
|
||||||
case Some(publicKey) => key.equals(publicKey)
|
case Some(publicKey) => key.equals(publicKey)
|
||||||
case _ => false
|
case _ => false
|
||||||
}
|
}
|
||||||
@@ -25,22 +20,4 @@ class PublicKeyAuthenticator(context: ServletContext) extends PublickeyAuthentic
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private def str2PublicKey(key: String): Option[PublicKey] = {
|
|
||||||
// TODO RFC 4716 Public Key is not supported...
|
|
||||||
val parts = key.split(" ")
|
|
||||||
if (parts.size < 2) {
|
|
||||||
logger.debug(s"Invalid PublicKey Format: key")
|
|
||||||
return None
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
val encodedKey = parts(1)
|
|
||||||
val decode = Base64.decodeBase64(Constants.encodeASCII(encodedKey))
|
|
||||||
Some(new Buffer(decode).getRawPublicKey)
|
|
||||||
} catch {
|
|
||||||
case e: Throwable =>
|
|
||||||
logger.debug(e.getMessage, e)
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
33
src/main/scala/ssh/SshUtil.scala
Normal file
33
src/main/scala/ssh/SshUtil.scala
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package ssh
|
||||||
|
|
||||||
|
import java.security.PublicKey
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import org.apache.commons.codec.binary.Base64
|
||||||
|
import org.eclipse.jgit.lib.Constants
|
||||||
|
import org.apache.sshd.common.util.{KeyUtils, Buffer}
|
||||||
|
|
||||||
|
object SshUtil {
|
||||||
|
|
||||||
|
private val logger = LoggerFactory.getLogger(SshUtil.getClass)
|
||||||
|
|
||||||
|
def str2PublicKey(key: String): Option[PublicKey] = {
|
||||||
|
// TODO RFC 4716 Public Key is not supported...
|
||||||
|
val parts = key.split(" ")
|
||||||
|
if (parts.size < 2) {
|
||||||
|
logger.debug(s"Invalid PublicKey Format: key")
|
||||||
|
return None
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
val encodedKey = parts(1)
|
||||||
|
val decode = Base64.decodeBase64(Constants.encodeASCII(encodedKey))
|
||||||
|
Some(new Buffer(decode).getRawPublicKey)
|
||||||
|
} catch {
|
||||||
|
case e: Throwable =>
|
||||||
|
logger.debug(e.getMessage, e)
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def fingerPrint(key: String): String = KeyUtils.getFingerPrint(str2PublicKey(key).get)
|
||||||
|
|
||||||
|
}
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
@if(i != 0){
|
@if(i != 0){
|
||||||
<hr>
|
<hr>
|
||||||
}
|
}
|
||||||
<strong>@key.title</strong>
|
<strong>@key.title</strong> (@_root_.ssh.SshUtil.fingerPrint(key.publicKey))
|
||||||
<a href="@path/@account.userName/_ssh/delete/@key.sshKeyId" class="btn btn-mini btn-danger pull-right">Delete</a>
|
<a href="@path/@account.userName/_ssh/delete/@key.sshKeyId" class="btn btn-mini btn-danger pull-right">Delete</a>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user