Skip to content

Commit 5969137

Browse files
authored
Merge pull request #48 from GetStream/feature/getUserSessionToken
Moved getUserSessionToken() to StreamClient to keep the design consis…
2 parents 634cca9 + b30e413 commit 5969137

File tree

10 files changed

+54
-35
lines changed

10 files changed

+54
-35
lines changed

stream-core/src/main/java/io/getstream/client/StreamClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public interface StreamClient {
3131
*/
3232
PersonalizedFeed newPersonalizedFeed(String feedSlug, String id) throws InvalidFeedNameException;
3333

34+
35+
/**
36+
* Generate User Session JWT Token.
37+
* @param userId User identifier
38+
* @return JWT Token
39+
*/
40+
String getUserSessionToken(String userId);
41+
3442
/**
3543
* Send the shutdown signal to the client.
3644
*

stream-core/src/main/java/io/getstream/client/model/feeds/BaseFeed.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ public String getReadOnlyToken() {
4848
return streamRepository.getReadOnlyToken(this);
4949
}
5050

51-
@Override
52-
public String getUserSessionToken() {
53-
return streamRepository.getUserSessionToken(this);
54-
}
55-
5651
@Override
5752
public void follow(String feedSlug, String userId) throws IOException, StreamClientException {
5853
String feedId = String.format("%s:%s", feedSlug, userId);

stream-core/src/main/java/io/getstream/client/model/feeds/Feed.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ public interface Feed {
3939
*/
4040
String getReadOnlyToken();
4141

42-
/**
43-
* Generate User Session JWT Token. UserId is taken from {@link Feed#getId()}.
44-
* @return Token
45-
*/
46-
String getUserSessionToken();
47-
4842
/**
4943
* Follows the given target feed.
5044
*

stream-core/src/main/java/io/getstream/client/repo/StreamRepository.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ public interface StreamRepository {
4848
*/
4949
String getReadOnlyToken(BaseFeed feed);
5050

51-
String getUserSessionToken(BaseFeed feed);
52-
5351
/**
5452
* Follow a feed.
5553
*

stream-repo-apache/src/main/java/io/getstream/client/apache/StreamClientImpl.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.getstream.client.repo.StreamPersonalizedRepository;
2121
import io.getstream.client.repo.StreamRepository;
2222
import io.getstream.client.util.InfoUtil;
23+
import io.getstream.client.util.JwtAuthenticationUtil;
2324
import org.apache.http.client.config.RequestConfig;
2425
import org.apache.http.impl.client.CloseableHttpClient;
2526
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
@@ -39,13 +40,18 @@ public class StreamClientImpl implements StreamClient {
3940
private FeedFactory feedFactory;
4041
private final StreamRepository streamRepository;
4142
private final Optional<StreamPersonalizedRepository> streamPersonalizedRepository;
43+
private final String apiKey;
44+
private final String secretKey;
4245

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

4851
public StreamClientImpl(final ClientConfiguration clientConfiguration, final String key, final String secretKey) {
52+
this.apiKey = key;
53+
this.secretKey = secretKey;
54+
4955
Preconditions.checkNotNull(clientConfiguration, "Client configuration cannot be null.");
5056
AuthenticationHandlerConfiguration authenticationHandlerConfiguration = new AuthenticationHandlerConfiguration();
5157
authenticationHandlerConfiguration.setApiKey(checkNotNull(key, "API key cannot be null."));
@@ -70,6 +76,11 @@ public PersonalizedFeed newPersonalizedFeed(final String feedSlug,
7076
return this.feedFactory.createPersonalizedFeed(feedSlug, id);
7177
}
7278

79+
@Override
80+
public String getUserSessionToken(String userId) {
81+
return JwtAuthenticationUtil.generateToken(getSecretKey(), null, userId);
82+
}
83+
7384
@Override
7485
public void shutdown() throws IOException {
7586
this.streamRepository.shutdown();
@@ -123,4 +134,12 @@ private CloseableHttpClient initClient(final ClientConfiguration config,
123134
public static ObjectMapper getObjectMapper() {
124135
return OBJECT_MAPPER;
125136
}
137+
138+
public String getApiKey() {
139+
return apiKey;
140+
}
141+
142+
public String getSecretKey() {
143+
return secretKey;
144+
}
126145
}

stream-repo-apache/src/main/java/io/getstream/client/apache/repo/StreamRepositoryImpl.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ public String getReadOnlyToken(BaseFeed feed) {
8383
return JwtAuthenticationUtil.generateToken(secretKey, "read", "*", feed.getFeedSlug().concat(feed.getUserId()), null);
8484
}
8585

86-
@Override
87-
public String getUserSessionToken(BaseFeed feed) {
88-
return JwtAuthenticationUtil.generateToken(secretKey, null, feed.getUserId());
89-
}
90-
9186
@Override
9287
public void follow(BaseFeed feed, String targetFeedId, int activityCopyLimit) throws StreamClientException, IOException {
9388
HttpPost request = new HttpPost(UriBuilder.fromEndpoint(baseEndpoint)

stream-repo-apache/src/test/java/io/getstream/client/apache/IntegrationTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ public void shouldGetReadOnlyToken() throws IOException, StreamClientException,
8181
@Test
8282
public void shouldGetUserSessionToken() throws StreamClientException {
8383
StreamClient streamClient = new StreamClientImpl(CLIENT_CONFIGURATION, API_KEY, API_SECRET);
84-
Feed feed = streamClient.newFeed("feedslug", "aUserId");
85-
86-
Map<String, Claim> map = verifyToken(feed.getUserSessionToken());
84+
Map<String, Claim> map = verifyToken(streamClient.getUserSessionToken("aUserId"));
8785
assertThat(map.get("user_id").asString(), is("aUserId"));
8886
}
8987

stream-repo-okhttp/src/main/java/io/getstream/client/okhttp/StreamClientImpl.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package io.getstream.client.okhttp;
22

3-
import static com.google.common.base.Preconditions.checkNotNull;
4-
5-
import java.io.IOException;
6-
import java.util.Properties;
7-
import java.util.concurrent.TimeUnit;
8-
93
import com.fasterxml.jackson.databind.DeserializationFeature;
104
import com.fasterxml.jackson.databind.ObjectMapper;
115
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
@@ -26,25 +20,37 @@
2620
import io.getstream.client.repo.StreamPersonalizedRepository;
2721
import io.getstream.client.repo.StreamRepository;
2822
import io.getstream.client.util.InfoUtil;
23+
import io.getstream.client.util.JwtAuthenticationUtil;
2924
import okhttp3.ConnectionPool;
3025
import okhttp3.Interceptor;
3126
import okhttp3.OkHttpClient;
3227
import okhttp3.Response;
3328

29+
import java.io.IOException;
30+
import java.util.Properties;
31+
import java.util.concurrent.TimeUnit;
32+
33+
import static com.google.common.base.Preconditions.checkNotNull;
34+
3435
public class StreamClientImpl implements StreamClient {
3536

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

3839
private final Optional<StreamPersonalizedRepository> streamPersonalizedRepository;
3940
private FeedFactory feedFactory;
4041
private final StreamRepository streamRepository;
42+
private final String apiKey;
43+
private final String secretKey;
4144

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

4750
public StreamClientImpl(final ClientConfiguration clientConfiguration, final String key, final String secretKey) {
51+
this.apiKey = key;
52+
this.secretKey = secretKey;
53+
4854
Preconditions.checkNotNull(clientConfiguration, "Client configuration cannot be null.");
4955
AuthenticationHandlerConfiguration authenticationHandlerConfiguration = new AuthenticationHandlerConfiguration();
5056
authenticationHandlerConfiguration.setApiKey(checkNotNull(key, "API key cannot be null."));
@@ -69,6 +75,11 @@ public PersonalizedFeed newPersonalizedFeed(final String feedSlug,
6975
return this.feedFactory.createPersonalizedFeed(feedSlug, id);
7076
}
7177

78+
@Override
79+
public String getUserSessionToken(String userId) {
80+
return JwtAuthenticationUtil.generateToken(getSecretKey(), null, userId);
81+
}
82+
7283
@Override
7384
public void shutdown() throws IOException {
7485
this.streamRepository.shutdown();
@@ -124,4 +135,12 @@ private String getUserAgent() {
124135
public static ObjectMapper getObjectMapper() {
125136
return OBJECT_MAPPER;
126137
}
138+
139+
public String getApiKey() {
140+
return apiKey;
141+
}
142+
143+
public String getSecretKey() {
144+
return secretKey;
145+
}
127146
}

stream-repo-okhttp/src/main/java/io/getstream/client/okhttp/repo/StreamRepositoryImpl.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@ public String getToken(BaseFeed feed) {
8686
return StreamRepoUtils.createFeedToken(feed, secretKey);
8787
}
8888

89-
@Override
90-
public String getUserSessionToken(BaseFeed feed) {
91-
return JwtAuthenticationUtil.generateToken(secretKey, null, feed.getUserId());
92-
}
93-
9489
@Override
9590
public void follow(BaseFeed feed, String targetFeedId, int activityCopyLimit) throws StreamClientException, IOException {
9691
Request.Builder requestBuilder = new Request.Builder().url(UriBuilder.fromEndpoint(baseEndpoint)

stream-repo-okhttp/src/test/java/io/getstream/client/okhttp/IntegrationTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ public void shouldGetReadOnlyToken() throws StreamClientException {
7171
@Test
7272
public void shouldGetUserSessionToken() throws StreamClientException {
7373
StreamClient streamClient = new StreamClientImpl(CLIENT_CONFIGURATION, API_KEY, API_SECRET);
74-
Feed feed = streamClient.newFeed("feedslug", "aUserId");
75-
76-
Map<String, Claim> map = verifyToken(feed.getUserSessionToken());
74+
Map<String, Claim> map = verifyToken(streamClient.getUserSessionToken("aUserId"));
7775
assertThat(map.get("user_id").asString(), is("aUserId"));
7876
}
7977

0 commit comments

Comments
 (0)