Add VAPID public key to instance info API

This commit is contained in:
Vyr Cossont 2024-11-23 20:35:02 -08:00
parent 1ea57b398a
commit b082e53c8c
3 changed files with 28 additions and 1 deletions

View file

@ -172,6 +172,8 @@ type InstanceV2Configuration struct {
Emojis InstanceConfigurationEmojis `json:"emojis"`
// True if instance is running with OIDC as auth/identity backend, else omitted.
OIDCEnabled bool `json:"oidc_enabled,omitempty"`
// Instance VAPID configuration.
VAPID InstanceV2ConfigurationVAPID `json:"vapid"`
}
// Information about registering for this instance.
@ -202,3 +204,11 @@ type InstanceV2Contact struct {
// Key/value not present if no contact account set.
Account *Account `json:"account,omitempty"`
}
// InstanceV2ConfigurationVAPID holds the instance's VAPID configuration.
//
// swagger:model instanceV2ConfigurationVAPID
type InstanceV2ConfigurationVAPID struct {
// The instance's VAPID public key, Base64-encoded.
PublicKey string `json:"public_key"`
}

View file

@ -1753,6 +1753,12 @@ func (c *Converter) InstanceToAPIV2Instance(ctx context.Context, i *gtsmodel.Ins
instance.Configuration.Emojis.EmojiSizeLimit = int(config.GetMediaEmojiLocalMaxSize()) // #nosec G115 -- Already validated.
instance.Configuration.OIDCEnabled = config.GetOIDCEnabled()
vapidKeyPair, err := c.state.DB.GetVAPIDKeyPair(ctx)
if err != nil {
return nil, gtserror.Newf("error getting VAPID key pair: %w", err)
}
instance.Configuration.VAPID.PublicKey = vapidKeyPair.Public
// registrations
instance.Registrations.Enabled = config.GetAccountsRegistrationOpen()
instance.Registrations.ApprovalRequired = true // always required

View file

@ -21,6 +21,7 @@
"bytes"
"context"
"encoding/json"
"strings"
"testing"
"github.com/stretchr/testify/suite"
@ -2051,6 +2052,13 @@ func (suite *InternalToFrontendTestSuite) TestInstanceV2ToFrontend() {
b, err := json.MarshalIndent(instance, "", " ")
suite.NoError(err)
// The VAPID public key changes from run to run.
vapidKeyPair, err := suite.db.GetVAPIDKeyPair(ctx)
if err != nil {
suite.FailNow(err.Error())
}
s := strings.Replace(string(b), vapidKeyPair.Public, "VAPID_PUBLIC_KEY_PLACEHOLDER", 1)
suite.Equal(`{
"domain": "localhost:8080",
"account_domain": "localhost:8080",
@ -2129,6 +2137,9 @@ func (suite *InternalToFrontendTestSuite) TestInstanceV2ToFrontend() {
},
"emojis": {
"emoji_size_limit": 51200
},
"vapid": {
"public_key": "VAPID_PUBLIC_KEY_PLACEHOLDER"
}
},
"registrations": {
@ -2173,7 +2184,7 @@ func (suite *InternalToFrontendTestSuite) TestInstanceV2ToFrontend() {
"rules": [],
"terms": "\u003cp\u003eThis is where a list of terms and conditions might go.\u003c/p\u003e\u003cp\u003eFor example:\u003c/p\u003e\u003cp\u003eIf you want to sign up on this instance, you oughta know that we:\u003c/p\u003e\u003col\u003e\u003cli\u003eWill sell your data to whoever offers.\u003c/li\u003e\u003cli\u003eSecure the server with password \u003ccode\u003epassword\u003c/code\u003e wherever possible.\u003c/li\u003e\u003c/ol\u003e",
"terms_text": "This is where a list of terms and conditions might go.\n\nFor example:\n\nIf you want to sign up on this instance, you oughta know that we:\n\n1. Will sell your data to whoever offers.\n2. Secure the server with password `+"`"+`password`+"`"+` wherever possible."
}`, string(b))
}`, s)
}
func (suite *InternalToFrontendTestSuite) TestEmojiToFrontend() {