-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcognito.go
More file actions
158 lines (138 loc) · 5.69 KB
/
cognito.go
File metadata and controls
158 lines (138 loc) · 5.69 KB
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
// Copyright © 2016 Tom Maiaroto <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package framework
import (
"context"
"errors"
"log"
"github.com/aws/aws-lambda-go/lambda"
)
// CognitoRouter struct provides an interface to handle Cognito trigger events (routers can be for a specific pool or any)
type CognitoRouter struct {
handlers map[string]CognitoHandler
PoolID string
Tracer TraceStrategy
}
// CognitoHandler handles routed trigger events, note that these must return a map[string]interface{} response
type CognitoHandler func(context.Context, *HandlerDependencies, map[string]interface{}) (map[string]interface{}, error)
// LambdaHandler handles Cognito trigger events.
func (r *CognitoRouter) LambdaHandler(ctx context.Context, d *HandlerDependencies, evt map[string]interface{}) (map[string]interface{}, error) {
// If this Router had a Tracer set for it, replace the default which came from the Aegis interface.
if r.Tracer != nil {
d.Tracer = r.Tracer
}
var err error
handled := false
userPoolID := evt["userPoolId"].(string)
triggerSource := evt["triggerSource"].(string)
userName := evt["userName"].(string)
// The trigger typically comes with a default response.
// Maybe that can be used if an empty result is returned from handler??
// What if an empty map is intended?
// defaultResponse := evt["response"].(map[string]interface{})
var response map[string]interface{}
// If an incoming event can be matched to this router, but the router has no registered handlers
// or if one hasn't been added to aegis.Handlers{}.
if r == nil {
return response, errors.New("no handlers registered for CognitoRouter")
}
if r.PoolID == "" || r.PoolID == userPoolID {
if handler, ok := r.handlers[triggerSource]; ok {
handled = true
r.Tracer.Record("annotation",
map[string]interface{}{
"CognitoUserPoolID": userPoolID,
"CognitoTriggerSource": triggerSource,
"UserName": userName,
},
)
err = r.Tracer.Capture(ctx, "CognitoHandler", func(ctx1 context.Context) error {
response, err = handler(ctx1, d, evt)
return err
})
}
}
// Otherwise, use the catch all (router "fallthrough" equivalent) handler.
// The application can inspect the map and make a decision on what to do, if anything.
// This is optional.
if !handled {
log.Println("using default fall through handler")
// It's possible that the CognitoRouter wasn't created with NewCognitoRouter, so check for this still.
if handler, ok := r.handlers["_"]; ok {
// Capture the handler (in XRay by default) automatically
r.Tracer.Record("annotation",
map[string]interface{}{
"CognitoUserPoolID": userPoolID,
"CognitoTriggerSource": triggerSource,
"UserName": userName,
"FallthroughHandler": true,
},
)
err = r.Tracer.Capture(ctx, "CognitoHandler", func(ctx1 context.Context) error {
response, err = handler(ctx, d, evt)
return err
})
}
}
return response, err
}
// Listen will start an S3 even listener that handles incoming object based events (put, delete, etc.)
func (r *CognitoRouter) Listen() {
lambda.Start(r.LambdaHandler)
}
// NewCognitoRouter simply returns a new CognitoRouter struct and behaves a bit like Router, it even takes an optional rootHandler or "fall through" catch all
func NewCognitoRouter(rootHandler ...CognitoHandler) *CognitoRouter {
// The catch all is optional, if not provided, an empty handler is still called and it returns nothing.
handler := func(context.Context, *HandlerDependencies, map[string]interface{}) (map[string]interface{}, error) {
return map[string]interface{}{}, nil
}
if len(rootHandler) > 0 {
handler = rootHandler[0]
}
return &CognitoRouter{
handlers: map[string]CognitoHandler{
"_": handler,
},
}
}
// NewCognitoRouterForPool is the same as NewCognitoRouter except it's for a specific pool ID (you could also set the PoolID field after using the other function)
func NewCognitoRouterForPool(poolID string, rootHandler ...CognitoHandler) *CognitoRouter {
var r *CognitoRouter
if len(rootHandler) > 0 {
r = NewCognitoRouter(rootHandler[0])
} else {
r = NewCognitoRouter()
}
// Just convenience
r.PoolID = poolID
return r
}
// Handle will register a handler for a given Cognito trigger source
// List of triggerSources here: https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-lambda-trigger-syntax-shared.html
func (r *CognitoRouter) Handle(triggerSource string, handler func(context.Context, *HandlerDependencies, map[string]interface{}) (map[string]interface{}, error)) {
if r.handlers == nil {
r.handlers = make(map[string]CognitoHandler)
}
r.handlers[triggerSource] = handler
}
// PreSignUp is the same as Handle only the triggerSource is already implied. It handles any PreSignUp_SignUp event.
func (r *CognitoRouter) PreSignUp(handler func(context.Context, *HandlerDependencies, map[string]interface{}) (map[string]interface{}, error)) {
r.Handle("PreSignUp_SignUp", handler)
}
// Too many?
// CustomMessage_ResendCode
// func (r *CognitoRouter) ResendCodeMessage(handler func(context.Context, *map[string]interface{}) error) {
// r.Handle("CustomMessage_ResendCode", handler)
// }
// PreAuthentication_Authentication