Skip to content

Commit

Permalink
[Lab] Allow setting custom API key.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Oct 22, 2022
1 parent fd407b2 commit 0d4dee7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Config(db: AppDb) : BaseConfig(db) {

var apiAvailabilityCheck by config<Boolean>(true)
var apiInvalidCert by config<String?>(null)
var apiKeyCustom by config<String?>(null)
var appInstalledTime by config<Long>(0L)
var appRateSnackbarTime by config<Long>(0L)
var appVersion by config<Int>(BuildConfig.VERSION_CODE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import pl.szczodrzynski.edziennik.ext.bodyToString
import pl.szczodrzynski.edziennik.ext.currentTimeUnix
import pl.szczodrzynski.edziennik.ext.hmacSHA1
import pl.szczodrzynski.edziennik.ext.md5
import pl.szczodrzynski.edziennik.ext.takeValue

class SignatureInterceptor(val app: App) : Interceptor {
companion object {
private const val API_KEY = "szkolny_android_42a66f0842fc7da4e37c66732acf705a"
const val API_KEY = "szkolny_android_42a66f0842fc7da4e37c66732acf705a"
}

override fun intercept(chain: Interceptor.Chain): Response {
Expand All @@ -27,7 +28,7 @@ class SignatureInterceptor(val app: App) : Interceptor {

return chain.proceed(
request.newBuilder()
.header("X-ApiKey", API_KEY)
.header("X-ApiKey", app.config.apiKeyCustom?.takeValue() ?: API_KEY)
.header("X-AppVersion", BuildConfig.VERSION_CODE.toString())
.header("X-Timestamp", timestamp.toString())
.header("X-Signature", sign(timestamp, body, url))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@ fun pendingIntentFlag(): Int {
fun Int?.takeValue() = if (this == -1) null else this
fun Int?.takePositive() = if (this == -1 || this == 0) null else this

fun String?.takeValue() = if (this.isNullOrBlank()) null else this

fun Any?.ignore() = Unit
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
import androidx.sqlite.db.SimpleSQLiteQuery
import com.chuckerteam.chucker.api.Chucker
import com.chuckerteam.chucker.api.Chucker.SCREEN_HTTP
Expand All @@ -21,6 +22,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.config.Config
import pl.szczodrzynski.edziennik.data.api.szkolny.interceptor.SignatureInterceptor
import pl.szczodrzynski.edziennik.databinding.LabFragmentBinding
import pl.szczodrzynski.edziennik.ext.*
import pl.szczodrzynski.edziennik.ui.base.lazypager.LazyFragment
Expand Down Expand Up @@ -141,6 +143,16 @@ class LabPageFragment : LazyFragment(), CoroutineScope {
app.config.apiInvalidCert = null
}

b.apiKey.setText(app.config.apiKeyCustom ?: SignatureInterceptor.API_KEY)
b.apiKey.doAfterTextChanged {
it?.toString()?.let { key ->
if (key == SignatureInterceptor.API_KEY)
app.config.apiKeyCustom = null
else
app.config.apiKeyCustom = key.takeValue()?.trim()
}
}

b.rebuildConfig.onClick {
App.config = Config(App.db)
}
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/lab_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@
android:text="Reset API signature"
android:textAllCaps="false" />

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">

<pl.szczodrzynski.edziennik.utils.TextInputKeyboardEdit
android:id="@+id/apiKey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="API Key" />
</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/disableDebug"
android:layout_width="match_parent"
Expand Down

0 comments on commit 0d4dee7

Please sign in to comment.