forked from awspring/spring-cloud-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
351 additions
and
0 deletions.
There are no files selected for viewing
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
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
69 changes: 69 additions & 0 deletions
69
...igure/src/main/java/io/awspring/cloud/autoconfigure/cognito/CognitoAutoConfiguration.java
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
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.clientId", "spring.cloud.aws.cognito.userPoolId"}) | ||
public CognitoTemplate cognitoTemplate(CognitoProperties cognitoProperties, | ||
CognitoIdentityProviderClient cognitoIdentityProviderClient) { | ||
return new CognitoTemplate(cognitoIdentityProviderClient, cognitoProperties.getClientId(), | ||
cognitoProperties.getUserPoolId()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...figure/src/main/java/io/awspring/cloud/autoconfigure/cognito/CognitoClientCustomizer.java
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
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> { | ||
} |
80 changes: 80 additions & 0 deletions
80
...utoconfigure/src/main/java/io/awspring/cloud/autoconfigure/cognito/CognitoProperties.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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; | ||
|
||
@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; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/cognito/package-info.java
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
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; |
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
74 changes: 74 additions & 0 deletions
74
...e/src/test/java/io/awspring/cloud/autoconfigure/cognito/CognitoAutoConfigurationTest.java
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
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); | ||
} | ||
} | ||
} |
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
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> |
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
26 changes: 26 additions & 0 deletions
26
spring-cloud-aws-starters/spring-cloud-aws-starter-cognito/pom.xml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>spring-cloud-aws</artifactId> | ||
<groupId>io.awspring.cloud</groupId> | ||
<version>3.3.0-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>spring-cloud-aws-starter-cognito</artifactId> | ||
<name>Spring Cloud AWS Cognito Starter</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.awspring.cloud</groupId> | ||
<artifactId>spring-cloud-aws-cognito</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.awspring.cloud</groupId> | ||
<artifactId>spring-cloud-aws-starter</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |