Search in sources :

Example 1 with CognitoConnectionBuilder

use of com.formkiq.aws.cognito.CognitoConnectionBuilder in project formkiq-core by formkiq.

the class WebsocketTest method beforeClass.

/**
 * beforeclass.
 *
 * @throws IOException IOException
 * @throws URISyntaxException URISyntaxException
 */
@BeforeClass
public static void beforeClass() throws IOException, URISyntaxException {
    Region awsregion = Region.of(System.getProperty("testregion"));
    String awsprofile = System.getProperty("testprofile");
    String app = System.getProperty("testappenvironment");
    SqsConnectionBuilder sqsConnection = new SqsConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    sqsService = new SqsService(sqsConnection);
    SsmConnectionBuilder ssmBuilder = new SsmConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    SsmService ssmService = new SsmServiceImpl(ssmBuilder);
    websocketSqsUrl = ssmService.getParameterValue("/formkiq/" + app + "/sqs/WebsocketUrl");
    websocketUrl = ssmService.getParameterValue("/formkiq/" + app + "/api/WebsocketUrl");
    String cognitoUserPoolId = ssmService.getParameterValue("/formkiq/" + app + "/cognito/UserPoolId");
    String cognitoClientId = ssmService.getParameterValue("/formkiq/" + app + "/cognito/UserPoolClientId");
    String cognitoIdentitypool = ssmService.getParameterValue("/formkiq/" + app + "/cognito/IdentityPoolId");
    CognitoConnectionBuilder adminBuilder = new CognitoConnectionBuilder(cognitoClientId, cognitoUserPoolId, cognitoIdentitypool).setCredentials(awsprofile).setRegion(awsregion);
    adminCognitoService = new CognitoService(adminBuilder);
    addAndLoginCognito(USER_EMAIL, GROUP);
    token = adminCognitoService.login(USER_EMAIL, USER_PASSWORD);
    String rootHttpUrl = ssmService.getParameterValue("/formkiq/" + app + "/api/DocumentsHttpUrl");
    FormKiqClientConnection connection = new FormKiqClientConnection(rootHttpUrl).cognitoIdToken(token.idToken()).header("Origin", Arrays.asList("http://localhost")).header("Access-Control-Request-Method", Arrays.asList("GET"));
    httpClient = new FormKiqClientV1(connection);
    webconnectionsTable = ssmService.getParameterValue("/formkiq/" + app + "/dynamodb/WebConnectionsTableName");
    dbConnection = new DynamoDbConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
}
Also used : SsmService(com.formkiq.aws.ssm.SsmService) SsmServiceImpl(com.formkiq.aws.ssm.SsmServiceImpl) CognitoConnectionBuilder(com.formkiq.aws.cognito.CognitoConnectionBuilder) SqsConnectionBuilder(com.formkiq.aws.sqs.SqsConnectionBuilder) SsmConnectionBuilder(com.formkiq.aws.ssm.SsmConnectionBuilder) SqsService(com.formkiq.aws.sqs.SqsService) FormKiqClientConnection(com.formkiq.stacks.client.FormKiqClientConnection) Region(software.amazon.awssdk.regions.Region) CognitoService(com.formkiq.aws.cognito.CognitoService) DynamoDbConnectionBuilder(com.formkiq.stacks.dynamodb.DynamoDbConnectionBuilder) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) BeforeClass(org.junit.BeforeClass)

Example 2 with CognitoConnectionBuilder

use of com.formkiq.aws.cognito.CognitoConnectionBuilder in project formkiq-core by formkiq.

the class AwsResourceTest method testAdminAddUserToGroup.

/**
 * Test Having Admin add new user to group.
 */
@Test(timeout = TEST_TIMEOUT)
public void testAdminAddUserToGroup() {
    // given
    String email = UUID.randomUUID() + "@formkiq.com";
    String group = getParameterStoreValue("/formkiq/" + getAppenvironment() + "/cognito/AdminGroup");
    Credentials cred = getAdminCognitoService().getCredentials(getAdminToken());
    AwsCredentials basic = AwsSessionCredentials.create(cred.accessKeyId(), cred.secretKey(), cred.sessionToken());
    CognitoConnectionBuilder userBuilder = createCognitoConnectionBuilder().setCredentials(StaticCredentialsProvider.create(basic));
    CognitoService userCognitoService = new CognitoService(userBuilder);
    // when
    GetUserResponse user = userCognitoService.getUser(getAdminToken());
    userCognitoService.addUser(email, USER_TEMP_PASSWORD);
    userCognitoService.addUserToGroup(email, group);
    // then
    assertNotNull(user.username());
    assertEquals("testadminuser@formkiq.com", user.userAttributes().stream().filter(f -> f.name().equals("email")).findFirst().get().value());
}
Also used : CognitoConnectionBuilder(com.formkiq.aws.cognito.CognitoConnectionBuilder) GetUserResponse(software.amazon.awssdk.services.cognitoidentityprovider.model.GetUserResponse) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) AwsSessionCredentials(software.amazon.awssdk.auth.credentials.AwsSessionCredentials) Credentials(software.amazon.awssdk.services.cognitoidentity.model.Credentials) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) CognitoService(com.formkiq.aws.cognito.CognitoService) Test(org.junit.Test)

