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 @@ -60,5 +60,11 @@ int main(int argc, char const *argv[]) {
privilege_expiration_in_seconds);
std::cout << "Token With RTM:" << result << std::endl;

result = RtcTokenBuilder2::BuildTokenWithRtm2(app_id, app_certificate, channel_name, account, UserRole::kRolePublisher, token_expiration_in_seconds,
join_channel_privilege_expiration_in_seconds, pub_audio_privilege_expiration_in_seconds,
pub_video_privilege_expiration_in_seconds, pub_data_stream_privilege_expiration_in_seconds, account,
token_expiration_in_seconds);
std::cout << "Token With RTM:" << result << std::endl;

return 0;
}
69 changes: 69 additions & 0 deletions DynamicKey/AgoraDynamicKey/cpp/src/RtcTokenBuilder2.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,51 @@ class RtcTokenBuilder2 {
*/
static std::string BuildTokenWithRtm(const std::string& app_id, const std::string& app_certificate, const std::string& channel_name,
const std::string& user_account, UserRole role, uint32_t token_expire, uint32_t privilege_expire = 0);

/**
* Builds an RTC and RTM token with account.
*
* @param app_id The App ID issued to you by Agora.
* @param app_certificate Certificate of the application that you registered in
* the Agora Dashboard.
* @param channel_name Unique channel name for the AgoraRTC session in the
* string format. The string length must be less than 64 bytes. Supported
* character scopes are:
* - The 26 lowercase English letters: a to z.
* - The 26 uppercase English letters: A to Z.
* - The 10 digits: 0 to 9.
* - The space.
* - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">",
* "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
* @param rtc_account The user account.
* @param rtc_role See #userRole.
* - Role_Publisher = 1: RECOMMENDED. Use this role for a voice/video call or a
* live broadcast.
* - Role_Subscriber = 2: ONLY use this role if your live-broadcast scenario
* requires authentication for
* [Co-host](https://docs.agora.io/en/video-calling/get-started/authentication-workflow?#co-host-token-authentication).
* In order for this role to take effect, please contact our support team to
* enable authentication for Hosting-in for you. Otherwise, Role_Subscriber
* still has the same privileges as Role_Publisher.
* @param rtc_token_expire represented by the number of seconds elapsed since now.
* If, for example, you want to access the Agora Service within 10 minutes after the token is generated, set rtc_token_expire as 600(seconds).
* @param join_channel_privilege_expire represented by the number of seconds elapsed since now.
* If, for example, you want to join channel and expect stay in the channel for 10 minutes, set join_channel_privilege_expire as 600(seconds).
* @param pub_audio_privilege_expire represented by the number of seconds elapsed since now.
* If, for example, you want to enable publish audio privilege for 10 minutes, set pub_audio_privilege_expire as 600(seconds).
* @param pub_video_privilege_expire represented by the number of seconds elapsed since now.
* If, for example, you want to enable publish video privilege for 10 minutes, set pub_video_privilege_expire as 600(seconds).
* @param pub_data_stream_privilege_expire represented by the number of seconds elapsed since now.
* If, for example, you want to enable publish data stream privilege for 10 minutes, set pub_data_stream_privilege_expire as 600(seconds).
* @param rtm_user_id The RTM user's account, max length is 255 Bytes.
* @param rtm_token_expire represented by the number of seconds elapsed since now.
* If, for example, you want to access the Agora Service within 10 minutes after the token is generated, set rtm_token_expire as 600(seconds).
@return The RTC and RTM token.
*/
static std::string BuildTokenWithRtm2(const std::string& app_id, const std::string& app_certificate, const std::string& channel_name,
const std::string& rtc_account, UserRole rtc_role, uint32_t rtc_token_expire, uint32_t join_channel_privilege_expire,
uint32_t pub_audio_privilege_expire, uint32_t pub_video_privilege_expire, uint32_t pub_data_stream_privilege_expire,
const std::string& rtm_user_id, uint32_t rtm_token_expire);
};

inline std::string RtcTokenBuilder2::BuildTokenWithUid(const std::string& app_id, const std::string& app_certificate, const std::string& channel_name,
Expand Down Expand Up @@ -349,5 +394,29 @@ inline std::string RtcTokenBuilder2::BuildTokenWithRtm(const std::string& app_id

return token.Build();
}

inline std::string RtcTokenBuilder2::BuildTokenWithRtm2(const std::string& app_id, const std::string& app_certificate, const std::string& channel_name,
const std::string& rtc_account, UserRole rtc_role, uint32_t rtc_token_expire,
uint32_t join_channel_privilege_expire, uint32_t pub_audio_privilege_expire,
uint32_t pub_video_privilege_expire, uint32_t pub_data_stream_privilege_expire,
const std::string& rtm_user_id, uint32_t rtm_token_expire) {
std::unique_ptr<Service> rtc_service(new ServiceRtc(channel_name, rtc_account));
AccessToken2 token(app_id, app_certificate, 0, rtc_token_expire);

rtc_service->AddPrivilege(ServiceRtc::kPrivilegeJoinChannel, join_channel_privilege_expire);
if (rtc_role == UserRole::kRolePublisher) {
rtc_service->AddPrivilege(ServiceRtc::kPrivilegePublishAudioStream, pub_audio_privilege_expire);
rtc_service->AddPrivilege(ServiceRtc::kPrivilegePublishVideoStream, pub_video_privilege_expire);
rtc_service->AddPrivilege(ServiceRtc::kPrivilegePublishDataStream, pub_data_stream_privilege_expire);
}
token.AddService(std::move(rtc_service));

std::unique_ptr<Service> rtm_service(new ServiceRtm(rtm_user_id));
rtm_service->AddPrivilege(ServiceRtm::kPrivilegeLogin, rtm_token_expire);

token.AddService(std::move(rtm_service));

return token.Build();
}
} // namespace tools
} // namespace agora
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,54 @@ public static string buildTokenWithRtm(string appId, string appCertificate, stri

return accessToken.build();
}

