mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"net/http"
|
|
|
|
"github.com/mattermost/mattermost-server/mlog"
|
|
"github.com/mattermost/mattermost-server/model"
|
|
)
|
|
|
|
func getKeyHash(key string) string {
|
|
hash := sha256.New()
|
|
hash.Write([]byte(key))
|
|
return base64.StdEncoding.EncodeToString(hash.Sum(nil))
|
|
}
|
|
|
|
func (a *App) SetPluginKey(pluginId string, key string, value []byte) *model.AppError {
|
|
kv := &model.PluginKeyValue{
|
|
PluginId: pluginId,
|
|
Key: getKeyHash(key),
|
|
Value: value,
|
|
}
|
|
|
|
result := <-a.Srv.Store.Plugin().SaveOrUpdate(kv)
|
|
|
|
if result.Err != nil {
|
|
mlog.Error(result.Err.Error())
|
|
}
|
|
|
|
return result.Err
|
|
}
|
|
|
|
func (a *App) GetPluginKey(pluginId string, key string) ([]byte, *model.AppError) {
|
|
result := <-a.Srv.Store.Plugin().Get(pluginId, getKeyHash(key))
|
|
|
|
if result.Err != nil {
|
|
if result.Err.StatusCode == http.StatusNotFound {
|
|
return nil, nil
|
|
}
|
|
mlog.Error(result.Err.Error())
|
|
return nil, result.Err
|
|
}
|
|
|
|
kv := result.Data.(*model.PluginKeyValue)
|
|
|
|
return kv.Value, nil
|
|
}
|
|
|
|
func (a *App) DeletePluginKey(pluginId string, key string) *model.AppError {
|
|
result := <-a.Srv.Store.Plugin().Delete(pluginId, getKeyHash(key))
|
|
|
|
if result.Err != nil {
|
|
mlog.Error(result.Err.Error())
|
|
}
|
|
|
|
return result.Err
|
|
}
|