Skip to content

Commit f5b30d6

Browse files
committed
docs(TwoFactorAuth): Add 2FA doc for integration in a new app
1 parent b78433c commit f5b30d6

1 file changed

Lines changed: 129 additions & 0 deletions

File tree

TwoFactorAuth/README.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# 2 factor authentication
2+
3+
## How to integrate into a new app (Part One, polling-only)
4+
5+
This enables the feature, that tries to retrieve any ongoing login challenge when the app is brought to the foreground.
6+
It does NOT include notifications support, which is an extra (Part Two).
7+
8+
### 1. Add the dependencies
9+
10+
Depend on these 2FA related libraries:
11+
12+
```kotlin
13+
implementation(project(":Core:TwoFactorAuth:Front"))
14+
implementation(project(":Core:TwoFactorAuth:Back:WithUserDb"))
15+
```
16+
17+
### 2. Define the TwoFactorAuthManager singleton
18+
19+
#### A. With the user db dependency
20+
21+
Example:
22+
23+
```kotlin
24+
/**
25+
* Singleton for incoming 2FA (two factor authentication) challenges.
26+
*
27+
* Not a ViewModel because the state needs to be scoped for the entire app.
28+
*/
29+
val twoFactorAuthManager = TwoFactorAuthManager { userId -> AccountUtils.getHttpClient(userId) }
30+
```
31+
32+
#### B. With NO dependency on the user db
33+
34+
If you can't or don't want to depend on the user database dependency, you need to provide several more parameters.
35+
Here's an example.
36+
37+
```kotlin
38+
val twoFactorAuthManager = TwoFactorAuthManager(
39+
coroutineScope = coroutineScope,
40+
userIds = someStorage.connectedUsers.map(mutableSetOf()) { users ->
41+
users.map { it.id }
42+
}.distinctUntilChanged(),
43+
getAccountInfo = {
44+
val info = getUserAccountInfoById(it)
45+
ConnectionAttemptInfo.TargetAccount(
46+
avatarUrl = info.avatar,
47+
fullName = info.displayName,
48+
initials = info.computeInitials(),
49+
email = info.email,
50+
id = it.toLong(),
51+
)
52+
},
53+
getConnectedHttpClient = { userId -> getHttpClientForUser(userId) }
54+
)
55+
```
56+
57+
### 3. Add the overlay where needed
58+
59+
For each Activity in the app (including login screen for multi-account apps):
60+
61+
Add `TwoFactorAuthApprovalAutoManagedBottomSheet(twoFactorAuthManager)` at the root of the content.
62+
63+
Example:
64+
65+
```kotlin
66+
class MainActivity : ComponentActivity() {
67+
override fun onCreate(savedInstanceState: Bundle?) {
68+
super.onCreate(savedInstanceState)
69+
setContent {
70+
TwoFactorAuthApprovalAutoManagedBottomSheet(twoFactorAuthManager) // References the singleton declare just before.
71+
Whatever()
72+
}
73+
}
74+
}
75+
```
76+
77+
If the Activity is NOT using Compose, or if you don't know (e.g. in a `BaseActivity` class), the `addComposableOverlay`
78+
function was made just for that, just make sure it's called after `setContentView` or `setContent`.
79+
80+
Example:
81+
82+
```kotlin
83+
class MainActivity : ComponentActivity() {
84+
override fun onCreate(savedInstanceState: Bundle?) {
85+
super.onCreate(savedInstanceState)
86+
setContentView(whatever)
87+
addComposableOverlay { TwoFactorAuthApprovalAutoManagedBottomSheet(twoFactorAuthManager) }
88+
}
89+
}
90+
```
91+
92+
At this point, you can expect the feature to be working (albeit without notifications).
93+
94+
## How to integrate into a new app (Part Two, notifications support)
95+
96+
**⚠️⚠️ NOTE: ⚠️⚠️**
97+
98+
Unless specified otherwise, the **code below is to be put into Google Play Services dedicated source sets**.
99+
100+
For example, if the host app is published to F-Droid + the Google Play Store and has 2 product flavors named "fdroid" and "standard", where "standard" contains Google Play Services dependent code, by default, the code below should be located into `app-module/src/standard/kotlin`, where `app-module` is the target app or library module.
101+
102+
### 1. Add the right dependency in the right configuration
103+
104+
Add this dependency in the host app's Gradle build file:
105+
106+
```kotlin
107+
"standardImplementation"(project(":Core:Notifications:Registration"))
108+
```
109+
110+
Here, "standard" matches the product flavor that includes Firebase/Google Play Services dependencies.
111+
112+
### 2. Ensure notification token and topics will be synced
113+
114+
First, create the `RegisterUserDeviceWorker` class (if it doesn't exist already). It needs to subclass `AbstractNotificationsRegistrationWorker`.
115+
116+
You can find the Mail app example here: https://github.com/Infomaniak/android-kMail/blob/e272d5b5ff5f4b1eb1f11784537a3d624c063e80/app/src/standard/java/com/infomaniak/mail/firebase/RegisterUserDeviceWorker.kt
117+
118+
Follow other changes from this PR in the Mail, or the kDrive app:
119+
https://github.com/Infomaniak/android-kDrive/pull/1872/changes
120+
https://github.com/Infomaniak/android-kMail/pull/2698/changes
121+
122+
Here's the list of changes you need to add from the example PRs above:
123+
- Ensure `TwoFactorAuthNotifications.channel()` is created/submitted to Android's NotificationManager.
124+
- Ensure `NotificationsRegistrationManager` is added in `userDataCleanableList` at the beginning of the app process for the Play Services app variant.
125+
- Ensure the user addition and removal functions to call `resetForUser` in elements registered `userDataCleanableList` (already done if added Cross-app login first).
126+
- Ensure `NotificationsRegistrationManager.scheduleWorkerOnUpdate` is called in an app process wide coroutine, right from the app process start.
127+
- Ensure the `Application` subclass is declared properly in the manifest for the Play Services dependent product flavor
128+
- In the `FirebaseMessagingService` subclass (create it and declare it in the manifest if needed), make sure the `onNewToken` and `onMessageReceived` functions are implemented properly to forward the new tokens to `NotificationsRegistrationManager`, and matching notifications (the ones with `TwoFactorAuthNotifications.TYPE` in key `"type"`), forwarded to `twoFactorAuthManager.onApprovalChallengePushed(…)`.
129+
- Ensure any other push notification topics are properly integrated for `RegisterUserDeviceWorker` and `NotificationsRegistrationManager.scheduleWorkerOnUpdate`.

0 commit comments

Comments
 (0)