Skip to content
Draft
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 @@ -11,6 +11,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.optimove.android.auth.AuthManager;
import com.optimove.android.embeddedmessaging.OptimoveEmbeddedMessaging;
import com.optimove.android.main.common.EventHandlerFactory;
import com.optimove.android.main.common.EventHandlerProvider;
Expand Down Expand Up @@ -72,6 +73,7 @@ final public class Optimove {
private final LifecycleObserver lifecycleObserver;

private static OptimoveConfig currentConfig;
private static @Nullable AuthManager authManager;

public enum IBeaconProximity {
UNKNOWN,
Expand Down Expand Up @@ -110,6 +112,7 @@ private Optimove(@NonNull Context context, OptimoveConfig config) {
.optistreamDbHelper(new OptistreamDbHelper(context))
.lifecycleObserver(lifecycleObserver)
.context(context)
.authManager(authManager)
.build());

this.optimoveLifecycleEventGenerator = new OptimoveLifecycleEventGenerator(eventHandlerProvider, userInfo,
Expand Down Expand Up @@ -141,10 +144,17 @@ public static Optimove getInstance() {
public static void initialize(@NonNull Application application, @NonNull OptimoveConfig config) {
currentConfig = config;

if (config.getAuthTokenProvider() != null) {
authManager = new AuthManager(config.getAuthTokenProvider());
} else {
authManager = null;
}

performSingletonInitialization(application.getApplicationContext(), config);

if (config.isOptimobileConfigured()) {
Optimobile.initialize(application, config, shared.userInfo.getInitialVisitorId(), shared.userInfo.getUserId());
Optimobile.setAuthManager(authManager);
}

if (config.isOptimoveConfigured()) {
Expand Down Expand Up @@ -285,6 +295,10 @@ public static OptimoveConfig getConfig() {
return currentConfig;
}

public static @Nullable AuthManager getAuthManager() {
return authManager;
}

/**
* Enables remote logs for investigations. Don't call it unless we explicitly asked you to.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.optimove.android.auth.AuthTokenProvider;
import com.optimove.android.embeddedmessaging.EmbeddedMessagingConfig;
import com.optimove.android.main.tools.opti_logger.LogLevel;
import com.optimove.android.optimobile.DeferredDeepLinkHandlerInterface;
Expand Down Expand Up @@ -73,6 +74,8 @@ public final class OptimoveConfig {

private @Nullable EmbeddedMessagingConfig embeddedMessagingConfig;

private @Nullable AuthTokenProvider authTokenProvider;

public enum InAppConsentStrategy {
AUTO_ENROLL,
EXPLICIT_BY_USER
Expand Down Expand Up @@ -415,6 +418,14 @@ public boolean usesDelayedConfiguration() {
return this.embeddedMessagingConfig;
}

public @Nullable AuthTokenProvider getAuthTokenProvider() {
return authTokenProvider;
}

private void setAuthTokenProvider(@Nullable AuthTokenProvider provider) {
this.authTokenProvider = provider;
}

private boolean hasFinishedInitialisation() {
boolean hasOptimoveCreds = optimoveToken != null && configFileName != null;
boolean hasOptimobileCreds = apiKey != null && secretKey != null;
Expand Down Expand Up @@ -473,6 +484,7 @@ public static class Builder {
private DeferredDeepLinkHandlerInterface deferredDeepLinkHandler;

private @Nullable LogLevel minLogLevel;
private @Nullable AuthTokenProvider authTokenProvider;

/**
* @deprecated Use {@link Builder#Builder(FeatureSet)} instead
Expand Down Expand Up @@ -575,6 +587,11 @@ public Builder enableEmbeddedMessaging(@NonNull String embeddedMessagingConfigur
return this;
}

public Builder enableAuth(@NonNull AuthTokenProvider provider) {
this.authTokenProvider = provider;
return this;
}

/**
* The minimum amount of time the user has to have left the app for a session end event to be
* recorded.
Expand Down Expand Up @@ -664,6 +681,7 @@ public OptimoveConfig build() {
newConfig.setDeferredDeepLinkHandler(this.deferredDeepLinkHandler);

newConfig.setMinLogLevel(this.minLogLevel);
newConfig.setAuthTokenProvider(this.authTokenProvider);

return newConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import androidx.annotation.Nullable;

import com.optimove.android.Optimove;
import com.optimove.android.auth.AuthManager;
import com.optimove.android.main.common.UserInfo;
import com.optimove.android.main.tools.networking.HttpClient;

Expand All @@ -17,6 +18,7 @@

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -129,7 +131,7 @@ public void deleteMessageAsync(EmbeddedMessage message, @NonNull EmbeddedMessage
new Date(), UUID.randomUUID().toString(), EventType.DELETED, context, userId,
userInfo.getVisitorId());

Runnable task = new EventReportEmbeddedMessagesRunnable(config, request, embeddedMessagesDeleteHandler);
Runnable task = new EventReportEmbeddedMessagesRunnable(config, userId, request, embeddedMessagesDeleteHandler);
executorService.submit(task);
}

Expand Down Expand Up @@ -158,7 +160,7 @@ public void reportClickMetricAsync(
EmbeddedMessageEventRequest request = new EmbeddedMessageEventRequest(
new Date(), UUID.randomUUID().toString(), EventType.CLICKED, context, userId,
userInfo.getVisitorId());
Runnable task = new EventReportEmbeddedMessagesRunnable(config, request, embeddedMessagesMetricsHandler);
Runnable task = new EventReportEmbeddedMessagesRunnable(config, userId, request, embeddedMessagesMetricsHandler);
executorService.submit(task);
}

Expand Down Expand Up @@ -188,7 +190,7 @@ public void setAsReadASync(EmbeddedMessage message, boolean isRead,
new Date(), UUID.randomUUID().toString(), isRead ? EventType.READ : EventType.UNREAD,
context, userId, userInfo.getVisitorId());
Runnable task = new EventReportEmbeddedMessagesRunnable(
config, request, embeddedMessagesStatusHandler);
config, userId, request, embeddedMessagesStatusHandler);
executorService.submit(task);
}

Expand All @@ -210,7 +212,7 @@ class GetEmbeddedMessagesRunnable extends EmbeddedMessagesRunnableBase implement

GetEmbeddedMessagesRunnable(
EmbeddedMessagingConfig config, String customerId, EmbeddedMessagesGetHandler callback, ContainerRequestOptions[] requestBody) {
super(config);
super(config, customerId);
this.customerId = customerId;
this.callback = callback;
this.requestBody = requestBody;
Expand Down Expand Up @@ -250,9 +252,10 @@ class EventReportEmbeddedMessagesRunnable extends EmbeddedMessagesRunnableBase i

EventReportEmbeddedMessagesRunnable(
EmbeddedMessagingConfig config,
String customerId,
EmbeddedMessageEventRequest request,
EmbeddedMessagesSetHandler callback) {
super(config);
super(config, customerId);
this.callback = callback;
this.request = request;
}
Expand Down Expand Up @@ -281,22 +284,44 @@ private void fireCallback(ResultType result) {

class EmbeddedMessagesRunnableBase {
protected EmbeddedMessagingConfig config;
protected String customerId;

public EmbeddedMessagesRunnableBase(EmbeddedMessagingConfig config) {
public EmbeddedMessagesRunnableBase(EmbeddedMessagingConfig config, String customerId) {
this.config = config;
this.customerId = customerId;
}

public EmbeddedMessagingResult postSync(String url, JSONArray postData, boolean expectResponse) {
Map<String, String> extraHeaders = resolveAuthHeaders();
if (extraHeaders == null) {
return new EmbeddedMessagingResult(ResultType.ERROR, null);
}

HttpClient httpClient = HttpClient.getInstance();

try (Response response = httpClient.postSync(url, postData, config.getTenantId())) {
try (Response response = httpClient.postSync(url, postData, config.getTenantId(), extraHeaders)) {
return handleResponse(response, expectResponse);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
return new EmbeddedMessagingResult(ResultType.ERROR, null);
}
}

@Nullable
private Map<String, String> resolveAuthHeaders() {
AuthManager authManager = Optimove.getAuthManager();
if (authManager == null || customerId == null || customerId.isEmpty()) {
return Collections.emptyMap();
}

String jwt = AuthManager.getTokenBlocking(authManager, customerId);
if (jwt == null) {
Log.e(TAG, "Auth token fetch failed. Dropping request.");
return null;
}
return Collections.singletonMap("X-User-JWT", jwt);
}
protected String getBaseUrl(String endpoint) {
String region = config.getRegion();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import android.content.Context;

import androidx.annotation.Nullable;

import com.optimove.android.auth.AuthManager;
import com.optimove.android.main.event_handlers.DestinationDecider;
import com.optimove.android.main.event_handlers.EventDecorator;
import com.optimove.android.main.event_handlers.EventMemoryBuffer;
Expand All @@ -28,17 +31,19 @@ public class EventHandlerFactory {
private OptistreamDbHelper optistreamDbHelper;
private LifecycleObserver lifecycleObserver;
private Context context;
private @Nullable AuthManager authManager;

private EventHandlerFactory(HttpClient httpClient, UserInfo userInfo,
int maximumBufferSize, OptistreamDbHelper optistreamDbHelper,
LifecycleObserver lifecycleObserver, Context context) {
LifecycleObserver lifecycleObserver, Context context,
@Nullable AuthManager authManager) {
this.httpClient = httpClient;
this.userInfo = userInfo;
this.maximumBufferSize = maximumBufferSize;
this.optistreamDbHelper = optistreamDbHelper;
this.lifecycleObserver = lifecycleObserver;
this.context = context;

this.authManager = authManager;
}

public EventMemoryBuffer getEventBuffer() {
Expand All @@ -58,11 +63,11 @@ public EventDecorator getEventDecorator(Map<String, EventConfigs> eventConfigs,
}

public RealtimeManager getRealtimeMananger(RealtimeConfigs realtimeConfigs) {
return new RealtimeManager(httpClient, realtimeConfigs, context);
return new RealtimeManager(httpClient, realtimeConfigs, context, authManager);
}

public OptistreamHandler getOptistreamHandler(OptitrackConfigs optitrackConfigs) {
return new OptistreamHandler(httpClient, lifecycleObserver, optistreamDbHelper, optitrackConfigs);
return new OptistreamHandler(httpClient, lifecycleObserver, optistreamDbHelper, optitrackConfigs, authManager);
}

public DestinationDecider getDestinationDecider(Map<String, EventConfigs> eventConfigs,
Expand Down Expand Up @@ -109,6 +114,7 @@ public interface ContextStep {
}

public interface Build {
Build authManager(@Nullable AuthManager authManager);
EventHandlerFactory build();
}

Expand All @@ -121,6 +127,7 @@ public static class Builder implements OptistreamDbHelperStep, MaximumBufferSize
private OptistreamDbHelper optistreamDbHelper;
private LifecycleObserver lifecycleObserver;
private Context context;
private @Nullable AuthManager authManager;

@Override
public HttpClientStep userInfo(UserInfo userInfo) {
Expand Down Expand Up @@ -158,10 +165,16 @@ public Build context(Context context) {
return this;
}

@Override
public Build authManager(@Nullable AuthManager authManager) {
this.authManager = authManager;
return this;
}

@Override
public EventHandlerFactory build() {
return new EventHandlerFactory(httpClient, userInfo, maximumBufferSize, optistreamDbHelper,
lifecycleObserver, context);
lifecycleObserver, context, authManager);
}
}

Expand Down
Loading
Loading