/**
* Build the RTC and RTM token with account.
*
* @param appId: The App ID issued to you by Agora. Apply for a new App ID from
* Agora Dashboard if it is missing from your kit. See Get an App ID.
* @param appCertificate: Certificate of the application that you registered in
* the Agora Dashboard. See Get an App Certificate.
* @param channelName: Unique channel name for the AgoraRTC session in the string format
* @param rtcAccount: The RTC user's account, max length is 255 Bytes.
* @param rtcRole: ROLE_PUBLISHER: A broadcaster/host in a live-broadcast profile.
* ROLE_SUBSCRIBER: An audience(default) in a live-broadcast profile.
* @param rtcTokenExpire: represented by the number of seconds elapsed since now.
* If, for example, you want to access the Agora Service within 10 minutes after the token is generated, set rtcTokenExpire as 600(seconds).
* @param joinChannelPrivilegeExpire: represented by the number of seconds elapsed since now.
* If, for example, you want to join channel and expect stay in the channel for 10 minutes, set joinChannelPrivilegeExpire as 600(seconds).
* @param pubAudioPrivilegeExpire: represented by the number of seconds elapsed since now.
* If, for example, you want to enable publish audio privilege for 10 minutes, set pubAudioPrivilegeExpire as 600(seconds).
* @param pubVideoPrivilegeExpire: represented by the number of seconds elapsed since now.
* If, for example, you want to enable publish video privilege for 10 minutes, set pubVideoPrivilegeExpire as 600(seconds).
* @param pubDataStreamPrivilegeExpire: represented by the number of seconds elapsed since now.
* If, for example, you want to enable publish data stream privilege for 10 minutes, set pubDataStreamPrivilegeExpire as 600(seconds).
* @param rtmUserId: The RTM user's account, max length is 255 Bytes.
* @param rtmTokenExpire: represented by the number of seconds elapsed since now.
If, for example, you want to access the Agora Service within 10 minutes after the token is generated, set rtmTokenExpire as 600(seconds).
* @return The RTC and RTM token.
*/
public static string buildTokenWithRtm2(string appId, string appCertificate, string channelName, string rtcAccount, Role rtcRole, uint rtcTokenExpire,
uint joinChannelPrivilegeExpire, uint pubAudioPrivilegeExpire, uint pubVideoPrivilegeExpire, uint pubDataStreamPrivilegeExpire, string rtmUserId, uint rtmTokenExpire)
{
AccessToken2 accessToken = new AccessToken2(appId, appCertificate, rtcTokenExpire);
AccessToken2.Service serviceRtc = new AccessToken2.ServiceRtc(channelName, rtcAccount);

serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtcEnum.PRIVILEGE_JOIN_CHANNEL, joinChannelPrivilegeExpire);
if (Role.RolePublisher == rtcRole)
{
serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtcEnum.PRIVILEGE_PUBLISH_AUDIO_STREAM, pubAudioPrivilegeExpire);
serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtcEnum.PRIVILEGE_PUBLISH_VIDEO_STREAM, pubVideoPrivilegeExpire);
serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtcEnum.PRIVILEGE_PUBLISH_DATA_STREAM, pubDataStreamPrivilegeExpire);
}
accessToken.addService(serviceRtc);

AccessToken2.Service serviceRtm = new AccessToken2.ServiceRtm(rtmUserId);

serviceRtm.addPrivilegeRtm(AccessToken2.PrivilegeRtmEnum.PRIVILEGE_LOGIN, rtmTokenExpire);
accessToken.addService(serviceRtm);

return accessToken.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public class RtcTokenBuilder2Test
private uint _uid = 2882341273;
private uint _tokenExpirationInSeconds = 3600;
private uint _privilegeExpirationInSeconds = 3600;
private uint _joinChannelPrivilegeExpireInSeconds = 3600;
private uint _pubAudioPrivilegeExpireInSeconds = 3600;
private uint _pubVideoPrivilegeExpireInSeconds = 3600;
private uint _pubDataStreamPrivilegeExpireInSeconds = 3600;

protected readonly ITestOutputHelper Output;

public RtcTokenBuilder2Test(ITestOutputHelper tempOutput)
Expand Down Expand Up @@ -47,5 +52,15 @@ public void testbuildTokenWithRtm()
Output.WriteLine(">> token");
Output.WriteLine(token);
}

[Fact]
public void testbuildTokenWithRtm2()
{
string token = RtcTokenBuilder2.buildTokenWithRtm2(_appId, _appCertificate, _channelName, _account, RtcTokenBuilder2.Role.RolePublisher, _tokenExpirationInSeconds,
_joinChannelPrivilegeExpireInSeconds, _pubAudioPrivilegeExpireInSeconds, _pubVideoPrivilegeExpireInSeconds, _pubDataStreamPrivilegeExpireInSeconds, _account, _tokenExpirationInSeconds);

Output.WriteLine(">> token");
Output.WriteLine(token);
}
}
}
Loading