From 5b151f4ec4d74432ac4189e470e454204e7eb2d5 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Mon, 13 Nov 2023 15:57:46 +0800 Subject: [PATCH] feat: improve cert edit page UI --- object/cert.go | 27 +++++++++++++++++++++---- object/token_jwt_key.go | 9 +++++---- object/token_jwt_key_test.go | 5 ++++- web/src/CertEditPage.js | 38 +++++++++++++++++++++++++++++------- web/src/Setting.js | 32 ++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 16 deletions(-) diff --git a/object/cert.go b/object/cert.go index 0ff5062f..a7da8758 100644 --- a/object/cert.go +++ b/object/cert.go @@ -163,6 +163,12 @@ func UpdateCert(id string, cert *Cert) (bool, error) { return false, err } } + + err := cert.populateContent() + if err != nil { + return false, err + } + affected, err := ormer.Engine.ID(core.PK{owner, name}).AllCols().Update(cert) if err != nil { return false, err @@ -172,10 +178,9 @@ func UpdateCert(id string, cert *Cert) (bool, error) { } func AddCert(cert *Cert) (bool, error) { - if cert.Certificate == "" || cert.PrivateKey == "" { - certificate, privateKey := generateRsaKeys(cert.BitSize, cert.ExpireInYears, cert.Name, cert.Owner) - cert.Certificate = certificate - cert.PrivateKey = privateKey + err := cert.populateContent() + if err != nil { + return false, err } affected, err := ormer.Engine.Insert(cert) @@ -199,6 +204,20 @@ func (p *Cert) GetId() string { return fmt.Sprintf("%s/%s", p.Owner, p.Name) } +func (p *Cert) populateContent() error { + if p.Certificate == "" || p.PrivateKey == "" { + certificate, privateKey, err := generateRsaKeys(p.BitSize, p.ExpireInYears, p.Name, p.Owner) + if err != nil { + return err + } + + p.Certificate = certificate + p.PrivateKey = privateKey + } + + return nil +} + func getCertByApplication(application *Application) (*Cert, error) { if application.Cert != "" { return getCertByName(application.Cert) diff --git a/object/token_jwt_key.go b/object/token_jwt_key.go index 7091c14f..a553a2d1 100644 --- a/object/token_jwt_key.go +++ b/object/token_jwt_key.go @@ -24,14 +24,14 @@ import ( "time" ) -func generateRsaKeys(bitSize int, expireInYears int, commonName string, organization string) (string, string) { +func generateRsaKeys(bitSize int, expireInYears int, commonName string, organization string) (string, string, error) { // https://stackoverflow.com/questions/64104586/use-golang-to-get-rsa-key-the-same-way-openssl-genrsa // https://stackoverflow.com/questions/43822945/golang-can-i-create-x509keypair-using-rsa-key // Generate RSA key. key, err := rsa.GenerateKey(rand.Reader, bitSize) if err != nil { - panic(err) + return "", "", err } // Encode private key to PKCS#1 ASN.1 PEM. @@ -54,9 +54,10 @@ func generateRsaKeys(bitSize int, expireInYears int, commonName string, organiza }, BasicConstraintsValid: true, } + cert, err := x509.CreateCertificate(rand.Reader, &tml, &tml, &key.PublicKey, key) if err != nil { - panic(err) + return "", "", err } // Generate a pem block with the certificate @@ -65,5 +66,5 @@ func generateRsaKeys(bitSize int, expireInYears int, commonName string, organiza Bytes: cert, }) - return string(certPem), string(privateKeyPem) + return string(certPem), string(privateKeyPem), nil } diff --git a/object/token_jwt_key_test.go b/object/token_jwt_key_test.go index 00b1e571..4e110672 100644 --- a/object/token_jwt_key_test.go +++ b/object/token_jwt_key_test.go @@ -23,7 +23,10 @@ import ( func TestGenerateRsaKeys(t *testing.T) { fileId := "token_jwt_key" - certificate, privateKey := generateRsaKeys(4096, 20, "Casdoor Cert", "Casdoor Organization") + certificate, privateKey, err := generateRsaKeys(4096, 20, "Casdoor Cert", "Casdoor Organization") + if err != nil { + panic(err) + } // Write certificate (aka certificate) to file. util.WriteStringToPath(certificate, fmt.Sprintf("%s.pem", fileId)) diff --git a/web/src/CertEditPage.js b/web/src/CertEditPage.js index ecc6eb69..33f2eac3 100644 --- a/web/src/CertEditPage.js +++ b/web/src/CertEditPage.js @@ -171,10 +171,27 @@ class CertEditPage extends React.Component { @@ -185,9 +202,15 @@ class CertEditPage extends React.Component { {Setting.getLabel(i18next.t("cert:Bit size"), i18next.t("cert:Bit size - Tooltip"))} : - { + @@ -205,14 +228,14 @@ class CertEditPage extends React.Component { {Setting.getLabel(i18next.t("cert:Certificate"), i18next.t("cert:Certificate - Tooltip"))} : - - -