-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (101 loc) · 2.96 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"net/url"
"github.com/gin-gonic/gin"
"github.com/gotify/plugin-api"
)
const routeSuffix = "authentik"
func GetGotifyPluginInfo() plugin.Info {
return plugin.Info{
Name: "Authentik Plugin",
Description: "Plugin that enables Gotify to receive and understand the webhook structure from Authentik",
ModulePath: "github.com/ckocyigit/gotify-authentik-plugin",
Author: "Can Kocyigit <[email protected]>",
Website: "https://cv.ck98.de",
}
}
type Plugin struct {
userCtx plugin.UserContext
msgHandler plugin.MessageHandler
basePath string
}
func (p *Plugin) Enable() error {
return nil
}
func (p *Plugin) Disable() error {
return nil
}
func (p *Plugin) GetDisplay(location *url.URL) string {
baseHost := ""
if location != nil {
baseHost = fmt.Sprintf("%s://%s", location.Scheme, location.Host)
}
webhookURL := baseHost + p.basePath + routeSuffix
return fmt.Sprintf(`Steps to Configure Authentik Webhooks with Gotify:
Create a Notification Transport in Authentik with the mode 'Webhook (generic)'.
Copy this URL: %s and paste it in 'Webhook URL'.
Keep the 'Webhook Mapping' field empty.
Make sure to enable the 'Send once' option.
Create a Notification Rule:
- Assign the rule to a group, such as 'authentik Admins'.
- Set the newly created transport as the delivery method.
- Select Severity: 'Notice'.
Create and bind two policies:
- Policy 1:
- Action: Login Failed
- App: authentik Core
- The rest stays empty
- Policy 2:
- Action: Login
- App: authentik Core
- The rest stays empty
Other event types are not currently supported for parsing but will still be displayed in Gotify, though without proper parsing.`, webhookURL)
}
func (p *Plugin) SetMessageHandler(h plugin.MessageHandler) {
p.msgHandler = h
}
func (p *Plugin) RegisterWebhook(basePath string, mux *gin.RouterGroup) {
p.basePath = basePath
mux.POST("/"+routeSuffix, p.webhookHandler)
}
func getMarkdownMsg(title string, message string, priority int, host string) plugin.Message {
formattedMessage := fmt.Sprintf("Authentik instance at: %s\n\n```\n%s\n```", host, message)
return plugin.Message{
Title: title,
Message: formattedMessage,
Priority: priority,
Extras: map[string]interface{}{
"client::display": map[string]interface{}{
"contentType": "text/markdown",
},
},
}
}
func (p *Plugin) webhookHandler(c *gin.Context) {
var payload AuthentikWebhookPayload
if err := c.ShouldBindJSON(&payload); err != nil {
p.msgHandler.SendMessage(getMarkdownMsg(
"Error parsing JSON message",
err.Error(),
7,
c.Request.RemoteAddr,
))
return
}
title, message, priority := ReturnGotifyMessageFromAuthentikPayload(payload)
p.msgHandler.SendMessage(getMarkdownMsg(
title,
message,
priority,
c.Request.RemoteAddr,
))
}
func NewGotifyPluginInstance(ctx plugin.UserContext) plugin.Plugin {
return &Plugin{
userCtx: ctx,
}
}
func main() {
panic("this should be built as go plugin")
}