@@ -13,34 +13,7 @@ import (
13
13
"github.com/mantil-io/mantil/kit/aws"
14
14
)
15
15
16
- func generatePolicy (principalId , effect , resource string ) * events.APIGatewayCustomAuthorizerResponse {
17
- rsp := events.APIGatewayCustomAuthorizerResponse {PrincipalID : principalId }
18
-
19
- if effect != "" && resource != "" {
20
- rsp .PolicyDocument = events.APIGatewayCustomAuthorizerPolicy {
21
- Version : "2012-10-17" ,
22
- Statement : []events.IAMPolicyStatement {
23
- {
24
- Action : []string {"execute-api:Invoke" },
25
- Effect : effect ,
26
- Resource : []string {resource },
27
- },
28
- },
29
- }
30
- }
31
- return & rsp
32
- }
33
-
34
- func allow (req * events.APIGatewayCustomAuthorizerRequestTypeRequest ) * events.APIGatewayCustomAuthorizerResponse {
35
- return generatePolicy ("Mantil" , "Allow" , req .MethodArn )
36
- }
37
-
38
- func errorResponse (err error ) (* events.APIGatewayCustomAuthorizerResponse , error ) {
39
- log .Print (err )
40
- return nil , err
41
- }
42
-
43
- func handleRequest (ctx context.Context , req * events.APIGatewayCustomAuthorizerRequestTypeRequest ) (* events.APIGatewayCustomAuthorizerResponse , error ) {
16
+ func handleRequest (ctx context.Context , req * events.APIGatewayCustomAuthorizerRequestTypeRequest ) (* events.APIGatewayV2CustomAuthorizerSimpleResponse , error ) {
44
17
buf , _ := json .Marshal (req )
45
18
log .Printf ("req %s" , buf )
46
19
@@ -52,11 +25,9 @@ func handleRequest(ctx context.Context, req *events.APIGatewayCustomAuthorizerRe
52
25
if err != nil {
53
26
return errorResponse (fmt .Errorf ("read runtime access token error %w" , err ))
54
27
}
55
- rsp := allow (req )
56
- if rsp .Context == nil {
57
- rsp .Context = make (map [string ]interface {})
58
- }
59
- domain .StoreUserClaims (claims , rsp .Context )
28
+ rsp := allowResponse (claims )
29
+ buf , _ = json .Marshal (rsp )
30
+ log .Printf ("rsp %s" , buf )
60
31
return rsp , nil
61
32
}
62
33
@@ -80,6 +51,20 @@ func publicKey() (string, error) {
80
51
return pk , nil
81
52
}
82
53
54
+ func allowResponse (claims * domain.AccessTokenClaims ) * events.APIGatewayV2CustomAuthorizerSimpleResponse {
55
+ rsp := & events.APIGatewayV2CustomAuthorizerSimpleResponse {
56
+ IsAuthorized : true ,
57
+ Context : make (map [string ]interface {}),
58
+ }
59
+ domain .StoreUserClaims (claims , rsp .Context )
60
+ return rsp
61
+ }
62
+
63
+ func errorResponse (err error ) (* events.APIGatewayV2CustomAuthorizerSimpleResponse , error ) {
64
+ log .Print (err )
65
+ return nil , err
66
+ }
67
+
83
68
func main () {
84
69
lambda .Start (handleRequest )
85
70
}
0 commit comments