Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions license-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@
<java.version>21</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<jjwt.version>0.12.7</jjwt.version>
<bcprov-jdk18on.version>1.81</bcprov-jdk18on.version>
<jackson-databind.version>2.19.2</jackson-databind.version>
<logback-classic.version>1.5.18</logback-classic.version>
<slf4j-api.version>2.0.17</slf4j-api.version>

<maven-compiler-plugin.version>3.14.0</maven-compiler-plugin.version>

<junit.jupiter.version>5.13.4</junit.jupiter.version>
<maven-surefire-plugin.version>3.5.3</maven-surefire-plugin.version>
<jacoco-maven-plugin.version>0.8.13</jacoco-maven-plugin.version>
<system-lambda.version>1.2.1</system-lambda.version>
<maven-compiler-plugin.version>3.14.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>3.5.4</maven-surefire-plugin.version>
<maven-failsafe-plugin.version>3.5.4</maven-failsafe-plugin.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -111,22 +109,30 @@
<executions>
<execution>
<id>prepare-agent</id>
<goals><goal>prepare-agent</goal></goals>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals><goal>report</goal></goals>
<goals>
<goal>report</goal>
</goals>
</execution>

<execution>
<id>prepare-agent-integration</id>
<goals><goal>prepare-agent-integration</goal></goals>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>report-integration</id>
<phase>verify</phase>
<goals><goal>report-integration</goal></goals>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
</executions>
</plugin>
Expand All @@ -141,7 +147,7 @@

<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<version>${maven-failsafe-plugin.version}</version>
<executions>
<execution>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ static Optional<String> readOpt(String[] argv, String name) {

private static void printUsage() {
log.info(
"""
"""
Usage:
# Sign sample payload (encrypted license key variant)
java -cp license-generator.jar io.github.bsayli.license.signature.SignatureDemo \\
--mode sign-sample-key \\
--privateKey <base64-pkcs8-ed25519>

# Sign sample payload (license token variant)
java -cp license-generator.jar io.github.bsayli.license.signature.SignatureDemo \\
--mode sign-sample-token \\
--privateKey <base64-pkcs8-ed25519>
""");
}
}
}
4 changes: 2 additions & 2 deletions licensing-service-sdk-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ java -jar target/licensing-service-sdk-cli-<version>.jar \
### Options

| Flag | Long Option | Description | Required |
| ---- | ------------------- | ---------------------------------------------------------------------------- | -------- |
|------|---------------------|------------------------------------------------------------------------------|----------|
| `-k` | `--key` | License key string (`PREFIX~RANDOM~ENCRYPTED_USER_ID`) | Yes |
| `-s` | `--service-id` | Service identifier (e.g. `crm`) | Yes |
| `-v` | `--service-version` | Service version (e.g. `1.5.0`) | Yes |
Expand Down Expand Up @@ -122,7 +122,7 @@ LICENSE_SERVICE_SDK_API_PATH=/v1/licenses/access
## Exit Codes

| Code | Meaning |
| ---- | ----------------------------------------------- |
|------|-------------------------------------------------|
| `0` | License validated successfully |
| `1` | License validation failed (client/server error) |
| `2` | CLI usage error |
Expand Down
38 changes: 38 additions & 0 deletions licensing-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.codemonstur</groupId>
<artifactId>embedded-redis</artifactId>
<version>1.4.3</version>
<scope>test</scope>
</dependency>


</dependencies>

<build>
Expand All @@ -150,6 +158,20 @@
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
Expand All @@ -161,13 +183,29 @@
<goal>prepare-agent</goal>
</goals>
</execution>

<execution>
<id>prepare-agent-integration</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>

<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>

<execution>
<id>report-integration</id>
<phase>verify</phase>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
</executions>
</plugin>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package io.github.bsayli.licensing.api.controller;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.bsayli.licensing.api.dto.IssueAccessRequest;
import io.github.bsayli.licensing.api.dto.LicenseAccessResponse;
import io.github.bsayli.licensing.api.dto.ValidateAccessRequest;
import io.github.bsayli.licensing.api.exception.LicenseControllerAdvice;
import io.github.bsayli.licensing.common.i18n.LocalizedMessageResolver;
import io.github.bsayli.licensing.service.LicenseOrchestrationService;
import io.github.bsayli.licensing.testconfig.TestControllerMocksConfig;
import io.github.bsayli.licensing.testconfig.TestWebMvcSecurityConfig;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

@WebMvcTest(controllers = LicenseController.class)
@Import({
LicenseControllerAdvice.class,
TestWebMvcSecurityConfig.class,
TestControllerMocksConfig.class
})
@Tag("integration")
class LicenseControllerIT {

@Autowired private MockMvc mvc;
@Autowired private ObjectMapper om;
@Autowired private LicenseOrchestrationService service;
@Autowired private LocalizedMessageResolver messageResolver;

private static String fakeJwt() {
String p1 = "A".repeat(80);
String p2 = "b".repeat(80);
String p3 = "C".repeat(80);
return p1 + "." + p2 + "." + p3;
}

@Test
@DisplayName("POST /v1/licenses/access -> 200 returns token")
void createAccess_ok() throws Exception {
var req =
new IssueAccessRequest(
"L".repeat(100) + "~rnd~" + "A".repeat(64),
"licensing-service~demo~00:11:22:33:44:55",
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8a5",
"crm",
"1.5.0",
"Q".repeat(60));

Mockito.when(service.issueAccess(req)).thenReturn(LicenseAccessResponse.created("jwt-token"));
Mockito.when(messageResolver.getMessage("license.validation.success"))
.thenReturn("License is valid");

mvc.perform(
post("/v1/licenses/access")
.contentType(MediaType.APPLICATION_JSON)
.content(om.writeValueAsBytes(req)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.status").value(200))
.andExpect(jsonPath("$.message").value("License is valid"))
.andExpect(jsonPath("$.data.licenseToken").value("jwt-token"));
}

@Test
@DisplayName("POST /v1/licenses/access -> 400 validation error")
void createAccess_validationError() throws Exception {
var bad = new IssueAccessRequest("x", "short", null, "c", "1", "y");

mvc.perform(
post("/v1/licenses/access")
.contentType(MediaType.APPLICATION_JSON)
.content(om.writeValueAsBytes(bad)))
.andExpect(status().isBadRequest())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.message").value("Request validation failed"))
.andExpect(jsonPath("$.errors").isArray());
}

@Test
@DisplayName("POST /v1/licenses/access/validate -> 200 returns refreshed token")
void validateAccess_ok_refreshed() throws Exception {
var req =
new ValidateAccessRequest(
"licensing-service~demo~00:11:22:33:44:55",
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8a5",
"crm",
"1.5.0",
"Q".repeat(60));

String jwt = fakeJwt();

Mockito.when(service.validateAccess(req, jwt))
.thenReturn(LicenseAccessResponse.refreshed("new-jwt"));
Mockito.when(messageResolver.getMessage("license.validation.success"))
.thenReturn("License is valid");

mvc.perform(
post("/v1/licenses/access/validate")
.contentType(MediaType.APPLICATION_JSON)
.header("License-Token", jwt)
.content(om.writeValueAsBytes(req)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.licenseToken").value("new-jwt"));
}

@Test
@DisplayName("POST /v1/licenses/access/validate -> 400 invalid or missing header JWT")
void validateAccess_badHeader() throws Exception {
var req =
new ValidateAccessRequest(
"licensing-service~demo~00:11:22:33:44:55", null, "crm", "1.5.0", "Q".repeat(60));

// Missing header
mvc.perform(
post("/v1/licenses/access/validate")
.contentType(MediaType.APPLICATION_JSON)
.content(om.writeValueAsBytes(req)))
.andExpect(status().isBadRequest())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.message").exists())
.andExpect(jsonPath("$.errors").isArray());

// Not 3-part JWT
mvc.perform(
post("/v1/licenses/access/validate")
.contentType(MediaType.APPLICATION_JSON)
.header("License-Token", "abc.def")
.content(om.writeValueAsBytes(req)))
.andExpect(status().isBadRequest())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.errors").isArray());
}
}
Loading