]> git.datanom.net - token-manager.git/commitdiff
initial commit master
authorMichael Rasmussen <mir@datanom.net>
Mon, 16 Nov 2026 18:05:10 +0000 (19:05 +0100)
committerMichael Rasmussen <mir@datanom.net>
Mon, 16 Nov 2026 18:05:10 +0000 (19:05 +0100)
Signed-off-by: Michael Rasmussen <mir@datanom.net>
14 files changed:
pom.xml [new file with mode: 0644]
src/main/java/dk/cbs/soa/soasi/service/tokenserver/TokenserverApplication.java [new file with mode: 0644]
src/main/java/dk/cbs/soa/soasi/service/tokenserver/config/DatabaseConfig.java [new file with mode: 0644]
src/main/java/dk/cbs/soa/soasi/service/tokenserver/config/SecurityConfig.java [new file with mode: 0644]
src/main/java/dk/cbs/soa/soasi/service/tokenserver/controller/TokenServer.java [new file with mode: 0644]
src/main/java/dk/cbs/soa/soasi/service/tokenserver/model/AuthCredential.java [new file with mode: 0644]
src/main/java/dk/cbs/soa/soasi/service/tokenserver/model/Token.java [new file with mode: 0644]
src/main/java/dk/cbs/soa/soasi/service/tokenserver/model/TokenPair.java [new file with mode: 0644]
src/main/java/dk/cbs/soa/soasi/service/tokenserver/repository/ClaimsRowMapper.java [new file with mode: 0644]
src/main/java/dk/cbs/soa/soasi/service/tokenserver/repository/ClientData.java [new file with mode: 0644]
src/main/java/dk/cbs/soa/soasi/service/tokenserver/service/AuthService.java [new file with mode: 0644]
src/main/resources/application.properties [new file with mode: 0644]
src/main/resources/templates/index.html [new file with mode: 0644]
src/test/java/dk/cbs/soa/soasi/service/tokenserver/TokenserverApplicationTests.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
new file mode 100644 (file)
index 0000000..e35e754
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,142 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+       <modelVersion>4.0.0</modelVersion>
+       <parent>
+               <groupId>org.springframework.boot</groupId>
+               <artifactId>spring-boot-starter-parent</artifactId>
+               <version>4.1.2</version>
+               <relativePath /> <!-- lookup parent from repository -->
+       </parent>
+       <groupId>dk.cbs.soa.soasi.service</groupId>
+       <artifactId>tokenserver</artifactId>
+       <version>0.0.1-SNAPSHOT</version>
+       <name />
+       <description />
+       <url />
+       <licenses>
+               <license />
+       </licenses>
+       <developers>
+               <developer />
+       </developers>
+       <scm>
+               <connection />
+               <developerConnection />
+               <tag />
+               <url />
+       </scm>
+       <properties>
+               <java.version>21</java.version>
+       </properties>
+       <dependencies>
+               <dependency>
+                       <groupId>dk.cbs.soa.soasi.commons</groupId>
+                       <artifactId>jwt</artifactId>
+                       <version>0.0.1</version>
+               </dependency>
+               <dependency>
+                       <groupId>dk.cbs.soa.soasi.commons</groupId>
+                       <artifactId>rest-helper</artifactId>
+                       <version>4.0.0</version>
+               </dependency>
+               <dependency>
+                       <groupId>org.springdoc</groupId>
+                       <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
+                       <version>2.7.0</version>
+               </dependency>
+               <dependency>
+                       <groupId>org.springframework.boot</groupId>
+                       <artifactId>spring-boot-starter-jdbc</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>com.zaxxer</groupId>
+                       <artifactId>HikariCP</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.springframework.boot</groupId>
+                       <artifactId>spring-boot-starter-thymeleaf</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.springframework.boot</groupId>
+                       <artifactId>spring-boot-starter-webmvc</artifactId>
+               </dependency>
+
+               <dependency>
+                       <groupId>org.postgresql</groupId>
+                       <artifactId>postgresql</artifactId>
+                       <scope>runtime</scope>
+               </dependency>
+               <dependency>
+                       <groupId>org.projectlombok</groupId>
+                       <artifactId>lombok</artifactId>
+                       <optional>true</optional>
+               </dependency>
+               <dependency>
+                       <groupId>org.springframework.boot</groupId>
+                       <artifactId>spring-boot-starter-jdbc-test</artifactId>
+                       <scope>test</scope>
+               </dependency>
+               <dependency>
+                       <groupId>org.springframework.boot</groupId>
+                       <artifactId>spring-boot-starter-thymeleaf-test</artifactId>
+                       <scope>test</scope>
+               </dependency>
+               <dependency>
+                       <groupId>org.springframework.boot</groupId>
+                       <artifactId>spring-boot-starter-webmvc-test</artifactId>
+                       <scope>test</scope>
+               </dependency>
+       </dependencies>
+
+       <build>
+               <plugins>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-compiler-plugin</artifactId>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.springframework.boot</groupId>
+                               <artifactId>spring-boot-maven-plugin</artifactId>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-compiler-plugin</artifactId>
+                               <executions>
+                                       <execution>
+                                               <id>default-compile</id>
+                                               <phase>compile</phase>
+                                               <goals>
+                                                       <goal>compile</goal>
+                                               </goals>
+                                               <configuration>
+                                                       <annotationProcessorPaths>
+                                                               <path>
+                                                                       <groupId>org.projectlombok</groupId>
+                                                                       <artifactId>lombok</artifactId>
+                                                               </path>
+                                                       </annotationProcessorPaths>
+                                               </configuration>
+                                       </execution>
+                                       <execution>
+                                               <id>default-testCompile</id>
+                                               <phase>test-compile</phase>
+                                               <goals>
+                                                       <goal>testCompile</goal>
+                                               </goals>
+                                               <configuration>
+                                                       <annotationProcessorPaths>
+                                                               <path>
+                                                                       <groupId>org.projectlombok</groupId>
+                                                                       <artifactId>lombok</artifactId>
+                                                               </path>
+                                                       </annotationProcessorPaths>
+                                               </configuration>
+                                       </execution>
+                               </executions>
+                       </plugin>
+               </plugins>
+       </build>
+
+</project>
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 (file)
index 0000000..e03ed9b
--- /dev/null
@@ -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 (file)
index 0000000..c0b7498
--- /dev/null
@@ -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 (file)
index 0000000..9f31ff5
--- /dev/null
@@ -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 (file)
index 0000000..0f86f37
--- /dev/null
@@ -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<TokenPair> authenticate(
+                       @Parameter(description = "AuthCredential must be digitally signed with a X.509 certifikate. Signature is part of credentials")
+                       @RequestBody AuthCredential authCredential) {
+               return new ResponseEntity<TokenPair>(authService.getTokenPair(authCredential), HttpStatus.OK);
+       }
+
+       @PostMapping("/refresh")
+       public ResponseEntity<TokenPair> 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<TokenPair>(authService.getRefreshPair(refreshToken), HttpStatus.OK);
+       }
+
+       @PostMapping("/token")
+       public ResponseEntity<Token> getToken(
+                       @Parameter(description = "Refresh token to create access from")
+                       @RequestBody String refreshToken) {
+               return new ResponseEntity<Token>(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 (file)
index 0000000..8cc8a9d
--- /dev/null
@@ -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 (file)
index 0000000..928d6f1
--- /dev/null
@@ -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 (file)
index 0000000..722fb9c
--- /dev/null
@@ -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 (file)
index 0000000..b0ca054
--- /dev/null
@@ -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<String> {
+
+       @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 (file)
index 0000000..6cc9433
--- /dev/null
@@ -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<String> getPublicCertKey(String userPrincipal) {
+               Optional<String> key = Optional.empty();
+               
+               Optional<Object> cert = jdbcClient.sql(pubCertKeySQL)
+                               .query()
+                               .optionalValue();
+
+               if (cert.isPresent()) {
+                       String certKey = (String) cert.get();
+                       key = Optional.of(certKey);
+               }
+
+               return key;
+       }
+
+       public Optional<String> getPublicKey(String userPrincipal) {
+               Optional<String> key = Optional.empty();
+               
+               Optional<Object> pubKey = jdbcClient.sql(pubKeySQL)
+                               .query()
+                               .optionalValue();
+
+               if (pubKey.isPresent()) {
+                       String pKey = (String) pubKey.get();
+                       key = Optional.of(pKey);
+               }
+
+               return key;
+       }
+
+       public Optional<List<String>> getClaims(String userPrincipal) {
+               Optional<String> key = Optional.empty();
+               
+               List<String> 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 (file)
index 0000000..b11fc10
--- /dev/null
@@ -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 (file)
index 0000000..6fb63c1
--- /dev/null
@@ -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 (file)
index 0000000..2571001
--- /dev/null
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="UTF-8">
+<title>Insert title here</title>
+</head>
+<body>
+
+</body>
+</html>
\ 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 (file)
index 0000000..51118eb
--- /dev/null
@@ -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() {
+       }
+
+}
This page took 0.508789 seconds and 5 git commands to generate.