diff --git a/lambda/shared/src/main/scala/feral/lambda/events/AppSyncLambdaAuthorizerEvent.scala b/lambda/shared/src/main/scala/feral/lambda/events/AppSyncLambdaAuthorizerEvent.scala new file mode 100644 index 00000000..17a1b677 --- /dev/null +++ b/lambda/shared/src/main/scala/feral/lambda/events/AppSyncLambdaAuthorizerEvent.scala @@ -0,0 +1,103 @@ +/* + * Copyright 2021 Typelevel + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package feral.lambda +package events + +import io.circe.Decoder +import io.circe.Json + +sealed abstract class AppSyncRequestContext { + def apiId: String + def accountId: String + def requestId: String + def queryString: String + def operationName: String + def variables: Map[String, Json] +} + +object AppSyncRequestContext { + + def apply( + apiId: String, + accountId: String, + requestId: String, + queryString: String, + operationName: String, + variables: Map[String, Json] + ): AppSyncRequestContext = + new Impl( + apiId, + accountId, + requestId, + queryString, + operationName, + variables + ) + + implicit def decoder: Decoder[AppSyncRequestContext] = Decoder.forProduct6( + "apiId", + "accountId", + "requestId", + "queryString", + "operationName", + "variables" + )(AppSyncRequestContext.apply) + + private final case class Impl( + apiId: String, + accountId: String, + requestId: String, + queryString: String, + operationName: String, + variables: Map[String, Json] + ) extends AppSyncRequestContext { + override def productPrefix = "AppSyncRequestContext" + } +} + +sealed abstract class AppSyncLambdaAuthorizerEvent { + def authorizationToken: String + def requestContext: AppSyncRequestContext + def requestHeaders: Map[String, String] +} + +object AppSyncLambdaAuthorizerEvent { + def apply( + authorizationToken: String, + requestContext: AppSyncRequestContext, + requestHeaders: Map[String, String] + ): AppSyncLambdaAuthorizerEvent = + new Impl( + authorizationToken: String, + requestContext: AppSyncRequestContext, + requestHeaders: Map[String, String] + ) + + implicit def decoder: Decoder[AppSyncLambdaAuthorizerEvent] = Decoder.forProduct3( + "authorizationToken", + "requestContext", + "requestHeaders" + )(AppSyncLambdaAuthorizerEvent.apply) + + private final case class Impl( + authorizationToken: String, + requestContext: AppSyncRequestContext, + requestHeaders: Map[String, String] + ) extends AppSyncLambdaAuthorizerEvent { + override def productPrefix = "AppSyncLambdaAuthorizerEvent" + } +} diff --git a/lambda/shared/src/test/scala/feral/lambda/events/AppSyncLambdaAuthorizerEventSuite.scala b/lambda/shared/src/test/scala/feral/lambda/events/AppSyncLambdaAuthorizerEventSuite.scala new file mode 100644 index 00000000..f9e7cc0d --- /dev/null +++ b/lambda/shared/src/test/scala/feral/lambda/events/AppSyncLambdaAuthorizerEventSuite.scala @@ -0,0 +1,55 @@ +/* + * Copyright 2021 Typelevel + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package feral.lambda.events + +import io.circe.literal._ +import munit.FunSuite + +class AppSyncLambdaAuthorizerEventSuite extends FunSuite { + + test("decoder") { + import AppSyncLambdaAuthorizerEventSuite._ + + val parsed_event = event.as[AppSyncLambdaAuthorizerEvent].toTry.get + + assertEquals(parsed_event.authorizationToken, "ExampleAUTHtoken123123123") + assertEquals(parsed_event.requestContext.accountId, "111122223333") + assertEquals(parsed_event.requestContext.apiId, "aaaaaa123123123example123") + assertEquals(parsed_event.requestContext.requestId, "f4081827-1111-4444-5555-5cf4695f339f") + assertEquals(parsed_event.requestHeaders("header"), "value") + } + +} + +object AppSyncLambdaAuthorizerEventSuite { + def event = json""" + { + "authorizationToken": "ExampleAUTHtoken123123123", + "requestContext": { + "apiId": "aaaaaa123123123example123", + "accountId": "111122223333", + "requestId": "f4081827-1111-4444-5555-5cf4695f339f", + "queryString": "mutation CreateEvent {...}\n\nquery MyQuery {...}\n", + "operationName": "MyQuery", + "variables": {} + }, + "requestHeaders": { + "header": "value" + } + } + """ +}