Skip to content

Instantly share code, notes, and snippets.

GET /secured/data HTTP/1.1
Host: damian.wtf
Accept: application/json
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Authentication: Token myrandomauthtoken
Pragma: no-cache
Cache-Control: no-cache
...
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(tokenInterceptor)
.build()
...
class LoginPresenter: LoginContract.Presenter<LoginContract.View> {
...
val tokenInterceptor: TokenInterceptor // must be the same instance (injected via dependency injection, or something else)
override fun login(email: String, password:String) {
authApi.login(email, password)
.subscribe({ result ->
if(result.isSuccessful) {
// retrieve the user-token and push it into the interceptor
tokenInterceptor.userToken = result.userToken
class TokenInterceptor: AuthenticationInterceptor(AUTHENTICATION_HEADER) {
var userToken = ""
var appToken = ""
override fun getTokenByIdentifier(identifier: String?): String = when(identifier) {
KEY_USER_TOKEN -> userToken
KEY_APP_TOKEN -> appToken
else -> ""
}
GET /secured/data HTTP/1.1
...
Authentication: User Token
...
GET /secured/data HTTP/1.1
...
Authentication: Token my_super_secret_user_token
...
class TokenInterceptor: AuthenticationInterceptor(AUTHENTICATION_HEADER) {
// ...
companion object {
const val AUTHENTICATION_HEADER = "Authentication"
const val KEY_USER_TOKEN = "User Token"
const val KEY_APP_TOKEN = "App Token"
const val AUTHENTICATION_USER = "$AUTHENTICATION_HEADER: $KEY_USER_TOKEN"
const val AUTHENTICATION_APP = "$AUTHENTICATION_HEADER: $KEY_APP_TOKEN"
interface SampleApi {
@GET("fact")
@Headers(TokenInterceptor.AUTHENTICATION_APP)
fun getAppScopeCatFact(): Call<CatFact>
@GET("fact")
@Headers(TokenInterceptor.AUTHENTICATION_USER)
fun getUserScopeCatFact(): Call<CatFact>
}
@damian-burke
damian-burke / dangerfile.js
Last active September 13, 2020 20:42
DangerJS CHANGELOG modification check
// Add a CHANGELOG entry for app changes
const hasChangelog = danger.git.modified_files.includes("CHANGELOG.yml")
if (!hasChangelog) {
fail("Please add a changelog entry for your changes. :crystall_ball:")
}
@damian-burke
damian-burke / danger.yml
Created September 13, 2020 20:57
DangerJS on GitHub Actions
name: DangerJS
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps: