Skip to content

Instantly share code, notes, and snippets.

@nathanmalishev
Created March 1, 2020 06:22
Show Gist options
  • Save nathanmalishev/fbf8725ecd691d20310c40ec1c40adbf to your computer and use it in GitHub Desktop.
Save nathanmalishev/fbf8725ecd691d20310c40ec1c40adbf to your computer and use it in GitHub Desktop.
func CreateLambdaContextLocally(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type MyCustomClaims struct {
Username string `json:"cognito:username"`
Email string `json:"email"`
// custom claims etc
jwt.StandardClaims
}
tokenString := r.Header.Get("Authorization")
// if we want to properly pass token must use jwt to parse
token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
set, err := jwk.Fetch("https://cognito-idp.{region}.amazonaws.com/{cognito-id}/.well-known/jwks.json")
// error handling & set key
return key, err
})
if err != nil {
// error
}
// create some new claims
var claims *MyCustomClaims
claims = token.Claims.(*MyCustomClaims)
// Create that new APIGateWayProxyRequest and smack it on the request
test := events.APIGatewayProxyRequest{
RequestContext: events.APIGatewayProxyRequestContext{
Authorizer: map[string]interface{}{"claims": claims},
}}
// create new context - must be able to fetch correct proxy request key
ctx := context.WithValue(r.Context(), GetProxyRequestKey(), test)
r = r.Clone(ctx)
// We pass r onto the next function
next.ServeHTTP(w, r)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment