Skip to content

Initial cognito trigger support #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions Sources/AWSLambdaEvents/Cognito.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2017-2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

enum CognitoEventError: Error {
case unimplementedEvent(String)
}

/// https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html
public enum CognitoEvent: Equatable {
public struct CallerContext: Codable, Hashable {
let awsSdkVersion: String
let clientId: String
}

public struct Parameters: Codable, Equatable {
let version: String
let triggerSource: String
let region: AWSRegion
let userPoolId: String
let userName: String
let callerContext: CallerContext
}

case preSignUpSignUp(Parameters, PreSignUp)

public struct PreSignUp: Codable, Hashable {
/// One or more name-value pairs representing user attributes. The attribute names are the keys.
public let userAttributes: [String: String]
/// One or more name-value pairs containing the validation data in the request to register a user.
///
/// The validation data is set and then passed from the client in the request to register a user. You can pass this data to your Lambda function by using the ClientMetadata parameter in the InitiateAuth and AdminInitiateAuth API actions.
public let validationData: [String: String]?
/// One or more key-value pairs that you can provide as custom input to the Lambda function that you specify for the pre sign-up trigger.
///
/// You can pass this data to your Lambda function by using the ClientMetadata parameter in the following API actions: AdminCreateUser, AdminRespondToAuthChallenge, ForgotPassword, and SignUp.
public let clientMetadata: [String: String]?
}

public var commonParameters: Parameters {
switch self {
case .preSignUpSignUp(let params, _):
return params
}
}
}

