Question about writing a Cognito post-auth lambda in go

0

I wrote a really simple post-auth lambda for cognito like this:

func HandleRequest(ctx context.Context, e events.CognitoEventUserPoolsPostAuthentication ) error {
	eventJson, _ := json.MarshalIndent(e, "", "  ")
	eventString := strings.ReplaceAll(string(eventJson), "\n", "\r")

	log.Printf("EVENT: %s", eventString)
	return nil
}

func main() {
	lambda.Start(HandleRequest)
}

It's only there - for now - to log activity. But when I actually attach it to the user pool as a post-authentication trigger it makes OAuth2 not work. The error message is "contact administrator."

I'm thinking that I need to actually do something besides return error = nil. I'm not having much luck finding any examples of this in go, though.

The javascript example ends with:

    // Return to Amazon Cognito
    callback(null, event);

Is that what the expectation is in 'go' as well - that I return (events.CognitoEventUserPoolsPostAuthentication, error) and just send it back the original event?

profile picture
wz2b
질문됨 2년 전396회 조회
2개 답변
0
수락된 답변

I figured this out. I want more control over things so I'm using this:

func HandleRequest(ctx context.Context, request events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) { ... }

which works great. It was kind of nice before that it would take care of marshaling/unmarshaling and now I have to do it myself, but that's not bad. I do kind of wish the Response "Body" was just interface{} and that it would automatically marshal/unmarshal based on Content-Type but it's fine.

One thing I do miss from doing this in Kotlin and Spring is the ability to just throw an Exception, and write an ExceptionMapper that would set the appropriate http status code.

profile picture
wz2b
답변함 2년 전
0

I tried following

package main

import (
	"fmt"

	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
)

func handler(event events.CognitoEventUserPoolsPostAuthentication) (events.CognitoEventUserPoolsPostAuthentication, error) {
	fmt.Printf("PreAuthentication of user: %s\n", event.UserName)

	// Return to Amazon Cognito
	return event, nil
	// Try raising an error
	// return event, fmt.Errorf("TESTING LAMBDA ERRORS WITH GOLANG")
}

func main() {
	lambda.Start(handler)
}
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인