Change to private key content signing

This commit is contained in:
Mikołaj Pich 2019-11-01 23:42:33 +01:00
parent 296908b46e
commit 570fa4d5f6
No known key found for this signature in database
GPG key ID: F62B26E36D4C4BAA
8 changed files with 27 additions and 18 deletions

1
.gitignore vendored
View file

@ -41,3 +41,4 @@ gradle-app.setting
# gradle/wrapper/gradle-wrapper.properties
*iml
classes

View file

@ -24,7 +24,7 @@ ext {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "io.github.wulkanowy:api:0.12.0"
implementation "io.github.wulkanowy:api:02e3190"
implementation "io.github.wulkanowy:uonet-request-signer:6d7aebf0e9"
compileOnly "org.threeten:threetenbp:$threetenbp:no-tzdb"

View file

@ -32,6 +32,7 @@ import io.github.wulkanowy.sdk.school.mapTeachers
import io.github.wulkanowy.sdk.student.mapStudent
import io.github.wulkanowy.sdk.timetable.mapCompletedLessons
import io.github.wulkanowy.sdk.timetable.mapTimetable
import io.github.wulkanowy.signer.getPrivateKeyFromCert
import io.reactivex.Completable
import io.reactivex.Maybe
import io.reactivex.Observable
@ -86,6 +87,12 @@ class Sdk {
resettableManager.reset()
}
var privateKey = ""
set(value) {
field = value
resettableManager.reset()
}
var ssl = true
set(value) {
field = value
@ -171,7 +178,7 @@ class Sdk {
private val resettableManager = resettableManager()
private val serviceManager by resettableLazy(resettableManager) {
RepositoryManager(logLevel, apiKey, certificate, certKey, interceptors, apiBaseUrl, schoolSymbol)
RepositoryManager(logLevel, privateKey, certKey, interceptors, apiBaseUrl, schoolSymbol)
}
private val routes by resettableLazy(resettableManager) {
@ -258,8 +265,8 @@ class Sdk {
}.flatMap { certificateResponse ->
if (certificateResponse.isError) throw RuntimeException(certificateResponse.message)
this@Sdk.certKey = certificateResponse.tokenCert!!.certificateKey
this@Sdk.certificate = certificateResponse.tokenCert.certificatePfx
getRegisterRepo(apiBaseUrl, this@Sdk.symbol).getPupils().map { it.mapStudents(symbol, certificateResponse) }
this@Sdk.privateKey = getPrivateKeyFromCert(apiKey, certificateResponse.tokenCert.certificatePfx)
getRegisterRepo(apiBaseUrl, this@Sdk.symbol).getPupils().map { it.mapStudents(symbol, certificateResponse, this@Sdk.privateKey) }
}
}

View file

@ -7,8 +7,7 @@ import okio.Buffer
import java.nio.charset.Charset
class SignInterceptor(
private val password: String,
private val certificate: String,
private val privateKey: String,
private val certKey: String
) : Interceptor {
@ -18,11 +17,11 @@ class SignInterceptor(
request.header("User-Agent", "MobileUserAgent")
if (certificate.isNotBlank() || certKey.isNotBlank()) {
if (privateKey.isNotBlank()) {
val signature = Buffer().run {
original.body()?.writeTo(this)
signContent(password, certificate, readString(Charset.defaultCharset()))
signContent(privateKey, readString(Charset.defaultCharset()))
}
request.header("RequestCertificateKey", certKey)

View file

@ -18,5 +18,5 @@ data class Student(
val scrapperHost: String,
val ssl: Boolean,
val certificateKey: String,
val certificate: String
val privateKey: String
)

View file

@ -5,7 +5,7 @@ import io.github.wulkanowy.sdk.pojo.Student
import io.github.wulkanowy.api.register.Student as ScrapperStudent
import io.github.wulkanowy.sdk.register.Student as ApiStudent
fun List<ApiStudent>.mapStudents(symbol: String, certificateResponse: CertificateResponse): List<Student> {
fun List<ApiStudent>.mapStudents(symbol: String, certificateResponse: CertificateResponse, privateKey: String): List<Student> {
return map {
Student(
email = it.userLogin,
@ -22,7 +22,7 @@ fun List<ApiStudent>.mapStudents(symbol: String, certificateResponse: Certificat
apiHost = certificateResponse.tokenCert!!.apiEndpoint.removeSuffix("/"),
scrapperHost = "",
ssl = certificateResponse.tokenCert.apiEndpoint.startsWith("https"),
certificate = certificateResponse.tokenCert.certificatePfx,
privateKey = privateKey,
certificateKey = certificateResponse.tokenCert.certificateKey
)
}
@ -46,7 +46,7 @@ fun List<ScrapperStudent>.mapStudents(ssl: Boolean, scrapperHost: String): List<
apiHost = "",
scrapperHost = scrapperHost,
certificateKey = "",
certificate = ""
privateKey = ""
)
}
}

View file

@ -12,8 +12,7 @@ import retrofit2.create
class RepositoryManager(
private val logLevel: HttpLoggingInterceptor.Level,
private val apiKey: String,
private val certificate: String,
private val privateKey: String,
private val certKey: String,
private val interceptors: MutableList<Pair<Interceptor, Boolean>>,
private val apiBaseUrl: String,
@ -39,7 +38,7 @@ class RepositoryManager(
.addConverterFactory(GsonConverterFactory.create())
.client(OkHttpClient().newBuilder()
.addInterceptor(HttpLoggingInterceptor().setLevel(logLevel))
.addInterceptor(SignInterceptor(apiKey, certificate, certKey))
.addInterceptor(SignInterceptor(privateKey, certKey))
.apply {
interceptors.forEach {
if (it.second) addNetworkInterceptor(it.first)

View file

@ -12,6 +12,7 @@ import io.github.wulkanowy.sdk.register.Student
import io.github.wulkanowy.sdk.repository.MobileRepository
import io.github.wulkanowy.sdk.repository.RegisterRepository
import io.github.wulkanowy.sdk.timetable.Lesson
import io.github.wulkanowy.signer.getPrivateKeyFromCert
import io.reactivex.observers.TestObserver
import junit.framework.TestCase.assertEquals
import okhttp3.OkHttpClient
@ -40,14 +41,14 @@ class UonetTest {
private lateinit var student: Student
private fun getRetrofitBuilder(certificate: String, certKey: String): Retrofit.Builder {
private fun getRetrofitBuilder(privateKey: String, certKey: String): Retrofit.Builder {
return Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(OkHttpClient().newBuilder()
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC))
.addInterceptor(SignInterceptor(PASSWORD, certificate, certKey))
.addInterceptor(SignInterceptor(privateKey, certKey))
.build()
)
}
@ -74,6 +75,8 @@ class UonetTest {
val certKey = tokenCrt!!.certificateKey
val cert = tokenCrt.certificatePfx
val privateKey = getPrivateKeyFromCert(PASSWORD, cert)
val pupils = register.getPupils()
val pupilSubscriber = TestObserver<List<Student>>()
pupils.subscribe(pupilSubscriber)
@ -84,7 +87,7 @@ class UonetTest {
student = pupilSubscriber.values()[0][0]
// MobileRepository
mobile = MobileRepository(getRetrofitBuilder(cert, certKey)
mobile = MobileRepository(getRetrofitBuilder(privateKey, certKey)
.baseUrl("$HOST/Default/${student.reportingUnitSymbol}/mobile-api/Uczen.v3.Uczen/")
.build().create()
)