generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
196 lines (162 loc) · 4.81 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"go.uber.org/zap/zapcore"
"github.com/mattermost/mattermost-plugin-apps/apps"
"github.com/mattermost/mattermost-plugin-apps/utils"
"github.com/mattermost/mattermost-plugin-apps/utils/httputils"
)
const (
AppSecret = "1234"
CommandTrigger = "test"
)
var IncludeInvalid bool
var Log = utils.MustMakeCommandLogger(zapcore.DebugLevel)
var ErrUnexpectedSignMethod = errors.New("unexpected signing method")
var ErrMissingHeader = errors.Errorf("missing %s: Bearer header", apps.OutgoingAuthHeader)
var ErrActingUserMismatch = errors.New("JWT claim doesn't match actingUser.Id in context")
type callHandler func(*apps.CallRequest) apps.CallResponse
func main() {
rootURL := os.Getenv("ROOT_URL")
if rootURL == "" {
rootURL = AppManifest.Deploy.HTTP.RootURL
}
if os.Getenv("INCLUDE_INVALID") != "" {
IncludeInvalid = true
}
portStr := os.Getenv("PORT")
if portStr == "" && rootURL != "" {
// Get the port from the original manifest's root_url.
u, err := url.Parse(rootURL)
if err != nil {
panic(err)
}
portStr = u.Port()
if portStr == "" {
portStr = "8080"
}
}
listen := ":" + portStr
if rootURL == "" {
rootURL = "http://localhost" + listen
}
AppManifest.Deploy.HTTP.RootURL = rootURL
r := mux.NewRouter()
r.HandleFunc(ManifestPath, httputils.DoHandleJSON(AppManifest))
r.PathPrefix(StaticPath).Handler(http.StripPrefix("/", http.FileServer(http.FS(StaticFS))))
handleCall(r, InstallPath, handleInstall)
handleCall(r, BindingsPath, handleBindings)
initHTTPEmbedded(r)
initHTTPError(r)
initHTTPForms(r)
initHTTPLookup(r)
initHTTPNavigate(r)
initHTTPOK(r)
initNumBindingsCommand(r)
initHTTPSubscriptions(r)
r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := errors.Errorf("path not found: %s", r.URL.Path)
_ = httputils.WriteJSON(w, apps.NewErrorResponse(err))
})
server := &http.Server{
Addr: listen,
ReadHeaderTimeout: 3 * time.Second,
WriteTimeout: 5 * time.Second,
Handler: r,
}
Log.Infof("test app started, listening on port %s, manifest at %s/manifest.json. Use %s as the JWT secret.", portStr, AppManifest.Deploy.HTTP.RootURL, AppSecret)
panic(server.ListenAndServe())
}
func handle(f callHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
creq, err := apps.CallRequestFromJSONReader(r.Body)
if err != nil {
httputils.WriteErrorIfNeeded(w, utils.NewInvalidError(err))
return
}
err = checkJWT(r, creq)
if err != nil {
httputils.WriteErrorIfNeeded(w, utils.NewInvalidError(err))
return
}
cresp := f(creq)
log := Log.With("req", creq.Path, "resp", cresp.Type)
if creq.Context.Subject != "" {
log = log.With("subject", creq.Context.Subject)
}
if len(creq.Values) > 0 {
log = log.With("values", creq.Values)
}
if cresp.Text != "" {
log = log.With("text", cresp.Text)
}
if cresp.Data != nil {
log = log.With("data", cresp.Data)
}
if cresp.Form != nil {
log = log.With("form", cresp.Form)
}
log.Debugw("processed call")
_ = httputils.WriteJSON(w, cresp)
}
}
func handleCall(router *mux.Router, path string, f callHandler) {
router.HandleFunc(path, handle(f))
}
func handleError(text string) callHandler {
return func(_ *apps.CallRequest) apps.CallResponse {
return apps.CallResponse{
Type: apps.CallResponseTypeError,
Text: text,
}
}
}
func handleForm(f apps.Form) callHandler {
return func(_ *apps.CallRequest) apps.CallResponse {
return apps.NewFormResponse(f)
}
}
type lookupResponse struct {
Items []apps.SelectOption `json:"items"`
}
func handleLookup(items []apps.SelectOption) callHandler {
return func(creq *apps.CallRequest) apps.CallResponse {
query := strings.ToLower(creq.Query)
finalItems := []apps.SelectOption{}
for _, item := range items {
if strings.Contains(strings.ToLower(item.Label), query) {
finalItems = append(finalItems, item)
}
}
return apps.NewDataResponse(lookupResponse{finalItems})
}
}
func checkJWT(req *http.Request, creq *apps.CallRequest) error {
authValue := req.Header.Get(apps.OutgoingAuthHeader)
if !strings.HasPrefix(authValue, "Bearer ") {
return ErrMissingHeader
}
jwtoken := strings.TrimPrefix(authValue, "Bearer ")
claims := apps.JWTClaims{}
_, err := jwt.ParseWithClaims(jwtoken, &claims, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("%w: %v", ErrUnexpectedSignMethod, token.Header["alg"])
}
return []byte(AppSecret), nil
})
if err != nil {
return err
}
if creq.Context.ActingUser != nil && creq.Context.ActingUser.Id != claims.ActingUserID {
return utils.NewInvalidError(ErrActingUserMismatch)
}
return nil
}