Created
December 30, 2019 09:13
-
-
Save wingkwong/2bc368c1b5cd116114560d17d4c4de6c to your computer and use it in GitHub Desktop.
A Simple Amazon API Gateway Lambda Authoriser in Go
This file contains hidden or 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 ( | |
"errors" | |
"strings" | |
"github.com/aws/aws-lambda-go/events" | |
"github.com/aws/aws-lambda-go/lambda" | |
jwt "github.com/dgrijalva/jwt-go" | |
"os" | |
) | |
func Auth(request events.APIGatewayCustomAuthorizerRequest) (events.APIGatewayCustomAuthorizerResponse, error) { | |
token := request.AuthorizationToken | |
tokenSlice := strings.Split(token, " ") | |
var tokenString string | |
if len(tokenSlice) > 1 { | |
tokenString = tokenSlice[len(tokenSlice)-1] | |
} | |
k := os.Getenv("JWT_SECRET_KEY") | |
var jwtToken, _ = jwt.Parse(tokenString, func(jwtToken *jwt.Token) (interface{}, error) { | |
// TODO: validate your expected algo here | |
return []byte(k), nil | |
}) | |
if jwtToken != nil && !jwtToken.Valid { | |
return events.APIGatewayCustomAuthorizerResponse{}, errors.New("Unauthorized") | |
} | |
return generatePolicy("user", "Allow", request.MethodArn), nil | |
} | |
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}, | |
}, | |
}, | |
} | |
} | |
return authResponse | |
} | |
func main() { | |
lambda.Start(Auth) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment