Skip to content

Commit

Permalink
Return 403 when test codes are disabled (#12)
Browse files Browse the repository at this point in the history
* Return 403 for diga test code requests when DISABLE_TESTCODES=true

* Add error response example to README
  • Loading branch information
bokchan authored Aug 24, 2021
1 parent 833b81f commit 7672265
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ docker run --env-file .env -p 5000:5000 -v /tmp/Krankenkassenverzeichnis_DiGA.xm
"message": "DigaCodeValidationResponseError(errorCode=REQUEST_OR_DATA_INVALID, errorText=Anfrage oder Datei ungültig)",
"path": "/validate/77AAAAAAAAAAAGIS"
}
Example error response for diga test code when DISABLE_TESTCODES=true
{
"timestamp": "2021-08-23T09:38:32.385+00:00",
"status": 403,
"error": "Forbidden",
"message": "Testcodes are not allowed",
"path": "/validate/77AAAAAAAAAAAAAX"
}
```
* POST /bill
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/dev/gtuk/diga/AppExceptionHandler.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.gtuk.diga

import dev.gtuk.diga.exceptions.BillingException
import dev.gtuk.diga.exceptions.TestCodesDisabledException
import dev.gtuk.diga.exceptions.ValidationException
import javax.servlet.http.HttpServletResponse
import org.springframework.http.HttpStatus
Expand All @@ -19,4 +20,9 @@ class AppExceptionHandler {
fun handleBillingException(e: BillingException, response: HttpServletResponse) {
response.sendError(HttpStatus.BAD_REQUEST.value())
}

@ExceptionHandler(TestCodesDisabledException::class)
fun handleCodeValidationException(e: TestCodesDisabledException, response: HttpServletResponse) {
response.sendError(HttpStatus.FORBIDDEN.value())
}
}
7 changes: 5 additions & 2 deletions src/main/kotlin/dev/gtuk/diga/DigaService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import dev.gtuk.diga.dtos.BillingResponse
import dev.gtuk.diga.dtos.ValidationResponse
import dev.gtuk.diga.exceptions.BillingException
import dev.gtuk.diga.exceptions.CodeValidationException
import dev.gtuk.diga.exceptions.TestCodesDisabledException
import dev.gtuk.diga.exceptions.ValidationException
import java.io.FileInputStream
import kotlin.jvm.Throws
Expand Down Expand Up @@ -64,18 +65,20 @@ class DigaService(private val appConfig: AppConfig) {
this.apiClient = DigaApiClient(apiClientSettings, digaInformation)
}

@Throws(ValidationException::class)
@Throws(ValidationException::class, TestCodesDisabledException::class)
fun verify(code: String): ValidationResponse {
val isTestCode = Utils.isTestCode(code)

val response: DigaCodeValidationResponse = try {
if (isTestCode && appConfig.disableTestcodes) {
throw CodeValidationException("Testcodes are not allowed")
throw TestCodesDisabledException("Testcodes are not allowed")
} else if (isTestCode) {
this.apiClient.sendTestCodeValidationRequest(Utils.getDigaTestCode(code), appConfig.testInsurance)
} else {
this.apiClient.validateDigaCode(code)
}
} catch (e: TestCodesDisabledException) {
throw e
} catch (e: Exception) {
logger.error("Validation errors", e)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.gtuk.diga.exceptions

class TestCodesDisabledException(message: String) : Exception(message)

0 comments on commit 7672265

Please sign in to comment.