mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-11-10 05:57:29 -06:00
Single-row table for storing instance's VAPID key pair
This commit is contained in:
parent
c4012b6f15
commit
7a02a19c3c
4 changed files with 116 additions and 0 deletions
|
|
@ -27,6 +27,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
webpushgo "github.com/SherClockHolmes/webpush-go"
|
||||
"github.com/google/uuid"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/config"
|
||||
|
|
@ -442,6 +443,38 @@ func (a *adminDB) CountUnhandledSignups(ctx context.Context) (int, error) {
|
|||
Count(ctx)
|
||||
}
|
||||
|
||||
func (a *adminDB) GetOrCreateVAPIDKeyPair(ctx context.Context) (*gtsmodel.VAPIDKeyPair, error) {
|
||||
var err error
|
||||
var vapidKeyPair *gtsmodel.VAPIDKeyPair
|
||||
|
||||
// Look for previously generated keys.
|
||||
if err = a.db.NewSelect().
|
||||
Model(vapidKeyPair).
|
||||
Limit(1).
|
||||
Scan(ctx); // nocollapse
|
||||
err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
return nil, gtserror.Newf("DB error getting VAPID key pair: %w", err)
|
||||
}
|
||||
|
||||
if vapidKeyPair == nil {
|
||||
// Generate new keys.
|
||||
vapidKeyPair = >smodel.VAPIDKeyPair{}
|
||||
if vapidKeyPair.Private, vapidKeyPair.Public, err = webpushgo.GenerateVAPIDKeys(); err != nil {
|
||||
return nil, gtserror.Newf("error generating VAPID key pair: %w", err)
|
||||
}
|
||||
|
||||
// Save them to the database.
|
||||
if _, err = a.db.NewInsert().
|
||||
Model(vapidKeyPair).
|
||||
Exec(ctx); // nocollapse
|
||||
err != nil {
|
||||
return nil, gtserror.Newf("DB error saving VAPID key pair: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return vapidKeyPair, err
|
||||
}
|
||||
|
||||
/*
|
||||
ACTION FUNCS
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue