From b342241abcdac28adc3bb4f03eb75f44f3f16150 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dachary?= Date: Fri, 24 Dec 2021 16:47:43 +0100 Subject: [PATCH] activitypub: hack_16834 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Loïc Dachary --- modules/activitypub/hack_16834.go | 44 ++++++++++++++++++++++++++ modules/activitypub/hack_16834_test.go | 27 ++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 modules/activitypub/hack_16834.go create mode 100644 modules/activitypub/hack_16834_test.go diff --git a/modules/activitypub/hack_16834.go b/modules/activitypub/hack_16834.go new file mode 100644 index 0000000000..b3537c6d32 --- /dev/null +++ b/modules/activitypub/hack_16834.go @@ -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 +} diff --git a/modules/activitypub/hack_16834_test.go b/modules/activitypub/hack_16834_test.go new file mode 100644 index 0000000000..8982afb7b6 --- /dev/null +++ b/modules/activitypub/hack_16834_test.go @@ -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) +}