Use wulkanowy.signer

This commit is contained in:
Mikołaj Pich 2018-10-14 00:57:02 +02:00
parent e084982ac2
commit 85b1e8ad5a
No known key found for this signature in database
GPG key ID: F62B26E36D4C4BAA
2 changed files with 5 additions and 23 deletions

View file

@ -1,6 +1,6 @@
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.2.61'
id 'org.jetbrains.kotlin.jvm' version '1.2.71'
id 'jacoco'
}
@ -23,6 +23,8 @@ ext {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation 'com.github.wulkanowy:uonet-request-signer:d2f351a70e'
implementation 'com.google.code.gson:gson:2.8.5'
implementation "com.squareup.retrofit2:retrofit:$retrofit"
implementation "com.squareup.retrofit2:converter-gson:$retrofit"

View file

@ -1,15 +1,9 @@
package io.github.wulkanowy.sdk.interceptor
import io.github.wulkanowy.sdk.USER_AGENT
import io.github.wulkanowy.signer.signContent
import okhttp3.Interceptor
import okhttp3.Response
import okio.Buffer
import java.io.ByteArrayInputStream
import java.io.InputStream
import java.security.KeyStore
import java.security.PrivateKey
import java.security.Signature
import java.util.*
class SignInterceptor(
private val password: String,
@ -24,24 +18,10 @@ class SignInterceptor(
request.header("User-Agent", USER_AGENT)
if (signature.isNotBlank() && certificate.isNotBlank()) {
val buffer = Buffer()
original.body()?.writeTo(buffer)
request.header("RequestCertificateKey", certificate)
request.header("RequestSignatureValue", signContent(password,
buffer.readByteArray(),
ByteArrayInputStream(Base64.getDecoder().decode(signature)))
)
request.header("RequestSignatureValue", signContent(password, signature, original.body().toString()))
}
return chain.proceed(request.method(original.method(), original.body()).build())
}
private fun signContent(password: String, contents: ByteArray, cert: InputStream): String {
return Base64.getEncoder().encodeToString(Signature.getInstance("SHA1withRSA").apply {
initSign(KeyStore.getInstance("pkcs12").apply {
load(cert, password.toCharArray())
}.getKey("LoginCert", password.toCharArray()) as PrivateKey)
update(contents)
}.sign())
}
}