|
1 | 1 | package fr.insee.genesis.configuration; |
2 | 2 |
|
| 3 | +import io.swagger.v3.oas.models.Components; |
3 | 4 | import io.swagger.v3.oas.models.OpenAPI; |
4 | 5 | import io.swagger.v3.oas.models.info.Info; |
5 | | -import io.swagger.v3.oas.models.servers.Server; |
| 6 | + |
| 7 | +import io.swagger.v3.oas.models.security.OAuthFlow; |
| 8 | +import io.swagger.v3.oas.models.security.OAuthFlows; |
| 9 | +import io.swagger.v3.oas.models.security.Scopes; |
| 10 | +import io.swagger.v3.oas.models.security.SecurityRequirement; |
| 11 | +import io.swagger.v3.oas.models.security.SecurityScheme; |
6 | 12 | import org.springframework.beans.factory.annotation.Value; |
| 13 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
7 | 14 | import org.springframework.context.annotation.Bean; |
8 | 15 | import org.springframework.context.annotation.Configuration; |
9 | 16 |
|
10 | 17 | @Configuration |
11 | 18 | public class SpringDocConfiguration { |
12 | 19 |
|
13 | | - @Value("${fr.insee.genesis.version}") |
14 | | - private String projectVersion; |
| 20 | + @Value("${fr.insee.genesis.version}") |
| 21 | + private String projectVersion; |
| 22 | + public static final String BEARERSCHEME = "bearerAuth"; |
| 23 | + public static final String OAUTH2SCHEME = "oauth2"; |
| 24 | + |
| 25 | + @Bean |
| 26 | + @ConditionalOnProperty(name = "fr.insee.genesis.authentication", havingValue = "NONE") |
| 27 | + public OpenAPI noAuthOpenAPI() { |
| 28 | + return generateOpenAPI(); |
| 29 | + } |
| 30 | + |
| 31 | + @Bean |
| 32 | + @ConditionalOnProperty(name = "fr.insee.genesis.authentication", havingValue = "OIDC") |
| 33 | + public OpenAPI oidcOpenAPI(Config config) { |
| 34 | + String authUrl = config.getAuthServerUrl() + "/realms/" + config.getRealm() + "/protocol/openid-connect"; |
| 35 | + return generateOpenAPI() |
| 36 | + .addSecurityItem(new SecurityRequirement().addList(OAUTH2SCHEME)) |
| 37 | + .addSecurityItem(new SecurityRequirement().addList(BEARERSCHEME)) |
| 38 | + .components( |
| 39 | + new Components() |
| 40 | + .addSecuritySchemes(OAUTH2SCHEME, |
| 41 | + new SecurityScheme() |
| 42 | + .name(OAUTH2SCHEME) |
| 43 | + .type(SecurityScheme.Type.OAUTH2) |
| 44 | + .flows(getFlows(authUrl)) |
| 45 | + ) |
| 46 | + .addSecuritySchemes(BEARERSCHEME, |
| 47 | + new SecurityScheme() |
| 48 | + .name(BEARERSCHEME) |
| 49 | + .type(SecurityScheme.Type.HTTP) |
| 50 | + .scheme("bearer") |
| 51 | + .bearerFormat("JWT") |
| 52 | + ) |
| 53 | + ); |
| 54 | + } |
15 | 55 |
|
16 | | - @Bean |
17 | | - public OpenAPI customOpenAPI() { |
18 | | - return new OpenAPI() |
19 | | - .addServersItem(new Server().url("/")) |
20 | | - .info(new Info() |
21 | | - .title("Genesis API") |
22 | | - .description("Rest Endpoints and services to communicate with Genesis database") |
23 | | - .version(projectVersion) |
24 | | - ); |
25 | | - } |
| 56 | + private OpenAPI generateOpenAPI() { |
| 57 | + return new OpenAPI() |
| 58 | + .info(new Info() |
| 59 | + .title("Genesis API") |
| 60 | + .description("Rest Endpoints and services to communicate with Genesis database") |
| 61 | + .version(projectVersion) |
| 62 | + ); |
| 63 | + } |
26 | 64 |
|
| 65 | + private OAuthFlows getFlows(String authUrl) { |
| 66 | + OAuthFlows flows = new OAuthFlows(); |
| 67 | + OAuthFlow flow = new OAuthFlow(); |
| 68 | + Scopes scopes = new Scopes(); |
| 69 | + flow.setAuthorizationUrl(authUrl + "/auth"); |
| 70 | + flow.setTokenUrl(authUrl + "/token"); |
| 71 | + flow.setRefreshUrl(authUrl + "/token"); |
| 72 | + flow.setScopes(scopes); |
| 73 | + return flows.authorizationCode(flow); |
| 74 | + } |
27 | 75 |
|
28 | 76 | } |
0 commit comments