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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public interface StreamClient {
*/
PersonalizedFeed newPersonalizedFeed(String feedSlug, String id) throws InvalidFeedNameException;


/**
* Generate User Session JWT Token.
* @param userId User identifier
* @return JWT Token
*/
String getUserSessionToken(String userId);

/**
* Send the shutdown signal to the client.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ public String getReadOnlyToken() {
return streamRepository.getReadOnlyToken(this);
}

@Override
public String getUserSessionToken() {
return streamRepository.getUserSessionToken(this);
}

@Override
public void follow(String feedSlug, String userId) throws IOException, StreamClientException {
String feedId = String.format("%s:%s", feedSlug, userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ public interface Feed {
*/
String getReadOnlyToken();

/**
* Generate User Session JWT Token. UserId is taken from {@link Feed#getId()}.
* @return Token
*/
String getUserSessionToken();

/**
* Follows the given target feed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ public interface StreamRepository {
*/
String getReadOnlyToken(BaseFeed feed);

String getUserSessionToken(BaseFeed feed);

/**
* Follow a feed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.getstream.client.repo.StreamPersonalizedRepository;
import io.getstream.client.repo.StreamRepository;
import io.getstream.client.util.InfoUtil;
import io.getstream.client.util.JwtAuthenticationUtil;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
Expand All @@ -39,13 +40,18 @@ public class StreamClientImpl implements StreamClient {
private FeedFactory feedFactory;
private final StreamRepository streamRepository;
private final Optional<StreamPersonalizedRepository> streamPersonalizedRepository;
private final String apiKey;
private final String secretKey;

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

public StreamClientImpl(final ClientConfiguration clientConfiguration, final String key, final String secretKey) {
this.apiKey = key;
this.secretKey = secretKey;

Preconditions.checkNotNull(clientConfiguration, "Client configuration cannot be null.");
AuthenticationHandlerConfiguration authenticationHandlerConfiguration = new AuthenticationHandlerConfiguration();
authenticationHandlerConfiguration.setApiKey(checkNotNull(key, "API key cannot be null."));
Expand All @@ -70,6 +76,11 @@ public PersonalizedFeed newPersonalizedFeed(final String feedSlug,
return this.feedFactory.createPersonalizedFeed(feedSlug, id);
}

@Override
public String getUserSessionToken(String userId) {
return JwtAuthenticationUtil.generateToken(getSecretKey(), null, userId);
}

@Override
public void shutdown() throws IOException {
this.streamRepository.shutdown();
Expand Down Expand Up @@ -123,4 +134,12 @@ private CloseableHttpClient initClient(final ClientConfiguration config,
public static ObjectMapper getObjectMapper() {
return OBJECT_MAPPER;
}

public String getApiKey() {
return apiKey;
}

public String getSecretKey() {
return secretKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ public String getReadOnlyToken(BaseFeed feed) {
return JwtAuthenticationUtil.generateToken(secretKey, "read", "*", feed.getFeedSlug().concat(feed.getUserId()), null);
}

@Override
public String getUserSessionToken(BaseFeed feed) {
return JwtAuthenticationUtil.generateToken(secretKey, null, feed.getUserId());
}

@Override
public void follow(BaseFeed feed, String targetFeedId, int activityCopyLimit) throws StreamClientException, IOException {
HttpPost request = new HttpPost(UriBuilder.fromEndpoint(baseEndpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ public void shouldGetReadOnlyToken() throws IOException, StreamClientException,
@Test
public void shouldGetUserSessionToken() throws StreamClientException {
StreamClient streamClient = new StreamClientImpl(CLIENT_CONFIGURATION, API_KEY, API_SECRET);
Feed feed = streamClient.newFeed("feedslug", "aUserId");

Map<String, Claim> map = verifyToken(feed.getUserSessionToken());
Map<String, Claim> map = verifyToken(streamClient.getUserSessionToken("aUserId"));
assertThat(map.get("user_id").asString(), is("aUserId"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package io.getstream.client.okhttp;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
Expand All @@ -26,25 +20,37 @@
import io.getstream.client.repo.StreamPersonalizedRepository;
import io.getstream.client.repo.StreamRepository;
import io.getstream.client.util.InfoUtil;
import io.getstream.client.util.JwtAuthenticationUtil;
import okhttp3.ConnectionPool;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import static com.google.common.base.Preconditions.checkNotNull;

public class StreamClientImpl implements StreamClient {

private static final String USER_AGENT_PREFIX = "stream-java-okhttp-%s";

private final Optional<StreamPersonalizedRepository> streamPersonalizedRepository;
private FeedFactory feedFactory;
private final StreamRepository streamRepository;
private final String apiKey;
private final String secretKey;

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

public StreamClientImpl(final ClientConfiguration clientConfiguration, final String key, final String secretKey) {
this.apiKey = key;
this.secretKey = secretKey;

Preconditions.checkNotNull(clientConfiguration, "Client configuration cannot be null.");
AuthenticationHandlerConfiguration authenticationHandlerConfiguration = new AuthenticationHandlerConfiguration();
authenticationHandlerConfiguration.setApiKey(checkNotNull(key, "API key cannot be null."));
Expand All @@ -69,6 +75,11 @@ public PersonalizedFeed newPersonalizedFeed(final String feedSlug,
return this.feedFactory.createPersonalizedFeed(feedSlug, id);
}

@Override
public String getUserSessionToken(String userId) {
return JwtAuthenticationUtil.generateToken(getSecretKey(), null, userId);
}

@Override
public void shutdown() throws IOException {
this.streamRepository.shutdown();
Expand Down Expand Up @@ -124,4 +135,12 @@ private String getUserAgent() {
public static ObjectMapper getObjectMapper() {
return OBJECT_MAPPER;
}

public String getApiKey() {
return apiKey;
}

public String getSecretKey() {
return secretKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ public String getToken(BaseFeed feed) {
return StreamRepoUtils.createFeedToken(feed, secretKey);
}

@Override
public String getUserSessionToken(BaseFeed feed) {
return JwtAuthenticationUtil.generateToken(secretKey, null, feed.getUserId());
}

@Override
public void follow(BaseFeed feed, String targetFeedId, int activityCopyLimit) throws StreamClientException, IOException {
Request.Builder requestBuilder = new Request.Builder().url(UriBuilder.fromEndpoint(baseEndpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ public void shouldGetReadOnlyToken() throws StreamClientException {
@Test
public void shouldGetUserSessionToken() throws StreamClientException {
StreamClient streamClient = new StreamClientImpl(CLIENT_CONFIGURATION, API_KEY, API_SECRET);
Feed feed = streamClient.newFeed("feedslug", "aUserId");

Map<String, Claim> map = verifyToken(feed.getUserSessionToken());
Map<String, Claim> map = verifyToken(streamClient.getUserSessionToken("aUserId"));
assertThat(map.get("user_id").asString(), is("aUserId"));
}

Expand Down