activitypub: hack_16834

Signed-off-by: Loïc Dachary <loic@dachary.org>
This commit is contained in:
Loïc Dachary 2021-12-24 16:47:43 +01:00 committed by Anthony Wang
parent 97fedf2616
commit b342241abc
No known key found for this signature in database
GPG Key ID: BC96B00AEC5F2D76
2 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,44 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package activitypub
import (
user_model "code.gitea.io/gitea/models/user"
)
// GetKeyPair function
func GetKeyPair(user *user_model.User) (pub, priv string, err error) {
var settings map[string]*user_model.Setting
if settings, err = user_model.GetUserSettings(user.ID, []string{"activitypub_privpem", "activitypub_pubpem"}); err != nil {
return
} else if len(settings) == 0 {
if priv, pub, err = GenerateKeyPair(); err != nil {
return
}
if err = user_model.SetUserSetting(user.ID, "activitypub_privpem", priv); err != nil {
return
}
if err = user_model.SetUserSetting(user.ID, "activitypub_pubpem", pub); err != nil {
return
}
return
} else {
priv = settings["activitypub_privpem"].SettingValue
pub = settings["activitypub_pubpem"].SettingValue
return
}
}
// GetPublicKey function
func GetPublicKey(user *user_model.User) (pub string, err error) {
pub, _, err = GetKeyPair(user)
return
}
// GetPrivateKey function
func GetPrivateKey(user *user_model.User) (priv string, err error) {
_, priv, err = GetKeyPair(user)
return
}

View File

@ -0,0 +1,27 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package activitypub
import (
"testing"
_ "code.gitea.io/gitea/models" // https://discourse.gitea.io/t/testfixtures-could-not-clean-table-access-no-such-table-access/4137/4
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"github.com/stretchr/testify/assert"
)
func TestHack16834(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
pub, priv, err := GetKeyPair(user1)
assert.NoError(t, err)
pub1, err := GetPublicKey(user1)
assert.NoError(t, err)
assert.Equal(t, pub, pub1)
priv1, err := GetPrivateKey(user1)
assert.NoError(t, err)
assert.Equal(t, priv, priv1)
}