Skip to content

Commit 11a4586

Browse files
committed
Added endpoint for fetching wallet details for plugin
1 parent 848c348 commit 11a4586

File tree

14 files changed

+139
-64
lines changed

14 files changed

+139
-64
lines changed

src/main/kotlin/io/openfuture/api/component/state/DefaultStateApi.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import org.springframework.web.client.RestTemplate
1111
@Component
1212
class DefaultStateApi(private val stateRestTemplate: RestTemplate) : StateApi {
1313

14-
override fun createWallet(address: String, webHook: String, blockchain: Blockchain, applicationId: String): StateWalletDto {
14+
override fun createWallet(
15+
address: String,
16+
webHook: String,
17+
blockchain: Blockchain,
18+
applicationId: String
19+
): StateWalletDto {
1520
val request = CreateStateWalletRequest(address, webHook, blockchain.getValue(), applicationId)
1621
val response = stateRestTemplate.postForEntity("/wallets/single", request, StateWalletDto::class.java)
1722
return response.body!!
@@ -51,4 +56,9 @@ class DefaultStateApi(private val stateRestTemplate: RestTemplate) : StateApi {
5156
return stateRestTemplate.getForEntity(url, PaymentWidgetResponse::class.java).body!!
5257
}
5358

59+
override fun getOrderDetailsByApplication(applicationId: String): Array<StateOrderDetail> {
60+
val url = "/wallets/application/${applicationId}"
61+
return stateRestTemplate.getForEntity(url, Array<StateOrderDetail>::class.java).body!!
62+
}
63+
5464
}

src/main/kotlin/io/openfuture/api/component/state/StateApi.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ interface StateApi {
2323
fun getTransactionsByAddress(address: String): Array<TransactionDto>
2424

2525
fun getPaymentDetailByOrder(orderKey: String): PaymentWidgetResponse
26+
27+
fun getOrderDetailsByApplication(applicationId: String): Array<StateOrderDetail>
2628
}

src/main/kotlin/io/openfuture/api/config/filter/PublicApiAuthorizationFilter.kt

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package io.openfuture.api.config.filter
33
import com.fasterxml.jackson.databind.ObjectMapper
44
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
55
import io.openfuture.api.config.propety.AuthorizationProperties
6-
import org.springframework.http.HttpStatus.UNAUTHORIZED
76
import io.openfuture.api.domain.exception.ExceptionResponse
87
import io.openfuture.api.domain.key.WalletApiCreateRequest
98
import io.openfuture.api.domain.state.WalletApiStateRequest
9+
import io.openfuture.api.entity.application.Application
1010
import io.openfuture.api.service.ApplicationService
1111
import io.openfuture.api.util.*
12+
import org.springframework.http.HttpStatus.NOT_FOUND
13+
import org.springframework.http.HttpStatus.UNAUTHORIZED
1214
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
1315
import org.springframework.security.core.authority.SimpleGrantedAuthority
1416
import org.springframework.security.core.context.SecurityContextHolder
@@ -36,44 +38,54 @@ class PublicApiAuthorizationFilter(
3638
val accessKey = request.getHeader("X-API-KEY")
3739
val signature = request.getHeader("X-API-SIGNATURE")
3840

39-
val application = applicationService.getByAccessKey(accessKey)
40-
41-
if (request.method == "POST") {
42-
43-
val requestWrapper = CustomHttpRequestWrapper(request)
44-
val walletApiCreateRequest =
45-
mapper.readValue(requestWrapper.bodyInStringFormat, WalletApiCreateRequest::class.java)
46-
val mapper = jacksonObjectMapper()
47-
val str = mapper.writeValueAsString(walletApiCreateRequest)
48-
49-
if (!checkHash(accessKey, signature, walletApiCreateRequest.timestamp.toLong(), str)) {
50-
val exceptionResponse =
51-
ExceptionResponse(UNAUTHORIZED.value(), "Signature mismatch or request timeout")
52-
response.status = exceptionResponse.status
53-
response.writer.write(mapper.writeValueAsString(exceptionResponse))
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)
5479
return
5580
}
5681

57-
val token = UsernamePasswordAuthenticationToken(
58-
application.user,
59-
null,
60-
listOf(SimpleGrantedAuthority("ROLE_APPLICATION"))
61-
)
62-
SecurityContextHolder.getContext().authentication = token
63-
64-
chain.doFilter(requestWrapper, response)
65-
return
82+
} catch (exception: RuntimeException) {
83+
println("Exception thrown")
84+
response.setContentType("application/json")
85+
response.setStatus(NOT_FOUND.value())
6686
}
67-
else {
68-
val token = UsernamePasswordAuthenticationToken(application.user, null, listOf(SimpleGrantedAuthority("ROLE_APPLICATION")))
69-
SecurityContextHolder.getContext().authentication = token
7087

71-
chain.doFilter(request, response)
72-
return
73-
}
74-
}
75-
76-
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) {
7789
7890
val accessKey = request.getHeader("OPEN-API-KEY")
7991
val signature = request.getHeader("OPEN-API-SIGNATURE")
@@ -86,19 +98,23 @@ class PublicApiAuthorizationFilter(
8698
8799
val application = applicationService.getByAccessKey(accessKey)
88100
89-
if (!checkHash(accessKey, signature, walletApiStateRequest.timestamp.toLong(), str)) {
101+
if (!checkHash(application, signature, str, walletApiStateRequest.timestamp.toLong())) {
90102
val exceptionResponse = ExceptionResponse(UNAUTHORIZED.value(), "Signature mismatch or request timeout")
91103
response.status = exceptionResponse.status
92104
response.writer.write(mapper.writeValueAsString(exceptionResponse))
93105
return
94106
}
95107
96-
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+
)
97113
SecurityContextHolder.getContext().authentication = token
98114
99115
chain.doFilter(requestWrapper, response)
100116
return
101-
}
117+
}*/
102118

