mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-07-04 17:06:49 +00:00
Move jwt keys to database
Moved code for it to one place Updated to chi v5
This commit is contained in:
@ -1,11 +1,14 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/asn1"
|
||||
"encoding/pem"
|
||||
|
||||
"npm/internal/config"
|
||||
"npm/internal/logger"
|
||||
|
||||
"github.com/rotisserie/eris"
|
||||
)
|
||||
@ -21,12 +24,12 @@ func GetPrivateKey() (*rsa.PrivateKey, error) {
|
||||
if privateKey == nil {
|
||||
var blankKey *rsa.PrivateKey
|
||||
|
||||
if config.PrivateKey == "" {
|
||||
if currentKeys.PrivateKey == "" {
|
||||
return blankKey, eris.New("Could not get Private Key from configuration")
|
||||
}
|
||||
|
||||
var err error
|
||||
privateKey, err = LoadPemPrivateKey(config.PrivateKey)
|
||||
privateKey, err = LoadPemPrivateKey(currentKeys.PrivateKey)
|
||||
if err != nil {
|
||||
return blankKey, err
|
||||
}
|
||||
@ -48,12 +51,12 @@ func GetPublicKey() (*rsa.PublicKey, error) {
|
||||
if publicKey == nil {
|
||||
var blankKey *rsa.PublicKey
|
||||
|
||||
if config.PublicKey == "" {
|
||||
return blankKey, eris.New("Could not get Public Key filename, check environment variables")
|
||||
if currentKeys.PublicKey == "" {
|
||||
return blankKey, eris.New("Could not get Public Key from configuration")
|
||||
}
|
||||
|
||||
var err error
|
||||
publicKey, err = LoadPemPublicKey(config.PublicKey)
|
||||
publicKey, err = LoadPemPublicKey(currentKeys.PublicKey)
|
||||
if err != nil {
|
||||
return blankKey, err
|
||||
}
|
||||
@ -85,3 +88,48 @@ func LoadPemPublicKey(content string) (*rsa.PublicKey, error) {
|
||||
|
||||
return publicKeyFileImported, nil
|
||||
}
|
||||
|
||||
func generateKeys() (KeysModel, error) {
|
||||
m := KeysModel{}
|
||||
reader := rand.Reader
|
||||
bitSize := 4096
|
||||
|
||||
key, err := rsa.GenerateKey(reader, bitSize)
|
||||
if err != nil {
|
||||
return m, err
|
||||
}
|
||||
|
||||
privateKey := &pem.Block{
|
||||
Type: "PRIVATE KEY",
|
||||
Bytes: x509.MarshalPKCS1PrivateKey(key),
|
||||
}
|
||||
|
||||
privateKeyBuffer := new(bytes.Buffer)
|
||||
err = pem.Encode(privateKeyBuffer, privateKey)
|
||||
if err != nil {
|
||||
return m, err
|
||||
}
|
||||
|
||||
asn1Bytes, err := asn1.Marshal(key.PublicKey)
|
||||
if err != nil {
|
||||
return m, err
|
||||
}
|
||||
|
||||
publicKey := &pem.Block{
|
||||
Type: "PUBLIC KEY",
|
||||
Bytes: asn1Bytes,
|
||||
}
|
||||
|
||||
publicKeyBuffer := new(bytes.Buffer)
|
||||
err = pem.Encode(publicKeyBuffer, publicKey)
|
||||
if err != nil {
|
||||
return m, err
|
||||
}
|
||||
|
||||
m.PublicKey = publicKeyBuffer.String()
|
||||
m.PrivateKey = privateKeyBuffer.String()
|
||||
|
||||
logger.Info("Generated new RSA keys")
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
54
backend/internal/jwt/keys_db.go
Normal file
54
backend/internal/jwt/keys_db.go
Normal file
@ -0,0 +1,54 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"npm/internal/database"
|
||||
"npm/internal/entity"
|
||||
"npm/internal/logger"
|
||||
)
|
||||
|
||||
var currentKeys KeysModel
|
||||
|
||||
// KeysModel is the model
|
||||
type KeysModel struct {
|
||||
entity.ModelBase
|
||||
PublicKey string `gorm:"column:public_key"`
|
||||
PrivateKey string `gorm:"column:private_key"`
|
||||
}
|
||||
|
||||
// TableName overrides the table name used by gorm
|
||||
func (KeysModel) TableName() string {
|
||||
return "keys"
|
||||
}
|
||||
|
||||
// LoadByID will load from an ID
|
||||
func (m *KeysModel) LoadLatest() error {
|
||||
db := database.GetDB()
|
||||
result := db.Order("created_at DESC").First(&m)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// Save will save this model to the DB
|
||||
func (m *KeysModel) Save() error {
|
||||
db := database.GetDB()
|
||||
result := db.Save(m)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// LoadKeys will load from the database, or generate and save new ones
|
||||
func LoadKeys() error {
|
||||
// Try to find in db
|
||||
if err := currentKeys.LoadLatest(); err != nil {
|
||||
// Keys probably don't exist, so we need to generate some
|
||||
if currentKeys, err = generateKeys(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// and save them
|
||||
if err = currentKeys.Save(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
logger.Debug("private: %s", currentKeys.PrivateKey)
|
||||
logger.Debug("public: %s", currentKeys.PublicKey)
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user