Verified Commit 24e1c3dc authored by Loïc Dachary's avatar Loïc Dachary
Browse files

activitypub: hack_16834


Signed-off-by: Loïc Dachary's avatarLoïc Dachary <loic@dachary.org>
parent 8a3c8657
// 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 string, priv string, err error) {
var settings map[string]*user_model.Setting
if settings, err = user_model.GetSettings(user.ID, []string{"activitypub_privpem", "activitypub_pubpem"}); err != nil {
return
} else if len(settings) == 0 {
if priv, pub, err = GenerateKeyPair(); err != nil {
return
}
privPem := &user_model.Setting{UserID: user.ID, SettingKey: "activitypub_privpem", SettingValue: priv}
if err = user_model.SetSetting(privPem); err != nil {
return
}
pubPem := &user_model.Setting{UserID: user.ID, SettingKey: "activitypub_pubpem", SettingValue: pub}
if err = user_model.SetSetting(pubPem); 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
}
// 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)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment