@@ -3,12 +3,14 @@ package io.openfuture.api.config.filter
33import com.fasterxml.jackson.databind.ObjectMapper
44import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
55import io.openfuture.api.config.propety.AuthorizationProperties
6- import org.springframework.http.HttpStatus.UNAUTHORIZED
76import io.openfuture.api.domain.exception.ExceptionResponse
87import io.openfuture.api.domain.key.WalletApiCreateRequest
98import io.openfuture.api.domain.state.WalletApiStateRequest
9+ import io.openfuture.api.entity.application.Application
1010import io.openfuture.api.service.ApplicationService
1111import io.openfuture.api.util.*
12+ import org.springframework.http.HttpStatus.NOT_FOUND
13+ import org.springframework.http.HttpStatus.UNAUTHORIZED
1214import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
1315import org.springframework.security.core.authority.SimpleGrantedAuthority
1416import org.springframework.security.core.context.SecurityContextHolder
@@ -36,29 +38,54 @@ class PublicApiAuthorizationFilter(
3638 val accessKey = request.getHeader(" X-API-KEY" )
3739 val signature = request.getHeader(" X-API-SIGNATURE" )
3840
39- val requestWrapper = CustomHttpRequestWrapper (request)
40- val walletApiCreateRequest =
41- mapper.readValue(requestWrapper.bodyInStringFormat, WalletApiCreateRequest ::class .java)
42- val mapper = jacksonObjectMapper()
43- val str = mapper.writeValueAsString(walletApiCreateRequest)
44-
45- val application = applicationService.getByAccessKey(accessKey)
46-
47- if (! checkHash(accessKey, signature, walletApiCreateRequest.timestamp.toLong(), str)) {
48- val exceptionResponse = ExceptionResponse (UNAUTHORIZED .value(), " Signature mismatch or request timeout" )
49- response.status = exceptionResponse.status
50- response.writer.write(mapper.writeValueAsString(exceptionResponse))
51- return
41+ try {
42+ val application = applicationService.getByAccessKey(accessKey)
43+
44+ if (request.method == " POST" ) {
45+
46+ val requestWrapper = CustomHttpRequestWrapper (request)
47+ val walletApiCreateRequest =
48+ mapper.readValue(requestWrapper.bodyInStringFormat, WalletApiCreateRequest ::class .java)
49+ val mapper = jacksonObjectMapper()
50+ val str = mapper.writeValueAsString(walletApiCreateRequest)
51+
52+ if (! checkHash(application, signature, str, walletApiCreateRequest.timestamp.toLong())) {
53+ println (" Signature mismatch or request timeout" )
54+ val exceptionResponse =
55+ ExceptionResponse (UNAUTHORIZED .value(), " Signature mismatch or request timeout" )
56+ response.status = exceptionResponse.status
57+ response.writer.write(mapper.writeValueAsString(exceptionResponse))
58+ return
59+ }
60+
61+ val token = UsernamePasswordAuthenticationToken (
62+ application.user,
63+ null ,
64+ listOf (SimpleGrantedAuthority (" ROLE_APPLICATION" ))
65+ )
66+ SecurityContextHolder .getContext().authentication = token
67+
68+ chain.doFilter(requestWrapper, response)
69+ return
70+ } else {
71+ val token = UsernamePasswordAuthenticationToken (
72+ application.user,
73+ null ,
74+ listOf (SimpleGrantedAuthority (" ROLE_APPLICATION" ))
75+ )
76+ SecurityContextHolder .getContext().authentication = token
77+
78+ chain.doFilter(request, response)
79+ return
80+ }
81+
82+ } catch (exception: RuntimeException ) {
83+ println (" Exception thrown" )
84+ response.setContentType(" application/json" )
85+ response.setStatus(NOT_FOUND .value())
5286 }
5387
54- val token = UsernamePasswordAuthenticationToken (application.user, null , listOf (SimpleGrantedAuthority (" ROLE_APPLICATION" )))
55- SecurityContextHolder .getContext().authentication = token
56-
57- chain.doFilter(requestWrapper, response)
58- return
59- }
60-
61- else if (request.requestURI.startsWith(" /public" ) && request.getHeader(" OPEN-API-KEY" ) != null ) {
88+ } /* else if (request.requestURI.startsWith("/public") && request.getHeader("OPEN-API-KEY") != null) {
6289
6390 val accessKey = request.getHeader("OPEN-API-KEY")
6491 val signature = request.getHeader("OPEN-API-SIGNATURE")
@@ -71,19 +98,23 @@ class PublicApiAuthorizationFilter(
7198
7299 val application = applicationService.getByAccessKey(accessKey)
73100
74- if (! checkHash(accessKey , signature, walletApiStateRequest.timestamp.toLong(), str )) {
101+ if (!checkHash(application , signature, str, walletApiStateRequest.timestamp.toLong())) {
75102 val exceptionResponse = ExceptionResponse(UNAUTHORIZED.value(), "Signature mismatch or request timeout")
76103 response.status = exceptionResponse.status
77104 response.writer.write(mapper.writeValueAsString(exceptionResponse))
78105 return
79106 }
80107
81- val token = UsernamePasswordAuthenticationToken (application.user, null , listOf (SimpleGrantedAuthority (" ROLE_APPLICATION" )))
108+ val token = UsernamePasswordAuthenticationToken(
109+ application.user,
110+ null,
111+ listOf(SimpleGrantedAuthority("ROLE_APPLICATION"))
112+ )
82113 SecurityContextHolder.getContext().authentication = token
83114
84115 chain.doFilter(requestWrapper, response)
85116 return
86- }
117+ }*/
87118
88119 chain.doFilter(request, response)
89120 }
@@ -92,16 +123,18 @@ class PublicApiAuthorizationFilter(
92123 // Do nothing
93124 }
94125
95- private fun checkHash (accessKey : String , signature : String , timestamp : Long , str : String ): Boolean {
126+ private fun checkHash (application : Application , signature : String , str : String , timestamp : Long ): Boolean {
127+
96128 val diffMinutes = differenceEpochs(currentEpochs(), timestamp)
97129 val expirePeriod = properties.expireApi!!
98130
99- val application = applicationService.getByAccessKey(accessKey)
100-
101131 val hmacSha256 = application.let {
102132 KeyGeneratorUtils .calcHmacSha256(it.apiSecretKey, str)
103133 }
104-
134+ println (hmacSha256)
135+ println (signature)
136+ println (" HASH ${hmacSha256 != signature} " )
137+ println (" PERIOD ${diffMinutes > expirePeriod} " )
105138 if (hmacSha256 != signature || diffMinutes > expirePeriod) {
106139 return false
107140 }
0 commit comments