103119
chain.doFilter(request, response)
104120
}
@@ -107,16 +123,18 @@ class PublicApiAuthorizationFilter(
107123
// Do nothing
108124
}
109125

110-
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+
111128
val diffMinutes = differenceEpochs(currentEpochs(), timestamp)
112129
val expirePeriod = properties.expireApi!!
113130

114-
val application = applicationService.getByAccessKey(accessKey)
115-
116131
val hmacSha256 = application.let {
117132
KeyGeneratorUtils.calcHmacSha256(it.apiSecretKey, str)
118133
}
119-
134+
println(hmacSha256)
135+
println(signature)
136+
println("HASH ${hmacSha256 != signature}")
137+
println("PERIOD ${diffMinutes > expirePeriod}")
120138
if (hmacSha256 != signature || diffMinutes > expirePeriod) {
121139
return false
122140
}

src/main/kotlin/io/openfuture/api/controller/api/PublicWalletApiController.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,29 @@ package io.openfuture.api.controller.api
33
import io.openfuture.api.domain.key.KeyWalletDto
44
import io.openfuture.api.domain.key.KeyWalletEncryptedDto
55
import io.openfuture.api.domain.key.WalletApiCreateRequest
6+
import io.openfuture.api.domain.state.StateOrderDetail
67
import io.openfuture.api.domain.state.WalletApiStateRequest
78
import io.openfuture.api.domain.state.WalletApiStateResponse
89
import io.openfuture.api.service.ApplicationService
910
import io.openfuture.api.service.ApplicationWalletService
1011
import io.openfuture.api.service.WalletApiService
12+
import org.springframework.security.access.prepost.PreAuthorize
1113
import org.springframework.web.bind.annotation.*
1214
import org.web3j.protocol.core.methods.response.TransactionReceipt
1315

1416

