Skip to content

Commit

Permalink
bugfix put expireInSeconds into token007 instead
Browse files Browse the repository at this point in the history
  • Loading branch information
kenuiuc committed Aug 20, 2021
1 parent 6520223 commit 00912f8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.easemob.im.server.api.util.Utilities;

import static com.easemob.im.server.api.util.Utilities.IT_TIMEOUT;
import static com.easemob.im.server.api.util.Utilities.toExpireOnSeconds;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down Expand Up @@ -71,8 +70,7 @@ public void userTokenTest() throws Exception {
token -> {
AccessToken2.ServiceRtc serviceRtc =
new AccessToken2.ServiceRtc(DUMMY_CHANNEL_NAME, DUMMY_UID);
serviceRtc.addPrivilegeRtc(DUMMY_RTC_PRIVILEGE,
toExpireOnSeconds(EXPIRE_IN_SECONDS));
serviceRtc.addPrivilegeRtc(DUMMY_RTC_PRIVILEGE, EXPIRE_IN_SECONDS);
token.addService(serviceRtc);
},
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,11 @@ public static Mono<Token> fetchUserTokenWithEasemobRealm(Context context,
* <p>
* 为用户 Bob 生成包含 AgoraChat 权限和 AgoraRTC (JOIN_CHANNEL) 权限的 Agora userToken, 有效期为600秒:
* <pre>{@code
* // please pay attention to expireInSeconds (Duration) vs. expireOnSeconds (Instant)
*
* int expireInSeconds = 600;
* int expireOnSeconds = Utilities.toExpireOnSeconds(expireInSeconds);
* EMUser bob = new EMUser("bob", "da921111-ecf9-11eb-9af3-296ff79acb67", true);
* String bobAgoraChatRtcToken = service.token().getUserToken(bob, expireInSeconds, token -> {
* String bobAgoraChatRtcToken = service.token().getUserToken(bob, 600, token -> {
* AccessToken2.ServiceRtc serviceRtc = new AccessToken2.ServiceRtc("dummyRtcChannelName", "dummyUid");
* serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtc.PRIVILEGE_JOIN_CHANNEL, expireOnSeconds);
* serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtc.PRIVILEGE_JOIN_CHANNEL, 600);
* token.addService(serviceRtc);
* }, null);
* }</pre>
Expand All @@ -102,12 +99,11 @@ public String getUserToken(EMUser user, Integer expireInSeconds,
String userId = user.getUuid();
String appId = properties.getAppId();
String appCert = properties.getAppCert();
int expireOnSeconds = Utilities.toExpireOnSeconds(expireInSeconds);

AccessToken2 accessToken = new AccessToken2(appId, appCert, expireOnSeconds);
AccessToken2 accessToken = new AccessToken2(appId, appCert, expireInSeconds);
AccessToken2.Service serviceChat = new AccessToken2.ServiceChat(userId);
serviceChat.addPrivilegeChat(AccessToken2.PrivilegeChat.PRIVILEGE_CHAT_USER,
expireOnSeconds);
expireInSeconds);
accessToken.addService(serviceChat);

if (tokenConfigurer != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public class AccessToken2 {
public Map<Short, Service> services = new TreeMap<>();
public AccessToken2() {
}

/**
*
* @param appId
* @param appCert
* @param expire expire Duration in seconds
*/
public AccessToken2(String appId, String appCert, int expire) {
this.appCert = appCert;
this.appId = appId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import reactor.netty.http.client.HttpClient;

import java.time.Duration;
import java.time.Instant;

import static com.easemob.im.server.api.util.Utilities.toExpireOnSeconds;

public class AgoraTokenProvider implements TokenProvider {

Expand Down Expand Up @@ -91,16 +88,11 @@ private Mono<Token> fetchEasemobToken(String appId, String appCert) {
}

private String buildAppToken(String appId, String appCert) {
int expireOnSeconds = toExpireOnSeconds(EXPIRE_IN_SECONDS);
Instant expireDate = Instant.ofEpochSecond(expireOnSeconds);
log.debug(
"buildingAppToken with expireInSeconds = {}, expireOnSeconds = {}, expireDate = {}",
EXPIRE_IN_SECONDS, expireOnSeconds, expireDate.toString());

AccessToken2 accessToken = new AccessToken2(appId, appCert, expireOnSeconds);
AccessToken2 accessToken = new AccessToken2(appId, appCert, EXPIRE_IN_SECONDS);
AccessToken2.Service serviceChat = new AccessToken2.ServiceChat();
serviceChat
.addPrivilegeChat(AccessToken2.PrivilegeChat.PRIVILEGE_CHAT_APP, expireOnSeconds);
.addPrivilegeChat(AccessToken2.PrivilegeChat.PRIVILEGE_CHAT_APP, EXPIRE_IN_SECONDS);
accessToken.addService(serviceChat);
try {
return accessToken.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public static String randomPassword() {
);
}

public static int toExpireOnSeconds(int expireInSeconds) {
return (int) Instant.now().plusSeconds(expireInSeconds).getEpochSecond();
}

public static String mask(String text) {
if (Strings.isBlank(text)) {
return text;
Expand Down

0 comments on commit 00912f8

Please sign in to comment.