diff options
author | Loïc Dachary | 2021-12-24 16:47:43 +0100 |
---|---|---|
committer | Anthony Wang | 2022-03-18 17:34:10 -0500 |
commit | b342241abcdac28adc3bb4f03eb75f44f3f16150 (patch) | |
tree | ab987d2716842fbef001a327fbb53da5639d724d | |
parent | 97fedf26169bca28cb310014e73be494d495a650 (diff) |
activitypub: hack_16834
Signed-off-by: Loïc Dachary <loic@dachary.org>
-rw-r--r-- | modules/activitypub/hack_16834.go | 44 | ||||
-rw-r--r-- | modules/activitypub/hack_16834_test.go | 27 |
2 files changed, 71 insertions, 0 deletions
diff --git a/modules/activitypub/hack_16834.go b/modules/activitypub/hack_16834.go new file mode 100644 index 000000000..b3537c6d3 --- /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 000000000..8982afb7b --- /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) +} |