README.md 15.9 KB
Newer Older
Jonas Passerini's avatar
Jonas Passerini committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
## Summary

This is the Android SDK of Futurae. You can read more about Futurae™ at [futurae.com].

## Table of contents

* [Basic integration](#basic-integration)
   * [Get FuturaeKit SDK for Android](#get-futuraekit-sdk-for-android)
   * [Add SDK to Project](#add-sdk-to-project)
   * [Add permissions](#add-permissions)
   * [Proguard settings](#proguard-settings)
   * [Basic setup](#basic-setup)
   * [Build your app](#build-your-app)
* [Features](#features)
   * [Callbacks](#callbacks)
   * [URI Schemes](#uri-schemes)
   * [Push Notifications](#push-notifications)
      * [GCM Token Registration](#gcm-token-registration)
      * [GCM Listener Service](#gcm-listener-service)
			* [Local Intents](#local-intents)
   * [Enroll User](#enroll-user)
   * [Logout User](#logout-user)
   * [Authenticate User](#authenticate-user)
      * [QR Code Factor](#qr-code-factor)
      * [Push Notification Factor](#push-notification-factor)
         * [Approve Authentication](#approve-authentication)
         * [Reject Authentication](#reject-authentication)
      * [TOTP Factor](#totp-factor)


## <a id="basic-integration" />Basic integration
We will describe the steps to integrate the FuturaeKit SDK into your Android project. We are going to assume that you are using Android Studio for your development.

### <a id="get-futuraekit-sdk-for-android" />Get FuturaeKit SDK for Android
Git clone this repository inside your project, in order to have full access to the FuturaeKit SDK project.

![][clone]

### <a id="add-sdk-to-project" />Add SDK to Project
If you are using Gradle, add the following to your `build.gradle` file:

```
compile project(':futuraekit-java')
```

also make sure to modify your `settings.gradle` file:
```
include ':futuraekit-java'
```

### <a id="add-permissions" />Add permissions
Please add the following permissions, which the FuturaeKit SDK needs, if they are not already present in your AndroidManifest.xml file:

```
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
```

![][permissions]

### <a id="proguard-settings" />Proguard settings
If you are using Proguard, add these lines to your Proguard file:

```
TBD
```

### <a id="basic-setup" />Basic setup
We recommend using an android [Application][android_application] class to initialize the SDK. If you already have one in your app already, follow these steps:

1. In your `Application` class find or create the `onCreate` method and add the following code to initialize the FuturaeKit SDK:

```java
import com.futurae.sdk.FuturaeClient;

public class AppMain extends Application {

    // overrides
    @Override
    public final void onCreate() {

        super.onCreate();

        FuturaeClient.launch(this);
    }
}
```

![][application_config]

2. In your `res/values` folder make sure to create a file `futurae.xml` with the following contents:

```xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

  <string name="ftr_api_key">{FuturaeApiKey}</string>
  <string name="ftr_push_gcm_sender_id">{GoogleApiProjectNumber}</string>

</resources>
```

![][application_config_xml]

**Note**: Initializing the FuturaeKit SDK like this is `very important`. Replace `{FuturaeApiKey}` with your Futurae API key, and `{GoogleApiProjectNumber}` with the project number of the Google API you have set up for the GCM (see the [GCM Token Registration](#gcm-token-registration) section for more information).

### <a id="build-your-app" />Build your app

Build and run your app. If the build succeeds, you should carefully read the SDK logs in the console.

## <a id="features" />Features
### <a id="callbacks" />Callbacks
The SDK methods that perform API calls use callbacks as the feedback mechanism. These calls expect an object of the `FuturaeCallback` interface as an argument:
```java
public interface FuturaeCallback {
    void success();
    void failure(Throwable throwable);
}
```

### <a id="uri-schemes" />URI Schemes
The SDK is able to handle URI scheme calls, for URIs that start with the `futurae://` prefix, which are used to either **enroll** or **authenticate** a user. To enable this behaviour in your app, you must add the following intent filter in your manifest, inside the activity that will handle the intent:
```xml
<intent-filter>
  <action android:name="android.intent.action.VIEW"/>

  <category android:name="android.intent.category.DEFAULT"/>
  <category android:name="android.intent.category.BROWSABLE"/>

  <data android:scheme="futurae"/>
</intent-filter>
```

Once the activity has been set up to handle the URI scheme call intents, get the intent data in the `onCreate()` method of your activity, which contains the URI that should be passed in the SDK, using the `handleUri()` method:
```java
FuturaeClient.sharedClient().handleUri(uriString, new FuturaeCallback() {
	@Override
	public void success() {

	}

	@Override
	public void failure(Throwable throwable) {

	}
});
```

### <a id="push-notifications" />Push Notifications
Your app must be set up to receive GCM push notifications from our server. You can choose to receive and handle these notifications yourself, or alternatively you can use the existing infrastructure provided in the SDK. You can find more information on how to setup GCM push notifications for your app in the [Google Cloud Messaging Developer Guide](https://developers.google.com/cloud-messaging/android/client).

In order to be able to receive GCM notifications, you need to specify the following components inside the application section of your Manifest:

The GCM Receiver:
```xml
<receiver
  android:name="com.google.android.gms.gcm.GcmReceiver"
  android:exported="true"
  android:permission="com.google.android.c2dm.permission.SEND">
  <intent-filter>
    <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
    <action android:name="com.google.android.c2dm.intent.RECEIVE"/>

    <category android:name="${applicationId}"/>
  </intent-filter>
</receiver>
```

The Instance ID listener service and Registration intent service (the ones provided in the SDK, or your own):
```xml
<service
  android:name="com.futurae.sdk.gcm.FTRInstanceIdListenerService"
  android:exported="false">
  <intent-filter>
    <action android:name="com.google.android.gms.iid.InstanceID"/>
  </intent-filter>
</service>

<service
  android:name="com.futurae.sdk.gcm.FTRRegistrationIntentService"
  android:exported="false"
  />
```

The GCM Listener service (the one provided in the SDK, or your own):
```xml
<service
  android:name="com.futurae.sdk.gcm.FTRGcmListenerService"
  android:exported="false">
  <intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
  </intent-filter>
</service>
```

#### <a id="gcm-token" />GCM Token Registration
The `FTRRegistrationIntentService` is responsible for registering the app's GCM token to the Futurae server. This is important for the server to be able to issue GCM notifications for your app. The provided service handles this, however if you need to, you can write your own or extend the existing one. The call that registers the GCM token to the Futurae server is `registerPushToken()`, and it is necessary every time the GCM token is generated or is changed by GCM.

For example, once the app receives a new GCM token (e.g. via an `InstanceIdListenerService`), the token needs to be obtained and registered to the Futurae server using the following code:
```java
FuturaeClient.sharedClient().registerPushToken(gcmToken, new FuturaeCallback() {
	@Override
	public void success() {

	}

	@Override
	public void failure(Throwable t) {

	}
});
```

#### <a id="gcm-listener" />GCM Listener Service
The `FTRGcmListenerService` receives GCM push notifications and handles them, according to the actions dictated by the Futurae server. You can use or extend the service provided by the SDK, or write your own. There are two distinct push notification types issued by the Futurae server: **Aprove** or **Unenroll**.

In case you want to process and handle the GCM notifications without using `FTRGcmListenerService`, you must use the following code in order to process and handle the notifications sent by the Futurae server, inside the implementation of your `GcmListenerService`:
```java
@Override
public void onMessageReceived(String from, Bundle data) {

	// Create and handle a Futurae notification
	FTRNotification notification = notificationFactory.createNotification(data);
	notification.handle();
}
```

#### <a id="local-intents" />Local Intents
Once a Futurae GCM notification has been handled, the SDK will notify the host app using **local broadcasts**. The app should register a broadcast receiver for these intents and react accordingly. There are three distinct Intents that the notification handlers might send in a local broadcast:
* `INTENT_GENERIC_NOTIFICATION_ERROR`: Indicates that an error was encountered during the processing or handling of a GCM notification.
* `INTENT_APPROVE_AUTH_MESSAGE`: Indicates that a Push Notification Authentication has been initiated.
* `INTENT_ACCOUNT_UNENROLL_MESSAGE`: Indicates that a user account has been logged out remotely.

The following example shows how to register for these intents:
```java
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Shared.INTENT_GENERIC_NOTIFICATION_ERROR);  // General notification error intent
intentFilter.addAction(Shared.INTENT_APPROVE_AUTH_MESSAGE);        // Approve Authentication notification intent
intentFilter.addAction(Shared.INTENT_ACCOUNT_UNENROLL_MESSAGE);    // Logout user notification intent

LocalBroadcastManager.getInstance(getContext()).registerReceiver(new BroadcastReceiver() {

	@Override
	public void onReceive(Context context, Intent intent) {

		switch(intent.getAction()) {
			case Shared.INTENT_ACCOUNT_UNENROLL_MESSAGE:
				// Handle unenroll notification (e.g. refresh lists)
				break;

			case Shared.INTENT_APPROVE_AUTH_MESSAGE:
				// Handle approve notification (e.g. show approve view)
				break;

			case Shared.INTENT_GENERIC_NOTIFICATION_ERROR:
				// Handle GCM notification error
				break;
		}
	}
}, intentFilter);
```

### <a id="enroll-user" />Enroll User

To enroll a user, you must call the `enroll()` method, using a valid code obtained by scanning an enrolment QR Code. For example, you can use a QR Code Reader Activity to scan a code and obtain the result:
```java
startActivityForResult(FTRQRCodeActivity.getIntent(this), FTRQRCodeActivity.RESULT_BARCODE);
```

If a QR Code is successfully scanned then `onActivityResult` will be called, with the scanned code:
```java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

	if (resultCode == RESULT_OK && requestCode == FTRQRCodeActivity.RESULT_BARCODE) {
		Barcode qrcode = data.getParcelableExtra(FTRQRCodeActivity.PARAM_BARCODE);

		FuturaeClient.sharedClient().enroll(qrcode.rawValue, new FuturaeCallback() {
			@Override
			public void success() {

			}

			@Override
			public void failure(Throwable throwable) {

			}
		});
	}

}
```

Please make sure to call enroll, inside the onActivityResult method to complete the user enrollment.

### <a id="logout-user" />Logout User
To remove a user account from the app and the SDK, call the `logout()` method:
```java
FuturaeClient.sharedClient().logout(userId, new FuturaeCallback() {
	@Override
	public void success() {

	}

	@Override
	public void failure(Throwable throwable) {

	}
});
```

Typically this should happen either when the user removes the account manually from the app, or when a user has been logged out remotely by the server. In the former case, calling the `logout()` method is enough, as it notifies the server of the logout and deletes the account from the SDK too. In the latter case, the server will send a GCM notification to the app, and the default handler of the notification will delete the account from the SDK as well. In this case, the notification handler will also send a local broadcast to the app (see `INTENT_ACCOUNT_UNENROLL_MESSAGE` [above](#local-intents), so that the app can also perform any further action required (e.g. refresh the list of active accounts in an account view).

The intent contains the User ID as an extra:
```java
String userId = intent.getStringExtra(FTRUnenrollNotification.PARAM_USER_ID);
```

### <a id="auth-user" />Authenticate User
To authenticate (or reject) a user session, depending on the authentication factor, you can use the following methods: **QR Code Factor**, or **Push Notification Factor**.

#### <a id="qrcode-auth" />QR Code Factor
To authenticate with the QR Code Factor, scan the QR Code provided by the server and pass its contents to the following method:
```java
FuturaeClient.sharedClient().approveAuth(qrCodeString, new FuturaeCallback() {
	@Override
	public void success() {

	}

	@Override
	public void failure(Throwable throwable) {

	}
});
```

#### <a id="approve-auth" />Push Notification Factor
When a Push Notification Factor session is initiated on the server side, the server will send a push notification to the app, where the user should approve or reject the authentication session. The notification is received and handled by the SDK, which in turn will send a local broadcast (see `INTENT_APPROVE_AUTH_MESSAGE` [above](#local-intents)), so that the app can perform any required actions. For example, the app might want to display a prompt to the user so that they can approve or reject the session.

The intent contains an object that describes the authentication session as an extra. This object contains the User ID and the Session ID, which are required for sending the authentication outcome to the server:
```java
ApproveSession authSession = intent.getParcelableExtra(FTRApproveNotification.PARAM_APPROVE_SESSION);
String userId = authSession.getUserId();
String sessionId = authSession.getSessionId();
```

Once the outcome of the authentication has been decided by the app, it should be sent to the server for the authentication session to complete.

##### <a id="approve-reply" />Approve Authentication
To approve the authentication session, use the following method:
```java
FuturaeClient.sharedClient().approveAuth(userId, sessionId, new FuturaeCallback() {
	@Override
	public void success() {

	}

	@Override
	public void failure(Throwable t) {

	}
});
```

##### <a id="reject-reply" />Reject Authentication
The user might choose to reject the authentication session. Additionally, in case the session has not been initiated by the user, they might also choose to report a fraudulent authentication attempt back to the server. In this case, use the following method:
```java
boolean reportFraud = false; // Set to true to report a fraudulent attempt
FuturaeClient.sharedClient().rejectAuth(userId, sessionId, reportFraud, new FuturaeCallback() {
	@Override
	public void success() {

	}

	@Override
	public void failure(Throwable t) {

	}
});
```

#### <a id="totp-auth" />TOTP Factor
The TOTP Factor can be used for offline authentication, as there is no requirement for an internet connection in the app. To get the current TOTP generated by the SDK for a specific user account, call the following method:
```java
CurrentTotp totp = FuturaeClient.sharedClient().nextTotp(userId);
String passcode = totp.passcode;            // The TOTP that the user should use to authenticate
int remainingSeconds = totp.remainingSecs;  // The remaining seconds of validity of this TOTP
```
As seen in this example, the `nextTotp()` method returns an object that contains the TOTP itself, but also the remaining seconds that this TOTP will be still valid for. After this time, a new TOTP must be obtained by the SDK.

[futurae.com]:  http://www.futurae.com

[releases]:    https://github.com/futurae/futura-ios-sdk/releases

[clone]:        ./resources/clone.png
[permissions]:  ./resources/permissions.png
[application_config]: ./resources/application_config.png
[application_config_xml]: ./resources/application_config_xml.png

[android_application]:            http://developer.android.com/reference/android/app/Application.html