Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

## Running

Before running the sample app please configure your google app id and Guardian url properly.
Before running the sample app please configure your Firebase app and Guardian URL properly.

You should change your google app id (required for GCM push notifications) at `src/main/res/values/google_gcm.xml`.
You should place the `google-services.json` file (required for FCM push notifications) inside your Android app module root. Typically at` at `MyApplication/app/google-services.json`.

The Guardian url can be changed at `src/main/res/values/guardian.xml`. It should look like
The Guardian URL can be changed at `src/main/res/values/guardian.xml`. It should look like
`https://<YOUR_TENANT>.guardian.auth0.com`.
6 changes: 4 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 28
Expand Down Expand Up @@ -29,8 +30,9 @@ dependencies {
implementation 'com.android.support:design:28.0.0'
//GSON
implementation 'com.google.code.gson:gson:2.8.5'
//GCM
implementation 'com.google.android.gms:play-services-gcm:16.1.0'
//FCM
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-messaging:17.5.0'
//EventBus
implementation 'org.greenrobot:eventbus:3.0.0'
//ZXing QR decoder deps
Expand Down
52 changes: 52 additions & 0 deletions app/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
** THIS IS A SAMPLE CONFIGURATIONFILE WITH DUMMY DATA
**
** Replace this file with a valid google-services.json
** downloaded from the Firebase Console with your project details.
** See: https://console.firebase.google.com
*/

{
"project_info": {
"project_number": "9999999999",
"firebase_url": "https://guardian.firebaseio.com",
"project_id": "guardian-fcm",
"storage_bucket": "guardian-fcm.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:9999999999:android:9999999999",
"android_client_info": {
"package_name": "com.auth0.guardian.sample"
}
},
"oauth_client": [
{
"client_id": "9999999999-j1oj123joojdsmnasdi123.apps.googleusercontent.com",
"client_type": 1,
"android_info": {
"package_name": "com.auth0.guardian.sample",
"certificate_hash": "n123n132o132noi1n3nnodon123"
}
}
],
"api_key": [
{
"current_key": "ANskdaSNDOsdno-ASKdknasdnsdKSD"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "9999999999-j1oj123joojdsmnasdi123.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
}
],
"configuration_version": "1"
}
62 changes: 7 additions & 55 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,40 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.auth0.guardian.sample">

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

<!-- [START GCM permissions] -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<!--
Optionally, the android.permission.WAKE_LOCK permission if the application needs to keep the
processor from sleeping when a message is received
-->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- [END GCM permissions] -->

<!-- [START GCM permissions for our app] -->
<!--
Your <application-package-name> + ".permission.C2D_MESSAGE" permission to prevent other Android
applications from registering and receiving the Android application's messages. The permission
name must exactly match this pattern—otherwise the Android application will not receive the
messages.
-->
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />

<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<!-- [END GCM permissions for our app] -->

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">

<activity
android:name=".MainActivity"
Expand All @@ -58,41 +36,15 @@

</activity>

<!-- [START GCM services] -->
<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.RECEIVE" />

<category android:name="${applicationId}" />
<!-- [START FCM services] -->

<!--
If you want to support pre-4.4 KitKat devices, add the following action to the
intent filter declaration for the receiver:
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
-->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>

<service
android:name=".gcm.GcmListenerService"
android:exported="false">
<service android:name=".fcm.FcmListenerService">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

<service
android:name=".gcm.GcmInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<!-- [END GCM services] -->
<!-- [END FCM services] -->

</application>

Expand Down
23 changes: 11 additions & 12 deletions app/src/main/java/com/auth0/guardian/sample/EnrollActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,24 @@ public class EnrollActivity extends AppCompatActivity implements CaptureView.Lis
private static final String TAG = EnrollActivity.class.getName();

private static final String DEVICE_NAME = "com.auth0.guardian.sample.EnrollActivity.DEVICE_NAME";
private static final String GCM_TOKEN = "com.auth0.guardian.sample.EnrollActivity.GCM_TOKEN";
private static final String FCM_TOKEN = "com.auth0.guardian.sample.EnrollActivity.FCM_TOKEN";

private static final int REQUEST_CAMERA = 55;

private Guardian guardian;
private String deviceName;
private String gcmToken;
private String fcmToken;

private View permissionLayout;
private View scannerLayout;
private CaptureView scanner;

static Intent getStartIntent(@NonNull Context context,
@NonNull String deviceName,
@NonNull String gcmToken) {
@NonNull String fcmToken) {
Intent intent = new Intent(context, EnrollActivity.class);
intent.putExtra(DEVICE_NAME, deviceName);
intent.putExtra(GCM_TOKEN, gcmToken);
intent.putExtra(FCM_TOKEN, fcmToken);
return intent;
}

Expand Down Expand Up @@ -132,7 +132,7 @@ public void onRequestPermissionsResult(int requestCode,
public void onCodeScanned(String enrollmentData) {
try {
KeyPair keyPair = generateKeyPair();
CurrentDevice device = new CurrentDevice(this, gcmToken, deviceName);
CurrentDevice device = new CurrentDevice(this, fcmToken, deviceName);
guardian.enroll(enrollmentData, device, keyPair)
.start(new DialogCallback<>(this,
R.string.progress_title_please_wait,
Expand All @@ -141,7 +141,7 @@ public void onCodeScanned(String enrollmentData) {
@Override
public void onSuccess(Enrollment enrollment) {
Log.d(TAG, "enroll success");
onEnrollSucess(enrollment);
onEnrollSuccess(enrollment);
}

@Override
Expand All @@ -158,10 +158,10 @@ public void onFailure(Throwable exception) {
private void setupGuardian() {
Intent intent = getIntent();
deviceName = intent.getStringExtra(DEVICE_NAME);
gcmToken = intent.getStringExtra(GCM_TOKEN);
fcmToken = intent.getStringExtra(FCM_TOKEN);

if (deviceName == null || gcmToken == null) {
throw new IllegalStateException("Missing deviceName or gcmToken");
if (deviceName == null || fcmToken == null) {
throw new IllegalStateException("Missing deviceName or fcmToken");
}

guardian = new Guardian.Builder()
Expand Down Expand Up @@ -231,7 +231,7 @@ private void resumeScanning() {
scanner.resume();
}

private void onEnrollSucess(Enrollment enrollment) {
private void onEnrollSuccess(Enrollment enrollment) {
Intent data = new Intent();
ParcelableEnrollment parcelableEnrollment = new ParcelableEnrollment(enrollment);
data.putExtra(Constants.ENROLLMENT, parcelableEnrollment);
Expand Down Expand Up @@ -270,8 +270,7 @@ private KeyPair generateKeyPair() {
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048); // at least 2048 bits!
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
return keyPairGenerator.generateKeyPair();
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "Error generating keys", e);
}
Expand Down
41 changes: 24 additions & 17 deletions app/src/main/java/com/auth0/guardian/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
import com.auth0.android.guardian.sdk.ParcelableNotification;
import com.auth0.android.guardian.sdk.networking.Callback;
import com.auth0.guardian.sample.events.GuardianNotificationReceivedEvent;
import com.auth0.guardian.sample.gcm.GcmUtils;
import com.auth0.guardian.sample.fcm.FcmUtils;
import com.auth0.guardian.sample.views.TOTPCodeView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity implements GcmUtils.GcmTokenListener {
public class MainActivity extends AppCompatActivity implements FcmUtils.FcmTokenListener {

private static final String TAG = MainActivity.class.getName();

Expand All @@ -60,14 +60,14 @@ public class MainActivity extends AppCompatActivity implements GcmUtils.GcmToken
private View enrollView;
private View accountView;
private TextView deviceNameText;
private TextView gcmTokenText;
private TextView fcmTokenText;
private TextView userText;
private TOTPCodeView otpView;

private EventBus eventBus;
private Guardian guardian;
private ParcelableEnrollment enrollment;
private String gcmToken;
private String fcmToken;

public static Intent getStartIntent(@NonNull Context context,
@NonNull ParcelableNotification notification) {
Expand All @@ -91,8 +91,15 @@ protected void onCreate(Bundle savedInstanceState) {
.enableLogging()
.build();

GcmUtils gcmUtils = new GcmUtils(this, getString(R.string.google_app_id));
gcmUtils.fetchGcmToken(this);
/*
* The following fetch token call is NOT required in a production app
* as the registration token is generated automatically by the Firebase SDK.
* This is just here for display purposes on this Activity's layout.
*
* See: https://developers.google.com/cloud-messaging/android/android-migrate-iid-service
*/
FcmUtils fcmUtils = new FcmUtils();
fcmUtils.fetchFcmToken(this);

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String enrollmentJSON = sharedPreferences.getString(Constants.ENROLLMENT, null);
Expand Down Expand Up @@ -130,7 +137,7 @@ private void setupUI() {
enrollView = findViewById(R.id.enrollLayout);
accountView = findViewById(R.id.accountLayout);
deviceNameText = (TextView) findViewById(R.id.deviceNameText);
gcmTokenText = (TextView) findViewById(R.id.gcmTokenText);
fcmTokenText = (TextView) findViewById(R.id.fcmTokenText);
userText = (TextView) findViewById(R.id.userText);
otpView = (TOTPCodeView) findViewById(R.id.otpView);

Expand Down Expand Up @@ -159,24 +166,24 @@ private void updateUI() {
runOnUiThread(new Runnable() {
@Override
public void run() {
loadingView.setVisibility(gcmToken != null ? View.GONE : View.VISIBLE);
loadingView.setVisibility(fcmToken != null ? View.GONE : View.VISIBLE);
if (enrollment == null) {
gcmTokenText.setText(gcmToken != null ? gcmToken : null);
fcmTokenText.setText(fcmToken);
accountView.setVisibility(View.GONE);
enrollView.setVisibility(gcmToken != null ? View.VISIBLE : View.GONE);
enrollView.setVisibility(fcmToken != null ? View.VISIBLE : View.GONE);
} else {
userText.setText(enrollment.getUserId());
otpView.setEnrollment(enrollment);
enrollView.setVisibility(View.GONE);
accountView.setVisibility(gcmToken != null ? View.VISIBLE : View.GONE);
accountView.setVisibility(fcmToken != null ? View.VISIBLE : View.GONE);
}
}
});
}

private void onEnrollRequested() {
Intent enrollIntent = EnrollActivity
.getStartIntent(this, deviceNameText.getText().toString(), gcmToken);
.getStartIntent(this, deviceNameText.getText().toString(), fcmToken);
startActivityForResult(enrollIntent, ENROLL_REQUEST);
}

Expand Down Expand Up @@ -222,18 +229,18 @@ private void onPushNotificationReceived(ParcelableNotification notification) {
}

@Override
public void onGcmTokenObtained(String gcmToken) {
this.gcmToken = gcmToken;
public void onFcmTokenObtained(String fcmToken) {
this.fcmToken = fcmToken;

updateUI();
}

@Override
public void onGcmFailure(Throwable exception) {
Log.e(TAG, "Error obtaining GCM token", exception);
public void onFcmFailure(Throwable exception) {
Log.e(TAG, "Error obtaining FCM token", exception);
new AlertDialog.Builder(this)
.setTitle(R.string.alert_title_error)
.setMessage(getString(R.string.alert_message_gcm_error))
.setMessage(getString(R.string.alert_message_fcm_error))
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Expand Down
Loading