Skip to content
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

AWS Cognito Integration #1256

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<module>spring-cloud-aws-sqs</module>
<module>spring-cloud-aws-dynamodb</module>
<module>spring-cloud-aws-s3</module>
<module>spring-cloud-aws-cognito</module>
<module>spring-cloud-aws-testcontainers</module>
<module>spring-cloud-aws-starters/spring-cloud-aws-starter</module>
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-dynamodb</module>
Expand All @@ -54,6 +55,7 @@
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-ses</module>
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-sns</module>
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-sqs</module>
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-cognito</module>
<module>spring-cloud-aws-samples</module>
<module>spring-cloud-aws-test</module>
<module>spring-cloud-aws-modulith</module>
Expand Down
5 changes: 5 additions & 0 deletions spring-cloud-aws-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
<artifactId>spring-cloud-aws-s3</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-cognito</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>software.amazon.dax</groupId>
<artifactId>amazon-dax-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2013-2024 the original author or authors.
*
* 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
*
* https://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 io.awspring.cloud.autoconfigure.cognito;

import io.awspring.cloud.autoconfigure.AwsSyncClientCustomizer;
import io.awspring.cloud.autoconfigure.core.AwsAutoConfiguration;
import io.awspring.cloud.autoconfigure.core.AwsClientBuilderConfigurer;
import io.awspring.cloud.autoconfigure.core.AwsConnectionDetails;
import io.awspring.cloud.autoconfigure.core.CredentialsProviderAutoConfiguration;
import io.awspring.cloud.autoconfigure.core.RegionProviderAutoConfiguration;
import io.awspring.cloud.cognito.CognitoTemplate;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import software.amazon.awssdk.services.cognitoidentityprovider.CognitoIdentityProviderClient;

/**
* {@link AutoConfiguration Auto-Configuration} for AWS Cognito integration.
*
* @author Oleh Onufryk
* @since 3.3.0
*/

