From: Michael Rasmussen Date: Mon, 16 Nov 2026 18:05:10 +0000 (+0100) Subject: initial commit X-Git-Url: http://git.datanom.net/token-manager.git/commitdiff_plain?ds=inline initial commit Signed-off-by: Michael Rasmussen --- a37bd04781cae995649f768bc69d5ea6e010c38a diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e35e754 --- /dev/null +++ b/pom.xml @@ -0,0 +1,142 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 4.1.2 + + + dk.cbs.soa.soasi.service + tokenserver + 0.0.1-SNAPSHOT + + + + + + + + + + + + + + + + + 21 + + + + dk.cbs.soa.soasi.commons + jwt + 0.0.1 + + + dk.cbs.soa.soasi.commons + rest-helper + 4.0.0 + + + org.springdoc + springdoc-openapi-starter-webmvc-ui + 2.7.0 + + + org.springframework.boot + spring-boot-starter-jdbc + + + com.zaxxer + HikariCP + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-webmvc + + + + org.postgresql + postgresql + runtime + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-jdbc-test + test + + + org.springframework.boot + spring-boot-starter-thymeleaf-test + test + + + org.springframework.boot + spring-boot-starter-webmvc-test + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-compiler-plugin + + + default-compile + compile + + compile + + + + + org.projectlombok + lombok + + + + + + default-testCompile + test-compile + + testCompile + + + + + org.projectlombok + lombok + + + + + + + + + + diff --git a/src/main/java/dk/cbs/soa/soasi/service/tokenserver/TokenserverApplication.java b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/TokenserverApplication.java new file mode 100644 index 0000000..e03ed9b --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/TokenserverApplication.java @@ -0,0 +1,13 @@ +package dk.cbs.soa.soasi.service.tokenserver; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class TokenserverApplication { + + public static void main(String[] args) { + SpringApplication.run(TokenserverApplication.class, args); + } + +} diff --git a/src/main/java/dk/cbs/soa/soasi/service/tokenserver/config/DatabaseConfig.java b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/config/DatabaseConfig.java new file mode 100644 index 0000000..c0b7498 --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/config/DatabaseConfig.java @@ -0,0 +1,48 @@ +package dk.cbs.soa.soasi.service.tokenserver.config; + +import java.sql.SQLException; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; + +import lombok.extern.slf4j.Slf4j; + +@Configuration +@EnableTransactionManagement +@Slf4j +public class DatabaseConfig { + + @Value("${soasi.token-server-db.service.url}") + private String url; + @Value("${soasi.token-server-db.service.username}") + private String username; + @Value("${soasi.token-server-db.service.password}") + private String password; + + @Bean + @ConfigurationProperties("soasi.token-server-db.service") + public HikariConfig domainHikariConfig() { + return new HikariConfig(); + } + + @Bean + @Qualifier("serviceDbDataSource") + public HikariDataSource domainDatasource() throws SQLException { + log.info("Creating service datasource"); + return new HikariDataSource(domainHikariConfig()); + } + + @Bean + @Qualifier("serviceRealJdbcTemplate") + public JdbcTemplate domainRealJdbctemplate() throws SQLException { + return new JdbcTemplate(domainDatasource()); + } +} diff --git a/src/main/java/dk/cbs/soa/soasi/service/tokenserver/config/SecurityConfig.java b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/config/SecurityConfig.java new file mode 100644 index 0000000..9f31ff5 --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/config/SecurityConfig.java @@ -0,0 +1,67 @@ +package dk.cbs.soa.soasi.service.tokenserver.config; + +import java.util.Collections; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.ProviderManager; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; +import org.springframework.security.config.http.SessionCreationPolicy; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.provisioning.InMemoryUserDetailsManager; +import org.springframework.security.web.SecurityFilterChain; +import org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter; +import org.springframework.security.web.header.HeaderWriterFilter; +import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher; + +import io.swagger.v3.oas.annotations.OpenAPIDefinition; +import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn; +import io.swagger.v3.oas.annotations.enums.SecuritySchemeType; +import io.swagger.v3.oas.annotations.info.Info; +import io.swagger.v3.oas.annotations.security.SecurityScheme; +import jakarta.servlet.http.HttpServletResponse; + +@Configuration +@EnableWebSecurity +@OpenAPIDefinition(info = @Info(title = "CBS Token Service API", version = "1.0.0")) +//@SecurityScheme(name = "x-api-key", scheme = "basic", type = SecuritySchemeType.APIKEY, in = SecuritySchemeIn.HEADER) +public class SecurityConfig { + + public static final String AUTH_HEADER_API_KEY = "x-api-key"; + + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + http + .cors(AbstractHttpConfigurer::disable) + .csrf(AbstractHttpConfigurer::disable) + .sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) + //.addFilterAfter(requestHeaderAuthenticationFilter, HeaderWriterFilter.class) + .authorizeHttpRequests(request -> request + .requestMatchers("/v1/api/**").authenticated() + .requestMatchers("/**").permitAll()) + .exceptionHandling(excConf -> excConf.authenticationEntryPoint((request, response, authException) -> + response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid or missing API key"))); + + return http.build(); + } +/* + @Bean + public RequestHeaderAuthenticationFilter requestHeaderAuthenticationFilter(AuthenticationManager authenticationManager) { + //RequestHeaderAuthenticationFilter filter = new RequestHeaderAuthenticationFilter(); + //filter.setPrincipalRequestHeader(AUTH_HEADER_API_KEY); + filter.setExceptionIfHeaderMissing(false); + filter.setRequiresAuthenticationRequestMatcher(PathPatternRequestMatcher.withDefaults().matcher("/v1/api/**")); + //filter.setAuthenticationManager(authenticationManager); + + return filter; + } +*/ + @Bean + public UserDetailsService userDetailsService() { + return new InMemoryUserDetailsManager(); + } + +} diff --git a/src/main/java/dk/cbs/soa/soasi/service/tokenserver/controller/TokenServer.java b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/controller/TokenServer.java new file mode 100644 index 0000000..0f86f37 --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/controller/TokenServer.java @@ -0,0 +1,46 @@ +package dk.cbs.soa.soasi.service.tokenserver.controller; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import dk.cbs.soa.soasi.service.tokenserver.model.AuthCredential; +import dk.cbs.soa.soasi.service.tokenserver.model.Token; +import dk.cbs.soa.soasi.service.tokenserver.model.TokenPair; +import dk.cbs.soa.soasi.service.tokenserver.service.AuthService; +import io.swagger.v3.oas.annotations.Parameter; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +@RestController +@RequestMapping(value = "/v1/api", produces = "application/json") +@Slf4j +@RequiredArgsConstructor +public class TokenServer { + + private final AuthService authService; + + @PostMapping("/auth") + public ResponseEntity authenticate( + @Parameter(description = "AuthCredential must be digitally signed with a X.509 certifikate. Signature is part of credentials") + @RequestBody AuthCredential authCredential) { + return new ResponseEntity(authService.getTokenPair(authCredential), HttpStatus.OK); + } + + @PostMapping("/refresh") + public ResponseEntity refresh( + @Parameter(description = "Refresh token to be reneewed. A refresh token can max be used 5 times before aauirering a new from authenticate") + @RequestBody String refreshToken) { + return new ResponseEntity(authService.getRefreshPair(refreshToken), HttpStatus.OK); + } + + @PostMapping("/token") + public ResponseEntity getToken( + @Parameter(description = "Refresh token to create access from") + @RequestBody String refreshToken) { + return new ResponseEntity(authService.getAccessToken(refreshToken), HttpStatus.OK); + } +} diff --git a/src/main/java/dk/cbs/soa/soasi/service/tokenserver/model/AuthCredential.java b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/model/AuthCredential.java new file mode 100644 index 0000000..8cc8a9d --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/model/AuthCredential.java @@ -0,0 +1,12 @@ +package dk.cbs.soa.soasi.service.tokenserver.model; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class AuthCredential { + private String userPrincipal; + private String endPoint; + private String signature; +} diff --git a/src/main/java/dk/cbs/soa/soasi/service/tokenserver/model/Token.java b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/model/Token.java new file mode 100644 index 0000000..928d6f1 --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/model/Token.java @@ -0,0 +1,13 @@ +package dk.cbs.soa.soasi.service.tokenserver.model; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class Token { + private String access_token; + private int expires_in; + private String token_type; + private String scope; +} diff --git a/src/main/java/dk/cbs/soa/soasi/service/tokenserver/model/TokenPair.java b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/model/TokenPair.java new file mode 100644 index 0000000..722fb9c --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/model/TokenPair.java @@ -0,0 +1,11 @@ +package dk.cbs.soa.soasi.service.tokenserver.model; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class TokenPair { + private String refreshToken; + private String accessToken; +} diff --git a/src/main/java/dk/cbs/soa/soasi/service/tokenserver/repository/ClaimsRowMapper.java b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/repository/ClaimsRowMapper.java new file mode 100644 index 0000000..b0ca054 --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/repository/ClaimsRowMapper.java @@ -0,0 +1,16 @@ +package dk.cbs.soa.soasi.service.tokenserver.repository; + +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.springframework.jdbc.core.RowMapper; + +public class ClaimsRowMapper implements RowMapper { + + @Override + public String mapRow(ResultSet rs, int rowNum) throws SQLException { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/main/java/dk/cbs/soa/soasi/service/tokenserver/repository/ClientData.java b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/repository/ClientData.java new file mode 100644 index 0000000..6cc9433 --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/repository/ClientData.java @@ -0,0 +1,79 @@ +package dk.cbs.soa.soasi.service.tokenserver.repository; + +import java.util.List; +import java.util.Optional; + +import org.springframework.jdbc.core.simple.JdbcClient; +import org.springframework.stereotype.Repository; + +import lombok.RequiredArgsConstructor; + +@Repository +@RequiredArgsConstructor +public class ClientData { + + private final JdbcClient jdbcClient; + private final String pubCertKeySQL = "select certkey from client where principal = ? "; + private final String pubKeySQL = "select pkey from client where principal = ? "; + private final String claimsSQL = "select claim from client c join claims cl on c.id = cl.cid"; + + /* + * Tabel structure + * Client + * int id + * varchar userPrincipal + * varchar public_certkey + * varchar public_Key + * + * Cliams + * int id + * int cid + * varchar claim + */ + + public Optional getPublicCertKey(String userPrincipal) { + Optional key = Optional.empty(); + + Optional cert = jdbcClient.sql(pubCertKeySQL) + .query() + .optionalValue(); + + if (cert.isPresent()) { + String certKey = (String) cert.get(); + key = Optional.of(certKey); + } + + return key; + } + + public Optional getPublicKey(String userPrincipal) { + Optional key = Optional.empty(); + + Optional pubKey = jdbcClient.sql(pubKeySQL) + .query() + .optionalValue(); + + if (pubKey.isPresent()) { + String pKey = (String) pubKey.get(); + key = Optional.of(pKey); + } + + return key; + } + + public Optional> getClaims(String userPrincipal) { + Optional key = Optional.empty(); + + List claims = jdbcClient.sql(claimsSQL) + .query(ClaimsRowMapper.getInstance) + .list(); + + if (pubKey.isPresent()) { + String pKey = (String) pubKey.get(); + key = Optional.of(pKey); + } + + return key; + } + +} diff --git a/src/main/java/dk/cbs/soa/soasi/service/tokenserver/service/AuthService.java b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/service/AuthService.java new file mode 100644 index 0000000..b11fc10 --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/service/tokenserver/service/AuthService.java @@ -0,0 +1,27 @@ +package dk.cbs.soa.soasi.service.tokenserver.service; + +import org.springframework.stereotype.Service; + +import dk.cbs.soa.soasi.service.tokenserver.model.AuthCredential; +import dk.cbs.soa.soasi.service.tokenserver.model.Token; +import dk.cbs.soa.soasi.service.tokenserver.model.TokenPair; + +@Service +public class AuthService { + + public TokenPair getTokenPair(AuthCredential authCredential) { + // TODO Auto-generated method stub + return null; + } + + public TokenPair getRefreshPair(String refreshToken) { + // TODO Auto-generated method stub + return null; + } + + public Token getAccessToken(String refreshToken) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..6fb63c1 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,23 @@ +spring.application.name=tokenserver +spring.application.version=@project.version@ + +springdoc.swagger-ui.displayRequestDuration=true +logging.level.org.apache.hc.client5.http=INFO + +springdoc.api-docs.path=/v1/api-docs +springdoc.paths-to-match=/v1/api/** + + +server.port=7007 + +soasi.token-server-db.service.username=tokenserver +soasi.token-server-db.service.password=tokenserver +soasi.token-server-db.service.read-only=false +soasi.token-server-db.service.jdbc-url=jdbc:postgresql://localhost:5432/tokenserver +soasi.token-server-db.service.url=${soasi.token-server-db.service.jdbc-url} + +soasi.token-server-db.service.validation-query=SELECT +soasi.token-server-db.service.connectionTimeout=30000 +soasi.token-server-db.service.idleTimeout=600000 +soasi.token-server-db.service.maxLifetime=1800000 +soasi.token-server-db.service.maximum-pool-size=2 diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 0000000..2571001 --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,10 @@ + + + + +Insert title here + + + + + \ No newline at end of file diff --git a/src/test/java/dk/cbs/soa/soasi/service/tokenserver/TokenserverApplicationTests.java b/src/test/java/dk/cbs/soa/soasi/service/tokenserver/TokenserverApplicationTests.java new file mode 100644 index 0000000..51118eb --- /dev/null +++ b/src/test/java/dk/cbs/soa/soasi/service/tokenserver/TokenserverApplicationTests.java @@ -0,0 +1,13 @@ +package dk.cbs.soa.soasi.service.tokenserver; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class TokenserverApplicationTests { + + @Test + void contextLoads() { + } + +}