Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Identity verification implementation #2196

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.multidex.MultiDexApplication;

import com.onesignal.Continue;
import com.onesignal.IUserJwtInvalidatedListener;
import com.onesignal.OneSignal;
import com.onesignal.UserJwtInvalidatedEvent;
import com.onesignal.inAppMessages.IInAppMessageClickListener;
import com.onesignal.inAppMessages.IInAppMessageClickEvent;
import com.onesignal.inAppMessages.IInAppMessageDidDismissEvent;
Expand Down Expand Up @@ -140,6 +141,13 @@ public void onUserStateChange(@NonNull UserChangedState state) {
}
});

OneSignal.addUserJwtInvalidatedListner(new IUserJwtInvalidatedListener() {
@Override
public void onUserJwtInvalidated(@NonNull UserJwtInvalidatedEvent event) {
Log.v(Tag.LOG_TAG, "onUserJwtInvalidated fired with ID:" + event.getExternalId());
}
});

OneSignal.getInAppMessages().setPaused(true);
OneSignal.getLocation().setShared(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
Expand Down Expand Up @@ -83,6 +84,10 @@ public class MainActivityViewModel implements ActivityViewModel, IPushSubscripti
private Button loginUserButton;
private Button logoutUserButton;

// JWT
private Button invalidateJwtButton;
private Button updateJwtButton;

// Alias
private TextView aliasTitleTextView;
private RecyclerView aliasesRecyclerView;
Expand Down Expand Up @@ -211,6 +216,9 @@ public ActivityViewModel onActivityCreated(Context context) {
loginUserButton = getActivity().findViewById(R.id.main_activity_login_user_button);
logoutUserButton = getActivity().findViewById(R.id.main_activity_logout_user_button);

invalidateJwtButton = getActivity().findViewById(R.id.main_activity_invalidate_jwt_button);
updateJwtButton = getActivity().findViewById(R.id.main_activity_update_jwt_button);

aliasTitleTextView = getActivity().findViewById(R.id.main_activity_aliases_title_text_view);
noAliasesTextView = getActivity().findViewById(R.id.main_activity_aliases_no_aliases_text_view);
addAliasButton = getActivity().findViewById(R.id.main_activity_add_alias_button);
Expand Down Expand Up @@ -422,6 +430,7 @@ public void onFailure() {
}

private void setupUserLayout() {
setupJWTLayout();
setupAliasLayout();
setupEmailLayout();
setupSMSLayout();
Expand All @@ -430,6 +439,28 @@ private void setupUserLayout() {
setupTriggersLayout();
}

private void setupJWTLayout() {
invalidateJwtButton.setOnClickListener(v -> {
OneSignal.updateUserJwt(OneSignal.getUser().getExternalId(), "");
});
updateJwtButton.setOnClickListener(v -> {
dialog.createUpdateAlertDialog("", Dialog.DialogAction.UPDATE, ProfileUtil.FieldType.JWT, new UpdateAlertDialogCallback() {
@Override
public void onSuccess(String update) {
if (update != null && !update.isEmpty()) {
OneSignal.updateUserJwt(OneSignal.getUser().getExternalId(), update);
refreshState();
}
}

@Override
public void onFailure() {

}
});
});
}

private void setupAliasLayout() {
setupAliasesRecyclerView();
addAliasButton.setOnClickListener(v -> dialog.createAddPairAlertDialog("Add Alias", ProfileUtil.FieldType.ALIAS, new AddPairAlertDialogCallback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum FieldType {
ALIAS("Alias"),
EMAIL("Email"),
SMS("SMS"),
JWT("JWT"),
EXTERNAL_USER_ID("External User Id"),

TAG("Tags"),
Expand Down Expand Up @@ -97,6 +98,10 @@ public static boolean isSMSValid(TextInputLayout smsTextInputLayout) {
return true;
}

private static boolean isJWTValid(TextInputLayout jwtTextInputLayout) {
return !jwtTextInputLayout.getEditText().toString().isEmpty();
}

private static boolean isExternalUserIdValid(TextInputLayout externalUserIdTextInputLayout) {
externalUserIdTextInputLayout.setErrorEnabled(false);
if (externalUserIdTextInputLayout.getEditText() != null) {
Expand Down Expand Up @@ -137,6 +142,8 @@ static boolean isContentValid(FieldType field, TextInputLayout alertDialogTextIn
return isEmailValid(alertDialogTextInputLayout);
case SMS:
return isSMSValid(alertDialogTextInputLayout);
case JWT:
return isJWTValid(alertDialogTextInputLayout);
case EXTERNAL_USER_ID:
return isExternalUserIdValid(alertDialogTextInputLayout);
case TAG:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,20 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:gravity="start|center_vertical"
android:layout_marginBottom="4dp"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginBottom="4dp"
android:gravity="start|center_vertical"
android:text="@string/app"
android:textColor="@color/colorDarkText" />

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
app:cardCornerRadius="6dp"
app:cardElevation="4dp">

Expand Down Expand Up @@ -226,23 +226,23 @@
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginStart="12dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:orientation="vertical"
android:background="@color/colorPrimary">
android:background="@color/colorPrimary"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/main_activity_app_revoke_consent_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ripple_selector_white_red"
android:text="@string/revoke_consent"
android:textSize="19sp"
android:textColor="@android:color/white"
android:background="@drawable/ripple_selector_white_red"
android:visibility="visible"/>
android:textSize="19sp"
android:visibility="visible" />

</LinearLayout>

Expand All @@ -263,11 +263,11 @@
android:id="@+id/main_activity_login_user_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ripple_selector_white_red"
android:text="@string/login_user"
android:textSize="19sp"
android:textColor="@android:color/white"
android:background="@drawable/ripple_selector_white_red"
android:visibility="visible"/>
android:textSize="19sp"
android:visibility="visible" />

</LinearLayout>

Expand All @@ -288,14 +288,97 @@
android:id="@+id/main_activity_logout_user_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ripple_selector_white_red"
android:text="@string/logout_user"
android:textSize="19sp"
android:textColor="@android:color/white"
android:background="@drawable/ripple_selector_white_red"
android:visibility="visible"/>
android:textSize="19sp"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>

<LinearLayout
android:id="@+id/main_activity_jwt_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical">

<TextView
android:id="@+id/main_activity_jwt_title_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginBottom="4dp"
android:gravity="start|center_vertical"
android:text="@string/JWT"
android:textColor="@color/colorDarkText" />

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
app:cardCornerRadius="6dp"
app:cardElevation="4dp">

<LinearLayout
android:id="@+id/main_activity_jwt_details_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/main_activity_invalidate_jwt_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ripple_selector_white_red"
android:text="@string/invalidate_JWT"
android:textColor="@android:color/white"
android:textSize="19sp"
android:visibility="visible" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/main_activity_update_jwt_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ripple_selector_white_red"
android:text="@string/update_JWT"
android:textColor="@android:color/white"
android:textSize="19sp"
android:visibility="visible" />
</LinearLayout>

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<!-- Aliases -->
<LinearLayout
android:id="@+id/main_activity_aliases_linear_layout"
Expand Down
5 changes: 5 additions & 0 deletions Examples/OneSignalDemo/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<string name="login_user">Login User</string>
<string name="logout_user">Logout User</string>

<string name="JWT">JWT</string>
<string name="invalidate_JWT">Invalidate JWT</string>
<string name="update_JWT">Update JWT</string>
<string name="no_jwt_added">No JWT Added</string>

<string name="aliases">Aliases</string>
<string name="external_user_id_colon">EUID:</string>
<string name="no_aliases_added">No Aliases Added</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,16 @@ interface IOneSignal {
* data is not cleared.
*/
fun logout()

/**
* Update JWT token for a user
*/
fun updateUserJwt(
externalId: String,
token: String,
)

fun addUserJwtInvalidatedListener(listener: IUserJwtInvalidatedListener)

fun removeUserJwtInvalidatedListener(listener: IUserJwtInvalidatedListener)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.onesignal

/**
* Implement this interface and provide an instance to [OneSignal.addUserJwtInvalidatedListner]
* in order to receive control when the JWT for the current user is invalidated.
*
* @see [User JWT Invalidated Event | OneSignal Docs](https://documentation.onesignal.com/docs/identity-verification)
*/
interface IUserJwtInvalidatedListener {
/**
* Called when the JWT is invalidated
*
* @param event The user JWT that expired.
*/
fun onUserJwtInvalidated(event: UserJwtInvalidatedEvent)
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,24 @@ object OneSignal {
@JvmStatic
fun logout() = oneSignal.logout()

@JvmStatic
fun updateUserJwt(
externalId: String,
token: String,
) {
oneSignal.updateUserJwt(externalId, token)
}

@JvmStatic
fun addUserJwtInvalidatedListner(listener: IUserJwtInvalidatedListener) {
oneSignal.addUserJwtInvalidatedListener(listener)
}

@JvmStatic
fun removeUserJwtInvalidatedListner(listener: IUserJwtInvalidatedListener) {
oneSignal.removeUserJwtInvalidatedListener(listener)
}

private val oneSignal: IOneSignal by lazy {
OneSignalImp()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.onesignal

/**
* The event passed into [IUserJwtInvalidatedListener.onUserJwtInvalidated], it provides access
* to the external ID whose JWT has just been invalidated.
*
* For more information https://documentation.onesignal.com/docs/identity-verification#4-handle-jwt-lifecycle-events
*/
class UserJwtInvalidatedEvent(
val externalId: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.onesignal.core.internal.preferences.IPreferencesService
import com.onesignal.core.internal.preferences.impl.PreferencesService
import com.onesignal.core.internal.purchases.impl.TrackAmazonPurchase
import com.onesignal.core.internal.purchases.impl.TrackGooglePurchase
import com.onesignal.core.internal.startup.IBootstrapService
import com.onesignal.core.internal.startup.IStartableService
import com.onesignal.core.internal.time.ITime
import com.onesignal.core.internal.time.impl.Time
Expand All @@ -40,6 +41,7 @@ import com.onesignal.location.ILocationManager
import com.onesignal.location.internal.MisconfiguredLocationManager
import com.onesignal.notifications.INotificationsManager
import com.onesignal.notifications.internal.MisconfiguredNotificationsManager
import com.onesignal.user.internal.service.IdentityVerificationService

internal class CoreModule : IModule {
override fun register(builder: ServiceBuilder) {
Expand All @@ -59,6 +61,7 @@ internal class CoreModule : IModule {
builder.register<ConfigModelStore>().provides<ConfigModelStore>()
builder.register<ParamsBackendService>().provides<IParamsBackendService>()
builder.register<ConfigModelStoreListener>().provides<IStartableService>()
builder.register<IdentityVerificationService>().provides<IBootstrapService>()

// Operations
builder.register<OperationModelStore>().provides<OperationModelStore>()
Expand Down
Loading
Loading