mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-09 15:05:50 +01:00
(refs #115)Display finger print on the ssh key setting page
This commit is contained in:
@@ -1,23 +1,18 @@
|
||||
package ssh
|
||||
|
||||
import org.apache.sshd.server.PublickeyAuthenticator
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.apache.sshd.server.session.ServerSession
|
||||
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 servlet.Database
|
||||
import javax.servlet.ServletContext
|
||||
|
||||
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 = {
|
||||
Database(context) withTransaction {
|
||||
getPublicKeys(username).exists { sshKey =>
|
||||
str2PublicKey(sshKey.publicKey) match {
|
||||
SshUtil.str2PublicKey(sshKey.publicKey) match {
|
||||
case Some(publicKey) => key.equals(publicKey)
|
||||
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)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user