Skip to content

Instantly share code, notes, and snippets.

@praveen001
Last active January 26, 2020 14:38
Show Gist options
  • Save praveen001/c43700d5ef070c408c4f89a58150eeb7 to your computer and use it in GitHub Desktop.
Save praveen001/c43700d5ef070c408c4f89a58150eeb7 to your computer and use it in GitHub Desktop.
Serverless WebSockets with API Gateway and Golang Lambda
// Authorizer custom api authorizer
func Authorizer(request APIGatewayWebsocketProxyRequest) (events.APIGatewayCustomAuthorizerResponse, error) {
token := request.QueryStringParameters["token"]
// Fetch all keys
jwkSet, err := jwk.Fetch("https://cognito-idp.us-east-1.amazonaws.com/us-east-1_vvx4f42sK/.well-known/jwks.json")
if err != nil {
log.Fatalln("Unable to fetch keys")
}
// Verify
t, err := jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
keys := jwkSet.LookupKeyID(t.Header["kid"].(string))
return keys[0].Materialize()
})
if err != nil || !t.Valid {
log.Fatalln("Unauthorized")
}
claims := t.Claims.(jwt.MapClaims)
return events.APIGatewayCustomAuthorizerResponse{
PrincipalID: "me",
PolicyDocument: events.APIGatewayCustomAuthorizerPolicy{
Version: "2012-10-17",
Statement: []events.IAMPolicyStatement{
events.IAMPolicyStatement{
Action: []string{"execute-api:*"},
Effect: "Allow",
Resource: []string{request.MethodArn},
},
},
},
Context: claims,
}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment