Skip to content

Instantly share code, notes, and snippets.

/**
* Intercept the given request chain to add the Approov token to an 'Approov-Token' header.
*
* @param chain the request chain to modify
* @return the modified response, authenticated by Approov
*/
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()
val approovToken = ApproovAttestation.shared().fetchApproovTokenAndWait(originalRequest.url().toString()).token
OkHttpClient.Builder()
.addInterceptor(ApproovRequestInterceptor())
.build()
// Verify and decode the Approov token and respond with 403 if the JWT
// could not be decoded, has expired, or has an invalid signature
const checkApproovTokenJWT = jwt({
secret: new Buffer(config.approovTokenSecret, 'base64'),
getToken: function fromApproovTokenHeader(req) {
// Retrieve the Approov token used to authenticate the mobile app from the request header
var approovToken = req.get('Approov-Token')
if (!approovToken) {
console.log('\tApproov token not specified or in the wrong format')
}
OkHttpClient.Builder()
.hostnameVerifier(ApproovHostnameVerifier(OkHttpClient().hostnameVerifier()))
.build()
OkHttpClient.Builder()
.hostnameVerifier(ApproovHostnameVerifier(OkHttpClient().hostnameVerifier()))
.build()
// The current demo stage
config.currentDemoStage = DEMO_STAGE.APPROOV_APP_AUTH_PROTECTION
// The Approov token secret
config.approovTokenSecret = 'PUT-YOUR-APPROOV-TOKEN-SECRET-HERE'
/** The current demo stage */
val currentDemoStage = DemoStage.APPROOV_APP_AUTH_PROTECTION
// The current demo stage
var currentDemoStage = DEMO_STAGE.HMAC_DYNAMIC_SECRET_PROTECTION
// The current demo stage
var currentDemoStage = DEMO_STAGE.HMAC_STATIC_SECRET_PROTECTION
val secret = HMAC_SECRET
var keySpec: SecretKeySpec
...
...
val obfuscatedSecretData = Base64.decode(secret, Base64.DEFAULT)
val shipFastAPIKeyData = loadShipFastAPIKey(context).toByteArray(Charsets.UTF_8)
for (i in 0 until minOf(obfuscatedSecretData.size, shipFastAPIKeyData.size)) {
obfuscatedSecretData[i] = (obfuscatedSecretData[i].toInt() xor shipFastAPIKeyData[i].toInt()).toByte()
}
val obfuscatedSecret = Base64.encode(obfuscatedSecretData, Base64.DEFAULT)