Skip to content

Commit e10092d

Browse files
committed
Move src folder to subproject
It also supports `ConnectionDetails`.
1 parent c815565 commit e10092d

File tree

13 files changed

+182
-73
lines changed

13 files changed

+182
-73
lines changed

build.gradle

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,87 @@
11
plugins {
2-
id 'java-library'
3-
id 'org.springframework.boot' version "${springBootVersion}" apply false
2+
id 'java'
43
id 'io.spring.dependency-management' version "${springDependencyManagementVersion}"
54

65
// quality
7-
id 'com.diffplug.spotless' version '6.25.0'
8-
id 'jacoco'
6+
id 'com.diffplug.spotless' version '6.25.0' apply false
97

108
// Publishing
11-
id 'maven-publish'
12-
id 'signing'
139
id 'io.github.gradle-nexus.publish-plugin' version '1.3.0'
1410
}
1511

16-
apply from: 'publish.gradle'
12+
subprojects {
13+
apply plugin: 'java'
14+
apply plugin: 'java-library'
15+
apply plugin: 'io.spring.dependency-management'
1716

18-
group = 'dev.openfga'
19-
version = '0.0.1'
17+
// quality
18+
apply plugin: 'com.diffplug.spotless'
19+
apply plugin: 'jacoco'
2020

21-
java {
22-
sourceCompatibility = 17
23-
targetCompatibility = 17
21+
apply from: "$rootDir/publish.gradle"
2422

25-
withJavadocJar()
26-
withSourcesJar()
27-
}
23+
group = 'dev.openfga'
24+
version = '0.0.1'
2825

29-
repositories {
30-
mavenCentral()
31-
}
26+
java {
27+
sourceCompatibility = 17
28+
targetCompatibility = 17
3229

33-
dependencyManagement {
34-
imports {
35-
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
30+
withJavadocJar()
31+
withSourcesJar()
3632
}
37-
}
3833

39-
dependencies {
40-
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
41-
42-
implementation 'org.springframework.boot:spring-boot'
43-
implementation 'org.springframework.boot:spring-boot-autoconfigure'
44-
implementation 'org.springframework.boot:spring-boot-starter-security'
45-
46-
api 'dev.openfga:openfga-sdk:0.4.0'
34+
repositories {
35+
mavenCentral()
36+
}
4737

48-
testImplementation 'org.springframework.boot:spring-boot-starter-test'
49-
testImplementation 'org.springframework.security:spring-security-test'
50-
testImplementation 'org.hamcrest:hamcrest:2.2'
51-
testImplementation 'org.mockito:mockito-core:5.11.0'
52-
}
38+
dependencyManagement {
39+
imports {
40+
mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootVersion}"
41+
}
42+
}
5343

54-
test {
55-
useJUnitPlatform()
56-
}
44+
test {
45+
useJUnitPlatform()
46+
}
5747

58-
spotless {
59-
format 'misc', {
60-
// define the files (e.g. '*.gradle', '*.md') to apply `misc` to
61-
target '.gitignore', '*.gradle'
62-
// define the steps to apply to those files
63-
trimTrailingWhitespace()
64-
indentWithSpaces()
65-
endWithNewline()
48+
spotless {
49+
format 'misc', {
50+
// define the files (e.g. '*.gradle', '*.md') to apply `misc` to
51+
target '.gitignore', '*.gradle'
52+
// define the steps to apply to those files
53+
trimTrailingWhitespace()
54+
indentWithSpaces()
55+
endWithNewline()
56+
}
57+
java {
58+
palantirJavaFormat()
59+
removeUnusedImports()
60+
importOrder()
61+
}
6662
}
67-
java {
68-
palantirJavaFormat()
69-
removeUnusedImports()
70-
importOrder()
63+
64+
test {
65+
finalizedBy jacocoTestReport // report is always generated after tests run
7166
}
72-
}
7367

74-
test {
75-
finalizedBy jacocoTestReport // report is always generated after tests run
76-
}
68+
jacocoTestReport {
69+
// tests are required to run before generating a JaCoCo coverage report.
70+
dependsOn test
71+
}
7772

78-
jacocoTestReport {
79-
// tests are required to run before generating a JaCoCo coverage report.
80-
dependsOn test
73+
tasks.register('fmt') {
74+
dependsOn 'spotlessApply'
75+
}
8176
}
8277

83-
tasks.register('fmt') {
84-
dependsOn 'spotlessApply'
78+
nexusPublishing {
79+
repositories {
80+
sonatype {
81+
nexusUrl.set(uri('https://s01.oss.sonatype.org/service/local/'))
82+
snapshotRepositoryUrl.set(uri('https://s01.oss.sonatype.org/content/repositories/snapshots/'))
83+
username.set(System.getenv('MAVEN_USERNAME'))
84+
password.set(System.getenv('MAVEN_PASSWORD'))
85+
}
86+
}
8587
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
description = 'This is the Spring Boot Starter for OpenFGA.'
2+
3+
dependencies {
4+
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
5+
6+
implementation 'org.springframework.boot:spring-boot'
7+
implementation 'org.springframework.boot:spring-boot-autoconfigure'
8+
implementation 'org.springframework.boot:spring-boot-starter-security'
9+
10+
api 'dev.openfga:openfga-sdk:0.4.0'
11+
12+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
13+
testImplementation 'org.springframework.security:spring-security-test'
14+
testImplementation 'org.hamcrest:hamcrest:2.2'
15+
testImplementation 'org.mockito:mockito-core:5.11.0'
16+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package dev.openfga.autoconfigure;
2+
3+
import dev.openfga.OpenFga;
4+
import dev.openfga.sdk.api.client.OpenFgaClient;
5+
import dev.openfga.sdk.api.configuration.*;
6+
import dev.openfga.sdk.errors.FgaInvalidParameterException;
7+
import org.springframework.beans.factory.BeanCreationException;
8+
import org.springframework.boot.autoconfigure.AutoConfiguration;
9+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
10+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
11+
import org.springframework.context.annotation.Bean;
12+
13+
/**
14+
* Configures an {@code openFgaClient} and {@code openFga} beans based
15+
* on configuration values. The beans will only be created if the
16+
* {@link OpenFgaClient} is present on the classpath, and the
17+
* {@code openfga.api-url} is specified.
18+
*/
19+
@AutoConfiguration
20+
@ConditionalOnFgaProperties
21+
@EnableConfigurationProperties(OpenFgaProperties.class)
22+
public class OpenFgaAutoConfiguration {
23+
24+
@Bean
25+
@ConditionalOnMissingBean(OpenFgaConnectionDetails.class)
26+
PropertiesOpenFgaConnectionDetails openFgaConnectionDetails(OpenFgaProperties openFgaProperties) {
27+
return new PropertiesOpenFgaConnectionDetails(openFgaProperties);
28+
}
29+
30+
@Bean
31+
@ConditionalOnMissingBean
32+
public ClientConfiguration fgaConfig(OpenFgaProperties properties, OpenFgaConnectionDetails connectionDetails) {
33+
var credentials = new Credentials();
34+
35+
var credentialsProperties = properties.getCredentials();
36+
37+
if (credentialsProperties != null) {
38+
if (OpenFgaProperties.CredentialsMethod.API_TOKEN.equals(credentialsProperties.getMethod())) {
39+
credentials.setCredentialsMethod(CredentialsMethod.API_TOKEN);
40+
credentials.setApiToken(
41+
new ApiToken(credentialsProperties.getConfig().getApiToken()));
42+
} else if (OpenFgaProperties.CredentialsMethod.CLIENT_CREDENTIALS.equals(
43+
credentialsProperties.getMethod())) {
44+
ClientCredentials clientCredentials = new ClientCredentials()
45+
.clientId(credentialsProperties.getConfig().getClientId())
46+
.clientSecret(credentialsProperties.getConfig().getClientSecret())
47+
.apiTokenIssuer(credentialsProperties.getConfig().getApiTokenIssuer())
48+
.apiAudience(credentialsProperties.getConfig().getApiAudience())
49+
.scopes(credentialsProperties.getConfig().getScopes());
50+
51+
credentials.setCredentialsMethod(CredentialsMethod.CLIENT_CREDENTIALS);
52+
credentials.setClientCredentials(clientCredentials);
53+
}
54+
}
55+
56+
return new ClientConfiguration()
57+
.apiUrl(connectionDetails.getApiUrl())
58+
.storeId(properties.getStoreId())
59+
.authorizationModelId(properties.getAuthorizationModelId())
60+
.credentials(credentials);
61+
}
62+
63+
@Bean
64+
@ConditionalOnMissingBean
65+
public OpenFgaClient fgaClient(ClientConfiguration configuration) {
66+
try {
67+
return new OpenFgaClient(configuration);
68+
} catch (FgaInvalidParameterException e) {
69+
throw new BeanCreationException("Failed to create OpenFgaClient", e);
70+
}
71+
}
72+
73+
@Bean
74+
@ConditionalOnMissingBean
75+
public OpenFga fga(OpenFgaClient openFgaClient) {
76+
return new OpenFga(openFgaClient);
77+
}
78+
79+
static class PropertiesOpenFgaConnectionDetails implements OpenFgaConnectionDetails {
80+
81+
private final OpenFgaProperties openFgaProperties;
82+
83+
public PropertiesOpenFgaConnectionDetails(OpenFgaProperties openFgaProperties) {
84+
this.openFgaProperties = openFgaProperties;
85+
}
86+
87+
@Override
88+
public String getApiUrl() {
89+
return this.openFgaProperties.getApiUrl();
90+
}
91+
}
92+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dev.openfga.autoconfigure;
2+
3+
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
4+
5+
public interface OpenFgaConnectionDetails extends ConnectionDetails {
6+
7+
String getApiUrl();
8+
}

0 commit comments

Comments
 (0)