Created
May 4, 2022 12:52
-
-
Save dsandor/ef2a979fa7babf4720e97caa8a394c7a to your computer and use it in GitHub Desktop.
Golang AWS API Gateway Lambda Authorizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"github.com/aws/aws-lambda-go/events" | |
) | |
func generatePolicy(principalId, effect, resource string) events.APIGatewayCustomAuthorizerResponse { | |
authResponse := events.APIGatewayCustomAuthorizerResponse{PrincipalID: principalId} | |
if effect != "" && resource != "" { | |
authResponse.PolicyDocument = events.APIGatewayCustomAuthorizerPolicy{ | |
Version: "2012-10-17", | |
Statement: []events.IAMPolicyStatement{ | |
{ | |
Action: []string{"execute-api:Invoke"}, | |
Effect: effect, | |
Resource: []string{resource}, | |
}, | |
}, | |
} | |
} | |
// Optional output with custom properties of the String, Number or Boolean type. | |
authResponse.Context = map[string]interface{}{ | |
"stringKey": "stringval", | |
"numberKey": 123, | |
"booleanKey": true, | |
} | |
return authResponse | |
} | |
func (d *dependencies) auth(ctx context.Context, websocketEvent events.APIGatewayCustomAuthorizerRequest) (events.APIGatewayCustomAuthorizerResponse, error) { | |
fmt.Println("Auth handler hit.") | |
fmt.Printf("%+v\n", websocketEvent) | |
policy := generatePolicy("user", "Allow", websocketEvent.MethodArn) | |
fmt.Printf("policy: %+v\n", policy) | |
return policy, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment