Trouble configuring Springboot with openapi 3.0, aws-cognito oauth2

Viewed 1395

I was trying to integrate OpenAPI 3.0 in my existing springboot restpai application. So far I can configure the openapi-with oAuth2 by using this code snippet.

    @Bean
    public OpenAPI customOpenAPI() {

        OAuthFlow oAuthFlowObject = new OAuthFlow();
        oAuthFlowObject
                .setAuthorizationUrl("https://<my-domain>.auth.us-east-2.amazoncognito.com/oauth2/authorize");
        oAuthFlowObject.setRefreshUrl("https://<my-domain>.auth.us-east-2.amazoncognito.com/oauth2/refresh");
        oAuthFlowObject.setTokenUrl("https://<my-domain>.auth.us-east-2.amazoncognito.com/oauth2/token");

        OAuthFlows oAuthFlows = new OAuthFlows();
        oAuthFlows.authorizationCode(oAuthFlowObject);

        return new OpenAPI()
                .components(new Components()
                                    .addSecuritySchemes("oauth2", new SecurityScheme().in(SecurityScheme.In.HEADER)
                                                                                      .type(SecurityScheme.Type.OAUTH2)
                                                                                      .flows(oAuthFlows)
                                                        .bearerFormat("JWT")
                                                        .scheme("bearer")
                                    ))
                .info(new Info().title("Contact Application API").description(
                        "This is a sample Spring Boot RESTful service using springdoc-openapi and OpenAPI 3."))
                ;
    }

It seems that I can successfully get the token from the cognito. bearer token

But the problem is when I try-out any of the api from swagger-ui, it does not include bearer token.

no-bearer-token

Is there anything that I'm missing? How could I set the path prefix so that the token will be attached when calling those paths. Also I only wanted to send the "id_token" in the Authorization-bearer header from swagger.

1 Answers

To configure aws-cognito for swagger and making it sent the id-token instead of access-token we need to configure two security mechanism. Here is the sample code snippets. This is a work around. As by default access-token is sent and we need to sent the id-token instead, we configured two options.


@Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                //bearer auth with oAuth2 
                .addSecurityItem(new SecurityRequirement().addList("bearerAuth"))
                .components(new Components().addSecuritySchemes("oAuth2",                   new SecurityScheme()                                                                .type(SecurityScheme.Type.OAUTH2)                                                               .flows(getOAuthFlows())
)

/// Bearer AUTH security config settings. ## for id-token.
.addSecuritySchemes("bearerAuth",                                                       new SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
)
).info(getInfo());
}


    private Info getInfo() {
        return new Info()
                .title("Title")
                .version("1.0")
                .description("Project description..");
    }

Here how it is looks like: swagger auth

After that you need to configure the id-token from the browser console. copy-ing id token as bearer auth

Related