Example 3 with CognitoConnectionBuilder

use of com.formkiq.aws.cognito.CognitoConnectionBuilder in project formkiq-core by formkiq.

the class AbstractApiTest method beforeClass.

/**
 * Before Class.
 *
 * @throws IOException IOException
 */
@BeforeClass
public static void beforeClass() throws IOException {
    awsregion = Region.of(System.getProperty("testregion"));
    String awsprofile = System.getProperty("testprofile");
    appenvironment = System.getProperty("testappenvironment");
    loadSsmParameterVariables(awsprofile);
    try (ProfileCredentialsProvider credentials = ProfileCredentialsProvider.builder().profileName(awsprofile).build()) {
        FormKiqClientConnection connection = new FormKiqClientConnection(rootRestUrl).region(awsregion).credentials(credentials.resolveCredentials()).header("Origin", Arrays.asList("http://localhost")).header("Access-Control-Request-Method", Arrays.asList("GET"));
        restClient = new FormKiqClientV1(connection);
    }
    CognitoConnectionBuilder adminBuilder = new CognitoConnectionBuilder(cognitoClientId, cognitoUserPoolId, cognitoIdentitypool).setCredentials(awsprofile).setRegion(awsregion);
    adminCognitoService = new CognitoService(adminBuilder);
    stsBuilder = new StsConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    stsService = new StsService(stsBuilder);
    try (StsClient stsClient = stsBuilder.build()) {
        GetCallerIdentityResponse identity = stsClient.getCallerIdentity();
        String user = identity.arn().substring(identity.arn().lastIndexOf("/") + 1);
        IamConnectionBuilder iamBuilder = new IamConnectionBuilder().setCredentials(awsprofile);
        IamService iam = new IamService(iamBuilder);
        try (IamClient iamClient = iamBuilder.build()) {
            iam.addUserToGroup(iamClient, user, apiGatewayInvokeGroup);
        }
        executeApiSigner = new Aws4SignerParamsBuilder().setRegion(awsregion).setSigningName("execute-api").setCredentials(awsprofile).build();
    }
    setupCognito();
    setupConfigService(awsprofile);
}
Also used : StsClient(software.amazon.awssdk.services.sts.StsClient) IamConnectionBuilder(com.formkiq.aws.iam.IamConnectionBuilder) GetCallerIdentityResponse(software.amazon.awssdk.services.sts.model.GetCallerIdentityResponse) StsConnectionBuilder(com.formkiq.aws.sts.StsConnectionBuilder) Aws4SignerParamsBuilder(com.formkiq.aws.sts.Aws4SignerParamsBuilder) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) CognitoConnectionBuilder(com.formkiq.aws.cognito.CognitoConnectionBuilder) IamClient(software.amazon.awssdk.services.iam.IamClient) StsService(com.formkiq.aws.sts.StsService) FormKiqClientConnection(com.formkiq.stacks.client.FormKiqClientConnection) ProfileCredentialsProvider(software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider) IamService(com.formkiq.aws.iam.IamService) CognitoService(com.formkiq.aws.cognito.CognitoService) BeforeClass(org.junit.BeforeClass)

Aggregations

CognitoConnectionBuilder (com.formkiq.aws.cognito.CognitoConnectionBuilder)3 CognitoService (com.formkiq.aws.cognito.CognitoService)3 FormKiqClientConnection (com.formkiq.stacks.client.FormKiqClientConnection)2 FormKiqClientV1 (com.formkiq.stacks.client.FormKiqClientV1)2 BeforeClass (org.junit.BeforeClass)2 IamConnectionBuilder (com.formkiq.aws.iam.IamConnectionBuilder)1 IamService (com.formkiq.aws.iam.IamService)1 SqsConnectionBuilder (com.formkiq.aws.sqs.SqsConnectionBuilder)1 SqsService (com.formkiq.aws.sqs.SqsService)1 SsmConnectionBuilder (com.formkiq.aws.ssm.SsmConnectionBuilder)1 SsmService (com.formkiq.aws.ssm.SsmService)1 SsmServiceImpl (com.formkiq.aws.ssm.SsmServiceImpl)1 Aws4SignerParamsBuilder (com.formkiq.aws.sts.Aws4SignerParamsBuilder)1 StsConnectionBuilder (com.formkiq.aws.sts.StsConnectionBuilder)1 StsService (com.formkiq.aws.sts.StsService)1 DynamoDbConnectionBuilder (com.formkiq.stacks.dynamodb.DynamoDbConnectionBuilder)1 Test (org.junit.Test)1 AwsCredentials (software.amazon.awssdk.auth.credentials.AwsCredentials)1 AwsSessionCredentials (software.amazon.awssdk.auth.credentials.AwsSessionCredentials)1 ProfileCredentialsProvider (software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider)1