Skip to content

Commit 0bca1f5

Browse files
ivolivol
authored andcommitted
docs: integrate quickstart content into README.md
1 parent e735d6d commit 0bca1f5

1 file changed

Lines changed: 99 additions & 3 deletions

File tree

README.md

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,104 @@ A wrapper for the [Approov SDK](https://github.com/approov/approov-android-sdk)
44

55
See [Java](https://github.com/approov/quickstart-android-java-retrofit) and [Kotlin](https://github.com/approov/quickstart-android-kotlin-retrofit) quickstarts for instructions on how to use this.
66

7+
## Adding Approov Service Dependency
8+
The Approov integration is available via [`maven`](https://mvnrepository.com/repos/central). This allows inclusion into the project by simply specifying a dependency in the `gradle` files for the app.
9+
The `Maven` repository is already present in the gradle.build file so the only import you need to make is the actual service layer itself:
10+
11+
```
12+
implementation("io.approov:service.retrofit:3.5.7")
13+
```
14+
15+
Make sure you do a Gradle sync (by selecting `Sync Now` in the banner at the top of the modified `.gradle` file) after making these changes.
16+
17+
This package is actually an open source wrapper layer that allows you to easily use Approov with `Retrofit`. This has a further dependency to the closed source [Approov SDK](https://mvnrepository.com/artifact/io.approov/approov-android-sdk). In some cases you may need to also add this implementation to your dependencies list to avoid build errors:
18+
19+
```
20+
implementation("io.approov:approov-android-sdk:3.5.3")
21+
```
22+
23+
## Manifest Changes
24+
The following app permissions need to be available in the manifest to use Approov:
25+
26+
```xml
27+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
28+
<uses-permission android:name="android.permission.INTERNET" />
29+
```
30+
31+
Note that the minimum SDK version you can use with the Approov package is 23 (Android 6.0).
32+
33+
Please [read this](https://approov.io/docs/latest/approov-usage-documentation/#targeting-android-11-and-above) section of the reference documentation if targeting Android 11 (API level 30) or above.
34+
35+
## Initializing Approov Service
36+
In order to use the `ApproovService` you must initialize it when your app is created, usually in the `onCreate` method:
37+
38+
```kotlin
39+
import io.approov.service.retrofit.ApproovService
40+
41+
class YourApp: Application() {
42+
override fun onCreate() {
43+
super.onCreate()
44+
ApproovService.initialize(applicationContext, "<enter-your-config-string-here>")
45+
}
46+
}
47+
```
48+
49+
The `<enter-your-config-string-here>` is a custom string that configures your Approov account access. This will have been provided in your Approov onboarding email.
50+
51+
## Using Approov Service
52+
You can then modify your code that obtains a `RetrofitInstance` to make API calls as follows:
53+
54+
```kotlin
55+
object ClientInstance {
56+
private const val BASE_URL = "https://your.domain"
57+
private var retrofitBuilder: Retrofit.Builder? = null
58+
val retrofitInstance: Retrofit
59+
get() {
60+
if (retrofitBuilder == null) {
61+
retrofitBuilder = Retrofit.Builder()
62+
.baseUrl(BASE_URL)
63+
.addConverterFactory(GsonConverterFactory.create())
64+
}
65+
return ApproovService.getRetrofit(retrofitBuilder!!)
66+
}
67+
}
68+
```
69+
70+
This obtains a retrofit instance includes an `OkHttp` interceptor that protects channel integrity (with either pinning or managed trust roots). The interceptor may also add `Approov-Token` or substitute app secret values, depending upon your integration choices. You should thus use this client for all API calls you may wish to protect.
71+
72+
Approov errors will generate an `ApproovException`, which is a type of `IOException`. This may be further specialized into an `ApproovNetworkException`, indicating an issue with networking that should provide an option for a user initiated retry (which must make the new request with a call to the `getRetrofit` to get the latest client).
73+
74+
## Custom OkHttp Builder
75+
By default, the Retrofit instance gets a default client constructed with a default `OkHttpClient`. However, your existing code may use a customized `OkHttpClient` with, for instance, different timeouts or other interceptors. For example, if you have existing code:
76+
77+
```kotlin
78+
val client = OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS).build()
79+
val retrofit = retrofit2.Retrofit.Builder().baseUrl("https://your.domain/").client(client).build()
80+
```
81+
Pass the modified `OkHttp.Builder` to the `ApproovService` as follows:
82+
83+
```kotlin
84+
ApproovService.setOkHttpClientBuilder(OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS))
85+
val retrofitBuilder = retrofit2.Retrofit.Builder().baseUrl("https://your.domain/")
86+
val retrofit = ApproovService.getRetrofit(retrofitBuilder)
87+
```
88+
89+
This call to `setOkHttpClientBuilder` only needs to be made once. Subsequent calls to `ApproovService.getRetrofit()` will then always a `OkHttpClient` with the builder values included.
90+
91+
## Checking it Works
92+
Initially you won't have set which API domains to protect, so the interceptor will not add anything. It will have called Approov though and made contact with the Approov cloud service. You will see logging from Approov saying `UNKNOWN_URL`.
93+
94+
Your Approov onboarding email should contain a link allowing you to access [Live Metrics Graphs](https://approov.io/docs/latest/approov-usage-documentation/#metrics-graphs). After you've run your app with Approov integration you should be able to see the results in the live metrics within a minute or so. At this stage you could even release your app to get details of your app population and the attributes of the devices they are running upon.
95+
96+
## Next Steps
97+
To actually protect your APIs and/or secrets there are some further steps. Approov provides two different options for protection:
98+
99+
**API Protection** You should use this if you control the backend API(s) being protected and are able to modify them to ensure that a valid Approov token is being passed by the app. An [Approov Token](https://approov.io/docs/latest/approov-usage-documentation/#approov-tokens) is short lived crytographically signed JWT proving the authenticity of the call.
100+
101+
**Secrets Protection** This allows app secrets, including API keys for 3rd party services, to be protected so that they no longer need to be included in the released app code. These secrets are only made available to valid apps at runtime.
102+
103+
Note that it is possible to use both approaches side-by-side in the same app.
104+
7105
# Changelog
8106

9107
Please see the [CHANGELOG.md](CHANGELOG.md) for more information on the changes in each version.
@@ -18,9 +116,7 @@ Please see the [USAGE.md](USAGE.md) for more information on how to use this wrap
18116

19117
## Included 3rd party Source
20118

21-
To support message signing, this repo has adapted code released by two 3rd
22-
party developers. The LICENSE files have been copied from the repos into the
23-
associated directories listed below:
119+
To support message signing, this repo has adapted code released by two 3rd party developers. The LICENSE files have been copied from the repos into the associated directories listed below:
24120

25121
* `approov-service/src/main/java/io/approov/util/http/sfv`
26122
* Repo: https://github.com/reschke/structured-fields

0 commit comments

Comments
 (0)