extension CognitoEvent: Codable {
public enum CodingKeys: String, CodingKey {
case version
case triggerSource
case region
case userPoolId
case userName
case callerContext
case request
case response
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

let version = try container.decode(String.self, forKey: .version)
let triggerSource = try container.decode(String.self, forKey: .triggerSource)
let region = try container.decode(AWSRegion.self, forKey: .region)
let userPoolId = try container.decode(String.self, forKey: .userPoolId)
let userName = try container.decode(String.self, forKey: .userName)
let callerContext = try container.decode(CallerContext.self, forKey: .callerContext)

let params = CognitoEvent.Parameters(version: version, triggerSource: triggerSource, region: region, userPoolId: userPoolId, userName: userName, callerContext: callerContext)

switch triggerSource {
case "PreSignUp_SignUp":
let value = try container.decode(CognitoEvent.PreSignUp.self, forKey: .request)

self = .preSignUpSignUp(params, value)
default:
throw CognitoEventError.unimplementedEvent(triggerSource)
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

let params = self.commonParameters

try container.encode(params.version, forKey: .version)
try container.encode(params.triggerSource, forKey: .triggerSource)
try container.encode(params.region, forKey: .region)
try container.encode(params.userPoolId, forKey: .userPoolId)
try container.encode(params.userName, forKey: .userName)
try container.encode(params.callerContext, forKey: .callerContext)

switch self {
case .preSignUpSignUp(_, let value):
try container.encode(value, forKey: .response)
}
}
}

public enum CognitoEventResponse {
case preSignUpSignUp(CognitoEvent.Parameters, CognitoEvent.PreSignUp, PreSignUp)

public struct PreSignUp: Codable, Hashable {
public let autoConfirmUser: Bool
public let autoVerifyPhone: Bool
public let autoVerifyEmail: Bool

public init(autoConfirmUser: Bool, autoVerifyPhone: Bool, autoVerifyEmail: Bool) {
self.autoConfirmUser = autoConfirmUser
self.autoVerifyPhone = autoVerifyPhone
self.autoVerifyEmail = autoVerifyEmail
}
}

public var commonParameters: CognitoEvent.Parameters {
switch self {
case .preSignUpSignUp(let params, _, _):
return params
}
}
}

extension CognitoEventResponse: Codable {
public enum CodingKeys: String, CodingKey {
case version
case triggerSource
case region
case userPoolId
case userName
case callerContext
case request
case response
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

let version = try container.decode(String.self, forKey: .version)
let triggerSource = try container.decode(String.self, forKey: .triggerSource)
let region = try container.decode(AWSRegion.self, forKey: .region)
let userPoolId = try container.decode(String.self, forKey: .userPoolId)
let userName = try container.decode(String.self, forKey: .userName)
let callerContext = try container.decode(CognitoEvent.CallerContext.self, forKey: .callerContext)

let params = CognitoEvent.Parameters(version: version, triggerSource: triggerSource, region: region, userPoolId: userPoolId, userName: userName, callerContext: callerContext)

switch triggerSource {
case "PreSignUp_SignUp":
let request = try container.decode(CognitoEvent.PreSignUp.self, forKey: .request)
let response = try container.decode(CognitoEventResponse.PreSignUp.self, forKey: .response)

self = .preSignUpSignUp(params, request, response)
default:
throw CognitoEventError.unimplementedEvent(triggerSource)
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

let params = self.commonParameters

try container.encode(params.version, forKey: .version)
try container.encode(params.triggerSource, forKey: .triggerSource)
try container.encode(params.region, forKey: .region)
try container.encode(params.userPoolId, forKey: .userPoolId)
try container.encode(params.userName, forKey: .userName)
try container.encode(params.callerContext, forKey: .callerContext)

switch self {
case .preSignUpSignUp(_, let request, let response):
try container.encode(request, forKey: .request)
try container.encode(response, forKey: .response)
}
}
}
2 changes: 2 additions & 0 deletions Sources/AWSLambdaEvents/Docs.docc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ AWS Lambda functions can be invoked directly from the AWS Lambda console UI, AWS
* [SNS Events](https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html)
* [SQS Events](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html)
* [CloudWatch Events](https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents.html)
* [Cognito Lambda Triggers](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html)

**Note**: Each one of the integration points mentioned above includes a set of `Codable` structs that mirror AWS' data model for these APIs.

Expand All @@ -38,3 +39,4 @@ Swift AWS Lambda Events is a supporting library for the [Swift AWS Lambda Runtim
- ``SESEvent``
- ``SNSEvent``
- ``SQSEvent``
- ``CognitoEvent``
91 changes: 91 additions & 0 deletions Tests/AWSLambdaEventsTests/CognitoTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

@testable import AWSLambdaEvents
import XCTest

final class CognitoTests: XCTestCase {
func testPreSignUpRequest() throws {
let json = """
{
"version": "1",
"triggerSource": "PreSignUp_SignUp",
"region": "us-east-1",
"userPoolId": "abc",
"userName": "blob",
"callerContext": {
"awsSdkVersion": "1",
"clientId": "abc",
},
"request": {
"userAttributes": {
"string": "string"
},
"validationData": {
"string": "string"
},
"clientMetadata": {
"string": "string"
}
},

"response": {}
}
"""
let event = try JSONDecoder().decode(CognitoEvent.self, from: json.data(using: .utf8)!)

guard case .preSignUpSignUp(let params, let request) = event else {
XCTFail()
return
}

XCTAssertEqual(params.triggerSource, "PreSignUp_SignUp")

let signUp = CognitoEvent.PreSignUp(userAttributes: ["string": "string"],
validationData: ["string": "string"],
clientMetadata: ["string": "string"])
XCTAssertEqual(request, signUp)
}

func testPreSignUpResponse() throws {
let params = CognitoEvent.Parameters(version: "1",
triggerSource: "PreSignUp_SignUp",
region: .us_east_1,
userPoolId: "abc",
userName: "blob",
callerContext: .init(awsSdkVersion: "1", clientId: "abc"))
let request = CognitoEvent.PreSignUp(userAttributes: ["string": "string"],
validationData: ["string": "string"],
clientMetadata: ["string": "string"])

let signUpResponse = CognitoEventResponse.PreSignUp(autoConfirmUser: true,
autoVerifyPhone: true,
autoVerifyEmail: true)

let response = CognitoEventResponse.preSignUpSignUp(params, request, signUpResponse)

let data = try JSONEncoder().encode(response)

let decodedResponse = try JSONDecoder().decode(CognitoEventResponse.self, from: data)

guard case .preSignUpSignUp(let decodedParams, let decodedRequest, let decodedResponse) = decodedResponse else {
XCTFail()
return
}

XCTAssertEqual(decodedParams, params)
XCTAssertEqual(decodedRequest, request)
XCTAssertEqual(decodedResponse, signUpResponse)
}
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ AWS Lambda functions can be invoked directly from the AWS Lambda console UI, AWS
* [SNS Events](https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html)
* [SQS Events](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html)
* [CloudWatch Events](https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents.html)
* [Cognito Lambda Triggers](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html)

**Note**: Each one of the integration points mentioned above includes a set of `Codable` structs that mirror AWS' data model for these APIs.

Expand Down