@AutoConfiguration
@EnableConfigurationProperties(CognitoProperties.class)
@ConditionalOnClass({ CognitoIdentityProviderClient.class })
@AutoConfigureAfter({ CredentialsProviderAutoConfiguration.class, RegionProviderAutoConfiguration.class,
AwsAutoConfiguration.class })
@ConditionalOnProperty(name = "spring.cloud.aws.cognito.enabled", havingValue = "true", matchIfMissing = true)
public class CognitoAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public CognitoIdentityProviderClient cognitoIdentityProviderClient(CognitoProperties cognitoProperties,
AwsClientBuilderConfigurer awsClientBuilderConfigurer, ObjectProvider<CognitoClientCustomizer> customizers,
ObjectProvider<AwsSyncClientCustomizer> awsSyncClientCustomizers,
ObjectProvider<AwsConnectionDetails> connectionDetails) {
return awsClientBuilderConfigurer.configureSyncClient(CognitoIdentityProviderClient.builder(),
cognitoProperties, connectionDetails.getIfAvailable(), customizers.orderedStream(),
awsSyncClientCustomizers.orderedStream()).build();
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = { "spring.cloud.aws.cognito.client-id", "spring.cloud.aws.cognito.user-pool-id" })
public CognitoTemplate cognitoTemplate(CognitoProperties cognitoProperties,
CognitoIdentityProviderClient cognitoIdentityProviderClient) {
return new CognitoTemplate(cognitoIdentityProviderClient, cognitoProperties.getClientId(),
cognitoProperties.getUserPoolId(), cognitoProperties.getClientSecret());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2013-2024 the original author or authors.
*
* 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
*
* https://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 io.awspring.cloud.autoconfigure.cognito;

import io.awspring.cloud.autoconfigure.AwsClientCustomizer;
import software.amazon.awssdk.services.cognitoidentityprovider.CognitoIdentityProviderClientBuilder;

@FunctionalInterface
public interface CognitoClientCustomizer extends AwsClientCustomizer<CognitoIdentityProviderClientBuilder> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2013-2024 the original author or authors.
*
* 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
*
* https://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 io.awspring.cloud.autoconfigure.cognito;

import io.awspring.cloud.autoconfigure.AwsClientProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Configuration properties for AWS Cognito Integration
*
* @author Oleh Onufryk
* @since 3.3.0
*/

@ConfigurationProperties(CognitoProperties.CONFIG_PREFIX)
public class CognitoProperties extends AwsClientProperties {

/**
* Configuration prefix.
*/
public static final String CONFIG_PREFIX = "spring.cloud.aws.cognito";

/**
* Enables Cognito integration.
*/
private boolean enabled = true;

/**
* The user pool ID.
*/
private String userPoolId;

/**
* The client ID.
*/
private String clientId;

/**
* The client secret.
*/
private String clientSecret;

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public String getUserPoolId() {
return userPoolId;
}

public void setUserPoolId(String userPoolId) {
this.userPoolId = userPoolId;
}

public String getClientId() {
return clientId;
}

public void setClientId(String clientId) {
this.clientId = clientId;
}

public String getClientSecret() {
return clientSecret;
}

public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2013-2022 the original author or authors.
*
* 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
*
* https://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.
*/

/**
* {@link org.springframework.boot.context.config.ConfigDataLoader} implementation for AWS Cognito.
*/
@org.springframework.lang.NonNullApi
@org.springframework.lang.NonNullFields
package io.awspring.cloud.autoconfigure.cognito;
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ io.awspring.cloud.autoconfigure.config.secretsmanager.SecretsManagerReloadAutoCo
io.awspring.cloud.autoconfigure.config.secretsmanager.SecretsManagerAutoConfiguration
io.awspring.cloud.autoconfigure.config.parameterstore.ParameterStoreReloadAutoConfiguration
io.awspring.cloud.autoconfigure.config.parameterstore.ParameterStoreAutoConfiguration
io.awspring.cloud.autoconfigure.cognito.CognitoAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2013-2024 the original author or authors.
*
* 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
*
* https://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 io.awspring.cloud.autoconfigure.cognito;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import io.awspring.cloud.autoconfigure.core.AwsAutoConfiguration;
import io.awspring.cloud.autoconfigure.core.CredentialsProviderAutoConfiguration;
import io.awspring.cloud.autoconfigure.core.RegionProviderAutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import software.amazon.awssdk.services.cognitoidentityprovider.CognitoIdentityProviderClient;

/**
* Test for {@link CognitoAutoConfiguration}
*/
class CognitoAutoConfigurationTest {

private final ApplicationContextRunner runner = new ApplicationContextRunner()
.withPropertyValues("spring.cloud.aws.region.static:eu-west-1")
.withConfiguration(AutoConfigurations.of(RegionProviderAutoConfiguration.class,
CredentialsProviderAutoConfiguration.class, CognitoAutoConfiguration.class,
AwsAutoConfiguration.class));

@Test
void cognitoAutoConfigurationIsDisabled() {
this.runner.withPropertyValues("spring.cloud.aws.cognito.enabled:false")
.run(context -> assertThat(context).doesNotHaveBean(CognitoIdentityProviderClient.class));
}

@Test
void cognitoAutoConfigurationIsEnabled() {
this.runner.withPropertyValues("spring.cloud.aws.cognito.enabled:true")
.run(context -> assertThat(context).hasSingleBean(CognitoIdentityProviderClient.class));
}

@Test
void createCognitoClientBeanByDefault() {
this.runner.run(context -> assertThat(context).hasSingleBean(CognitoAutoConfiguration.class));
}

@Test
void usesCustomBeanWhenProvided() {
this.runner.withUserConfiguration(CustomCognitoConfiguration.class).run(context -> {
assertThat(context).hasSingleBean(CognitoIdentityProviderClient.class);
assertThat(context.getBean(CognitoIdentityProviderClient.class)).isNotNull();
});
}

@TestConfiguration
static class CustomCognitoConfiguration {
@Bean
CognitoIdentityProviderClient cognitoIdentityProviderClient() {
return mock(CognitoIdentityProviderClient.class);
}
}
}
37 changes: 37 additions & 0 deletions spring-cloud-aws-cognito/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws</artifactId>
<version>3.3.0-SNAPSHOT</version>
</parent>

<artifactId>spring-cloud-aws-cognito</artifactId>
<name>Spring Cloud AWS Cognito Integration</name>

<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>cognitoidentityprovider</artifactId>
<exclusions>
<exclusion>
<groupId>software.amazon.awssdk</groupId>
<artifactId>netty-nio-client</artifactId>
</exclusion>
<exclusion>
<groupId>software.amazon.awssdk</groupId>
<artifactId>apache-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
</dependencies>

</project>
Loading