107 lines
4.0 KiB
Kotlin
107 lines
4.0 KiB
Kotlin
package org.mosad.seil0.projectlaogai.controller
|
|
|
|
import android.content.Context
|
|
import android.util.Log
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.runBlocking
|
|
import kotlinx.coroutines.withContext
|
|
import org.jsoup.Jsoup
|
|
import org.mosad.seil0.projectlaogai.R
|
|
import org.mosad.seil0.projectlaogai.controller.preferences.EncryptedPreferences
|
|
import java.security.KeyStore
|
|
import java.security.cert.CertificateFactory
|
|
import java.security.cert.X509Certificate
|
|
import javax.net.ssl.SSLContext
|
|
import javax.net.ssl.SSLSocketFactory
|
|
import javax.net.ssl.TrustManagerFactory
|
|
|
|
class QISPOSParser {
|
|
|
|
private val className = this.javaClass.name
|
|
private val baseURL = "https://notenverwaltung.hs-offenburg.de"
|
|
private val loginPath = "/qispos/rds?state=user&type=1&category=auth.login&startpage=portal.vm&breadCrumbSource=portal"
|
|
private val rootPath = "/qispos/rds?state=change&type=1&moduleParameter=studyPOSMenu&nextdir=change&next=menu.vm&subdir=applications&xml=menu&purge=y&navigationPosition=functions%2CstudyPOSMenu&breadcrumb=studyPOSMenu&topitem=functions&subitem=studyPOSMenu"
|
|
|
|
fun test(context: Context) {
|
|
|
|
val credentials = EncryptedPreferences.readCredentials(context)
|
|
val username = credentials.first.substringBefore("@")
|
|
val password = credentials.second
|
|
|
|
runBlocking {
|
|
withContext(Dispatchers.IO) {
|
|
try {
|
|
println("querying qispos ...")
|
|
println("using $username with ********")
|
|
|
|
val socketFactory = createSSLSocketFactory(context)
|
|
|
|
// login, asdf = username, fdsa = password, wtf
|
|
val list = mapOf(
|
|
Pair("asdf", username),
|
|
Pair("fdsa", password),
|
|
Pair("submit", "Anmelden")
|
|
)
|
|
|
|
// login and get session cookies
|
|
val res = Jsoup.connect(baseURL + loginPath)
|
|
.sslSocketFactory(socketFactory)
|
|
.followRedirects(true)
|
|
.referrer("https://notenverwaltung.hs-offenburg.de/qispos/rds?state=user&type=0")
|
|
.data(list)
|
|
.postDataCharset("UTF-8")
|
|
.execute()
|
|
|
|
val loginCookies = res.cookies()
|
|
|
|
println("cookies: $loginCookies")
|
|
println("status is: ${res.statusMessage()}: ${res.statusCode()}")
|
|
println(res.body())
|
|
|
|
// grades url
|
|
val test = Jsoup.parse(res.body()).select("li.menueListStyle").select("a.auflistung").last()
|
|
println(test)
|
|
|
|
} catch (ex: Exception) {
|
|
Log.e(className, "could not load qispos", ex)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private fun createSSLSocketFactory(context: Context): SSLSocketFactory {
|
|
// Load CAs from an InputStream
|
|
// (could be from a resource or ByteArrayInputStream or ...)
|
|
val cf: CertificateFactory = CertificateFactory.getInstance("X.509")
|
|
val caInput = context.resources.openRawResource(R.raw.notenverwaltung_hs_offenburg_de)
|
|
val ca = caInput.use {
|
|
cf.generateCertificate(it) as X509Certificate
|
|
}
|
|
println("ca=" + ca.subjectDN)
|
|
|
|
// Create a KeyStore containing our trusted CAs
|
|
val keyStoreType = KeyStore.getDefaultType()
|
|
val keyStore = KeyStore.getInstance(keyStoreType).apply {
|
|
load(null, null)
|
|
setCertificateEntry("ca", ca)
|
|
}
|
|
|
|
// Create a TrustManager that trusts the CAs inputStream our KeyStore
|
|
val tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm()
|
|
val tmf = TrustManagerFactory.getInstance(tmfAlgorithm).apply {
|
|
init(keyStore)
|
|
}
|
|
|
|
// Create an SSLContext that uses our TrustManager
|
|
val sslContext = SSLContext.getInstance("TLS").apply {
|
|
init(null, tmf.trustManagers, null)
|
|
}
|
|
|
|
return sslContext.socketFactory
|
|
}
|
|
|
|
|
|
|
|
|
|
} |