Created
February 20, 2021 16:03
-
-
Save sgswtky/4e55a27b26959f88f17c4ce06730dc9a to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"context" | |
"errors" | |
"fmt" | |
"log" | |
"math/rand" | |
"time" | |
"github.com/aws/aws-lambda-go/lambda" | |
) | |
func main() { | |
lambda.Start(createAuthChallenge) | |
} | |
type Req map[string]interface{} | |
func createAuthChallenge(ctx context.Context, req Req) (interface{}, error) { | |
fmt.Println("called create auth challenge") | |
for k, v := range req { | |
log.Println(fmt.Sprintf("%s => %v", k, v)) | |
} | |
reqReq, isErr := req["request"].(map[string]interface{}) | |
if !isErr { | |
return nil, errors.New("cannot convert request") | |
} | |
response, isErr := req["response"].(map[string]interface{}) | |
if !isErr { | |
return nil, errors.New("cannot convert response") | |
} | |
if reqReq["challengeName"].(string) == "CUSTOM_CHALLENGE" { | |
challengeAnswer := createChallengeString() | |
if err := sendChallengeString(challengeAnswer); err != nil { | |
return req, err | |
} | |
response["publicChallengeParameters"] = map[string]string{"challenge": "チャレンジです"} | |
response["privateChallengeParameters"] = map[string]string{"answer": challengeAnswer} | |
response["challengeMetadata"] = "SECRET_CHALLENGE" | |
} | |
log.Println(fmt.Sprintf("response: %v", response)) | |
req["response"] = response | |
return req, nil | |
} | |
func createChallengeString() string { | |
rand.Seed(time.Now().UnixNano()) | |
return fmt.Sprintf("%d", rand.Intn(9999)) | |
} | |
func sendChallengeString(challengeAnswer string) error { | |
log.Println(fmt.Sprintf("challenge answer: %s", challengeAnswer)) | |
// TODO: send email... | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment