Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split up the auth function and changed the repo to be examples for fu…
…ture auth stuff instead of just a spike
- Loading branch information
Showing
5 changed files
with
90 additions
and
62 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package util | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/dgrijalva/jwt-go" | ||
"github.com/lestrrat/go-jwx/jwk" | ||
) | ||
|
||
func GetKey(region string, userPoolId string) jwt.Keyfunc { | ||
return func(token *jwt.Token) (interface{}, error) { | ||
jwksURL := fmt.Sprintf("https://cognito-idp.%s.amazonaws.com/%s/.well-known/jwks.json", region, userPoolId) | ||
|
||
fmt.Printf("JWKS URL: %s", jwksURL) | ||
// TODO: cache response so we don't have to make a request every time | ||
// must be set manually in API Gateway Authorizers (checkbox) | ||
set, err := jwk.FetchHTTP(jwksURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
keyID, ok := token.Header["kid"].(string) | ||
if !ok { | ||
return nil, errors.New("expecting JWT header to have string kid") | ||
} | ||
|
||
if key := set.LookupKeyID(keyID); len(key) == 1 { | ||
return key[0].Materialize() | ||
} | ||
|
||
return nil, errors.New("unable to find key") | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package util | ||
|
||
import ( | ||
"github.com/aws/aws-lambda-go/events" | ||
) | ||
|
||
func GenerateLambdaInvokePolicy(principalId string, effect string, 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}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
authResponse.Context = map[string]interface{}{} | ||
return authResponse | ||
} |