api/pkg/notifications/webhook.go

57 lines
1.8 KiB
Go

// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package notifications
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"code.vikunja.io/api/pkg/config"
)
const (
webhookNotificationSignatureHeader = "x-vikunja-signature"
webhookNotificationContentTypeHeader = "Content-Type"
)
// WebhookNotification represents a notification that published to a http endpoint
type WebhookNotification struct {
endpointURL string
headers map[string]string
body string
}
// GenerateHMAC generates a HMAC code for the webhook body signed with the configured secret
func GenerateHMAC(requestBody string) string {
h := hmac.New(sha256.New, []byte(config.WebhookNotificationSignatureSecret.GetString()))
h.Write([]byte(requestBody))
return hex.EncodeToString(h.Sum(nil))
}
// SendWebhook sends the notification to the configured endpoint
func SendWebhook(w *WebhookNotification) error {
resp, err := DoPost(w.endpointURL, w.body, w.headers)
if err != nil {
return err
}
defer resp.Body.Close()
// TODO: Have response-code based checks
return nil
}