2023-05-26 04:50:41 +00:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"npm/internal/database"
|
|
|
|
"npm/internal/entity"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2023-05-30 03:26:46 +00:00
|
|
|
return "jwt_keys"
|
2023-05-26 04:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
2023-05-26 08:46:55 +00:00
|
|
|
|
2023-05-26 04:50:41 +00:00
|
|
|
return nil
|
|
|
|
}
|