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 @@ -48,6 +48,11 @@ 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,6 +39,12 @@ 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,6 +48,8 @@ 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
@@ -1,11 +1,11 @@
package io.getstream.client.util;

import java.io.UnsupportedEncodingException;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;

import java.io.UnsupportedEncodingException;

/**
* Utility class to generate a JWT token.
*/
Expand All @@ -28,8 +28,41 @@ public class JwtAuthenticationUtil {
public static String generateToken(final String secretKey, final String action, final String resource, final String feedId, final String userId) {
JWTCreator.Builder jwtBuilder = JWT.create();

jwtBuilder = jwtBuilder.withClaim("action", action);
jwtBuilder = jwtBuilder.withClaim("resource", resource);
if (null != action) {
jwtBuilder = jwtBuilder.withClaim("action", action);
}

if (null != resource) {
jwtBuilder = jwtBuilder.withClaim("resource", resource);
}

if (null != feedId) {
jwtBuilder = jwtBuilder.withClaim("feed_id", feedId);
}

if (null != userId) {
jwtBuilder = jwtBuilder.withClaim("user_id", userId);
}

try {
Algorithm algorithm = Algorithm.HMAC256(secretKey);

return jwtBuilder.sign(algorithm);
} catch (UnsupportedEncodingException exc) {
throw new IllegalStateException("Fatal error: JWT Algorithm unsupported.");
}
}

/**
* Generate JWT token.
* @param secretKey API Secret
* @param feedId FeedId (if null it will not be added to the payload)
* @param userId UserId (if null it will not be added to the payload)
* @return Token string
*/
public static String generateToken(final String secretKey, final String feedId, final String userId) {
JWTCreator.Builder jwtBuilder = JWT.create();

if (null != feedId) {
jwtBuilder = jwtBuilder.withClaim("feed_id", feedId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ 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 @@ -78,6 +78,15 @@ public void shouldGetReadOnlyToken() throws IOException, StreamClientException,
assertThat(map.get("resource").asString(), is(ALL));
}

@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());
assertThat(map.get("user_id").asString(), is("aUserId"));
}

@Test
public void shouldGetFollowers() throws IOException, StreamClientException {
StreamClient streamClient = new StreamClientImpl(CLIENT_CONFIGURATION, API_KEY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ 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 @@ -68,6 +68,15 @@ public void shouldGetReadOnlyToken() throws StreamClientException {
assertThat(map.get("resource").asString(), is(ALL));
}

@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());
assertThat(map.get("user_id").asString(), is("aUserId"));
}

@Test
public void shouldGetFollowers() throws IOException, StreamClientException {
StreamClient streamClient = new StreamClientImpl(CLIENT_CONFIGURATION, API_KEY,
Expand Down