1517
@RestController
1618
@RequestMapping("/public/api/v1/wallet")
19+
@PreAuthorize(value = "hasAnyRole('ROLE_APPLICATION')")
1720
class PublicWalletApiController(
1821
private val walletApiService: WalletApiService,
1922
private val applicationService: ApplicationService,
2023
private val applicationWalletService: ApplicationWalletService
2124
) {
2225

23-
//@PreAuthorize(value = "hasAnyRole('ROLE_APPLICATION')")
2426
@PostMapping("/process")
2527
fun generateWallet(@RequestBody walletApiCreateRequest: WalletApiCreateRequest, @RequestHeader("X-API-KEY") accessKey: String): Array<KeyWalletDto> {
28+
println("ACCESS KEY: $accessKey")
2629
val application = applicationService.getByAccessKey(accessKey)
2730
return walletApiService.processWalletSDK(walletApiCreateRequest, application, application.user)
2831
}
@@ -33,6 +36,12 @@ class PublicWalletApiController(
3336
return applicationWalletService.getAllWallets(application.id)
3437
}
3538

39+
@GetMapping("/details")
40+
fun getWalletDetails(@RequestHeader("X-API-KEY") accessKey: String): Array<StateOrderDetail> {
41+
val application = applicationService.getByAccessKey(accessKey)
42+
return walletApiService.getOrderDetails(application.id.toString())
43+
}
44+
3645
@PostMapping("/save")
3746
fun saveWallet(@RequestBody walletApiStateRequest: WalletApiStateRequest, @RequestHeader("OPEN-API-KEY") accessKey: String): Boolean {
3847
val application = applicationService.getByAccessKey(accessKey)

src/main/kotlin/io/openfuture/api/controller/base/ExceptionRestControllerAdvice.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import javax.validation.ConstraintViolationException
1616
@RestControllerAdvice
1717
class ExceptionRestControllerAdvice {
1818

19+
@ResponseStatus(code = NOT_FOUND)
20+
@ExceptionHandler(NotFoundException::class)
21+
fun notFoundExceptionHandler(exception: NotFoundException): ExceptionResponse =
22+
ExceptionResponse(NOT_FOUND.value(), exception.message!!)
23+
1924
@ResponseStatus(code = BAD_REQUEST)
2025
@ExceptionHandler(MethodArgumentNotValidException::class)
2126
fun methodArgumentNotValidExceptionHandler(exception: MethodArgumentNotValidException): ExceptionResponse {
@@ -75,11 +80,6 @@ class ExceptionRestControllerAdvice {
7580
ExceptionResponse(BAD_REQUEST.value(), exception.message ?: """Something went wrong. Please read the
7681
|documentation https://docs.openfuture.io/ or contact us openplatform@zensoft.io""".trimMargin())
7782

78-
@ResponseStatus(code = NOT_FOUND)
79-
@ExceptionHandler(NotFoundException::class)
80-
fun notFoundExceptionHandler(exception: NotFoundException): ExceptionResponse =
81-
ExceptionResponse(NOT_FOUND.value(), exception.message!!)
82-
8383
@ResponseStatus(code = BAD_REQUEST)
8484
@ExceptionHandler(RuntimeException::class)
8585
fun runtimeExceptionHandler(exception: RuntimeException): ExceptionResponse =

src/main/kotlin/io/openfuture/api/controller/widget/PaymentAddressWidgetController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.*
88
@RestController
99
@RequestMapping("/widget/payment/addresses")
1010
class PaymentAddressWidgetController(
11-
private val service : WalletApiService
11+
private val service: WalletApiService
1212
) {
1313
@GetMapping("/order/{orderKey}")
1414
fun getAllAddressesByOrder(@PathVariable orderKey: String): PaymentWidgetResponse {

src/main/kotlin/io/openfuture/api/controller/widget/TransactionWidgetController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.*
88
@RestController
99
@RequestMapping("/widget/transactions")
1010
class TransactionWidgetController(
11-
private val service : ApplicationWalletService
11+
private val service: ApplicationWalletService
1212
) {
1313

1414
@GetMapping("/address/{address}")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.openfuture.api.domain.state
2+
3+
import java.math.BigDecimal
4+
5+
data class StateOrderDetail(
6+
var orderKey: String,
7+
var amount: BigDecimal,
8+
var totalPaid: BigDecimal = BigDecimal.ZERO,
9+
var currency: String,
10+
val blockchains: List<BlockchainWallets>
11+
)
12+
13+
data class BlockchainWallets(
14+
val address: String,
15+
val blockchain: String,
16+
val rate: BigDecimal
17+
)
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.openfuture.api.domain.state
22

33
data class StateSignRequest(
4-
val address: String,
5-
val order_id: Int,
4+
val order_key: String,
65
val status: String
76
)

src/main/kotlin/io/openfuture/api/domain/state/WalletMetaData.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ data class WalletMetaData(
55
var orderKey: String,
66
var productCurrency: String,
77
var source: String,
8-
var test: Boolean,
9-
var clientManaged: Boolean
8+
var test: Boolean
109
)

0 commit comments

Comments
 (0)