diff --git a/DynamicKey/AgoraDynamicKey/cpp/sample/RtcTokenBuilder2Sample.cpp b/DynamicKey/AgoraDynamicKey/cpp/sample/RtcTokenBuilder2Sample.cpp index e3d013a4..baf6c167 100644 --- a/DynamicKey/AgoraDynamicKey/cpp/sample/RtcTokenBuilder2Sample.cpp +++ b/DynamicKey/AgoraDynamicKey/cpp/sample/RtcTokenBuilder2Sample.cpp @@ -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; } diff --git a/DynamicKey/AgoraDynamicKey/cpp/src/RtcTokenBuilder2.h b/DynamicKey/AgoraDynamicKey/cpp/src/RtcTokenBuilder2.h index d7f8e6f4..52a27aa3 100644 --- a/DynamicKey/AgoraDynamicKey/cpp/src/RtcTokenBuilder2.h +++ b/DynamicKey/AgoraDynamicKey/cpp/src/RtcTokenBuilder2.h @@ -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, @@ -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 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 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 diff --git a/DynamicKey/AgoraDynamicKey/csharp/src/AgoraIO/TokenBuilders/RtcTokenBuilder2.cs b/DynamicKey/AgoraDynamicKey/csharp/src/AgoraIO/TokenBuilders/RtcTokenBuilder2.cs index a1df807d..88725d36 100644 --- a/DynamicKey/AgoraDynamicKey/csharp/src/AgoraIO/TokenBuilders/RtcTokenBuilder2.cs +++ b/DynamicKey/AgoraDynamicKey/csharp/src/AgoraIO/TokenBuilders/RtcTokenBuilder2.cs @@ -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(); + } } } diff --git a/DynamicKey/AgoraDynamicKey/csharp/test/AgoraIO.Tests/RtcTokenBuilder2Test.cs b/DynamicKey/AgoraDynamicKey/csharp/test/AgoraIO.Tests/RtcTokenBuilder2Test.cs index 05026b5a..bae7877c 100644 --- a/DynamicKey/AgoraDynamicKey/csharp/test/AgoraIO.Tests/RtcTokenBuilder2Test.cs +++ b/DynamicKey/AgoraDynamicKey/csharp/test/AgoraIO.Tests/RtcTokenBuilder2Test.cs @@ -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) @@ -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); + } } } \ No newline at end of file diff --git a/DynamicKey/AgoraDynamicKey/deno/index.d.ts b/DynamicKey/AgoraDynamicKey/deno/index.d.ts index 19deac5b..406606a8 100644 --- a/DynamicKey/AgoraDynamicKey/deno/index.d.ts +++ b/DynamicKey/AgoraDynamicKey/deno/index.d.ts @@ -213,6 +213,193 @@ export namespace RtcTokenBuilder { tokenExpire: number, privilegeExpire: number ): string + + /** + * Generates an RTC token with the specified privilege. + * + * This method supports generating a token with the following privileges: + * - Joining an RTC channel. + * - Publishing audio in an RTC channel. + * - Publishing video in an RTC channel. + * - Publishing data streams in an RTC channel. + * + * The privileges for publishing audio, video, and data streams in an RTC channel apply only if you have + * enabled co-host authentication. + * + * A user can have multiple privileges. Each privilege is valid for a maximum of 24 hours. + * The SDK triggers the onTokenPrivilegeWillExpire and onRequestToken callbacks when the token is about to expire + * or has expired. The callbacks do not report the specific privilege affected, and you need to maintain + * the respective timestamp for each privilege in your app logic. After receiving the callback, you need + * to generate a new token, and then call renewToken to pass the new token to the SDK, or call joinChannel to re-join + * the channel. + * + * @note + * Agora recommends setting a reasonable timestamp for each privilege according to your scenario. + * Suppose the expiration timestamp for joining the channel is set earlier than that for publishing audio. + * When the token for joining the channel expires, the user is immediately kicked off the RTC channel + * and cannot publish any audio stream, even though the timestamp for publishing audio has not expired. + * + * @param appId The App ID of your Agora project. + * @param appCertificate The App Certificate of your Agora project. + * @param channelName The unique channel name for the Agora RTC session in string format. The string length must be less than 64 bytes. The channel name may contain the following characters: + * - All lowercase English letters: a to z. + * - All uppercase English letters: A to Z. + * - All numeric characters: 0 to 9. + * - The space character. + * - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",". + * @param uid The user ID. A 32-bit unsigned integer with a value range from 1 to (2^32 - 1). It must be unique. Set uid as 0, if you do not want to authenticate the user ID, that is, any uid from the app client can join the channel. + * @param tokenExpire 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 tokenExpire 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). + * @return The RTC Token + */ + export function buildTokenWithUidAndPrivilege( + appId: string, + appCertificate: string, + channelName: string, + uid: string | number, + tokenExpire: number, + joinChannelPrivilegeExpire: number, + pubAudioPrivilegeExpire: number, + pubVideoPrivilegeExpire: number, + pubDataStreamPrivilegeExpire: number + ): string + + /** + * Generates an RTC token with the specified privilege. + * + * This method supports generating a token with the following privileges: + * - Joining an RTC channel. + * - Publishing audio in an RTC channel. + * - Publishing video in an RTC channel. + * - Publishing data streams in an RTC channel. + * + * The privileges for publishing audio, video, and data streams in an RTC channel apply only if you have + * enabled co-host authentication. + * + * A user can have multiple privileges. Each privilege is valid for a maximum of 24 hours. + * The SDK triggers the onTokenPrivilegeWillExpire and onRequestToken callbacks when the token is about to expire + * or has expired. The callbacks do not report the specific privilege affected, and you need to maintain + * the respective timestamp for each privilege in your app logic. After receiving the callback, you need + * to generate a new token, and then call renewToken to pass the new token to the SDK, or call joinChannel to re-join + * the channel. + * + * @note + * Agora recommends setting a reasonable timestamp for each privilege according to your scenario. + * Suppose the expiration timestamp for joining the channel is set earlier than that for publishing audio. + * When the token for joining the channel expires, the user is immediately kicked off the RTC channel + * and cannot publish any audio stream, even though the timestamp for publishing audio has not expired. + * + * @param appId The App ID of your Agora project. + * @param appCertificate The App Certificate of your Agora project. + * @param channelName The unique channel name for the Agora RTC session in string format. The string length must be less than 64 bytes. The channel name may contain the following characters: + * - All lowercase English letters: a to z. + * - All uppercase English letters: A to Z. + * - All numeric characters: 0 to 9. + * - The space character. + * - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",". + * @param userAccount The user account. + * @param tokenExpire 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 tokenExpire 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). + * @return The RTC Token. + */ + export function BuildTokenWithUserAccountAndPrivilege( + appId: string, + appCertificate: string, + channelName: string, + account: string | number, + tokenExpire: number, + joinChannelPrivilegeExpire: number, + pubAudioPrivilegeExpire: number, + pubVideoPrivilegeExpire: number, + pubDataStreamPrivilegeExpire: number + ): string + + /** + * Build an RTC and RTM token with account. + * @param {*} appId The App ID issued to you by Agora. + * @param {*} appCertificate Certificate of the application that you registered in the Agora Dashboard. + * @param {*} channelName The 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 {*} account The user account. + * @param {*} role See #userRole. + * - Role.PUBLISHER; RECOMMENDED. Use this role for a voice/video call or a live broadcast. + * - Role.SUBSCRIBER: 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 Co-host for you. Otherwise, Role_Subscriber still has the same privileges as Role_Publisher. + * @param {*} tokenExpire epresented 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 tokenExpire as 600(seconds) + * @param {*} privilegeExpire represented by the number of seconds elapsed since now. If, for example, you want to enable your privilege for 10 minutes, set privilegeExpire as 600(seconds). + * @return The RTC and RTM Token. + */ + export function buildTokenWithRtm( + appId: string, + appCertificate: string, + channelName: string, + account: string | number, + role: number, + tokenExpire: number, + privilegeExpire: number + ): string + + /** + * Build an RTC and RTM token with account. + * @param {*} appId The App ID issued to you by Agora. + * @param {*} appCertificate Certificate of the application that you registered in the Agora Dashboard. + * @param {*} channelName The 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 {*} rtcAccount The RTC user's account, max length is 255 Bytes. + * @param {*} rtcRole See #userRole. + * - Role.PUBLISHER; RECOMMENDED. Use this role for a voice/video call or a live broadcast. + * - Role.SUBSCRIBER: 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 Co-host for you. Otherwise, Role_Subscriber still has the same privileges as Role_Publisher. + * @param {*} rtcTokenExpire epresented 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 tokenExpire 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. + */ + export function buildTokenWithRtm2( + appId: string, + appCertificate: string, + channelName: string, + rtcAccount: string | number, + rtcRole: number, + rtcTokenExpire: number, + joinChannelPrivilegeExpire: number, + pubAudioPrivilegeExpire: number, + pubVideoPrivilegeExpire: number, + pubDataStreamPrivilegeExpire: number, + rtmUserId: string | number, + rtmTokenExpire: number + ): string } export namespace RtmTokenBuilder { diff --git a/DynamicKey/AgoraDynamicKey/deno/sample/RtcTokenBuilder2Sample.js b/DynamicKey/AgoraDynamicKey/deno/sample/RtcTokenBuilder2Sample.js index e0a93b26..d3f7404e 100644 --- a/DynamicKey/AgoraDynamicKey/deno/sample/RtcTokenBuilder2Sample.js +++ b/DynamicKey/AgoraDynamicKey/deno/sample/RtcTokenBuilder2Sample.js @@ -9,9 +9,12 @@ const channelName = '7d72365eb983485397e3e3f9d460bdda' const uid = 2882341273 const account = '2882341273' const role = RtcRole.PUBLISHER -const expirationInSeconds = 3600 const tokenExpirationInSecond = 3600 const privilegeExpirationInSecond = 3600 +const joinChannelPrivilegeExpireInSeconds = 3600 +const pubAudioPrivilegeExpireInSeconds = 3600 +const pubVideoPrivilegeExpireInSeconds = 3600 +const pubDataStreamPrivilegeExpireInSeconds = 3600 console.log('App Id:', appId) console.log('App Certificate:', appCertificate) @@ -41,11 +44,11 @@ const tokenWithUidAndPrivilege = RtcTokenBuilder.buildTokenWithUidAndPrivilege( appCertificate, channelName, uid, - expirationInSeconds, - expirationInSeconds, - expirationInSeconds, - expirationInSeconds, - expirationInSeconds, + tokenExpirationInSecond, + joinChannelPrivilegeExpireInSeconds, + pubAudioPrivilegeExpireInSeconds, + pubVideoPrivilegeExpireInSeconds, + pubDataStreamPrivilegeExpireInSeconds, ) console.log('Token with int uid and privilege:', tokenWithUidAndPrivilege) @@ -54,11 +57,11 @@ const tokenWithUserAccountAndPrivilege = RtcTokenBuilder.BuildTokenWithUserAccou appCertificate, channelName, account, - expirationInSeconds, - expirationInSeconds, - expirationInSeconds, - expirationInSeconds, - expirationInSeconds, + tokenExpirationInSecond, + joinChannelPrivilegeExpireInSeconds, + pubAudioPrivilegeExpireInSeconds, + pubVideoPrivilegeExpireInSeconds, + pubDataStreamPrivilegeExpireInSeconds, ) console.log('Token with user account and privilege:', tokenWithUserAccountAndPrivilege) @@ -72,3 +75,19 @@ const tokenWithRtm = RtcTokenBuilder.buildTokenWithRtm( privilegeExpirationInSecond, ) console.log('Token with RTM:', tokenWithRtm) + +const tokenWithRtm2 = RtcTokenBuilder.buildTokenWithRtm2( + appId, + appCertificate, + channelName, + account, + role, + tokenExpirationInSecond, + joinChannelPrivilegeExpireInSeconds, + pubAudioPrivilegeExpireInSeconds, + pubVideoPrivilegeExpireInSeconds, + pubDataStreamPrivilegeExpireInSeconds, + account, + tokenExpirationInSecond, +) +console.log('Token with RTM:', tokenWithRtm2) diff --git a/DynamicKey/AgoraDynamicKey/deno/src/RtcTokenBuilder2.js b/DynamicKey/AgoraDynamicKey/deno/src/RtcTokenBuilder2.js index 3747e914..5318f6d5 100644 --- a/DynamicKey/AgoraDynamicKey/deno/src/RtcTokenBuilder2.js +++ b/DynamicKey/AgoraDynamicKey/deno/src/RtcTokenBuilder2.js @@ -16,7 +16,7 @@ const Role = { * to enable authentication for Hosting-in for you. Otherwise, Role_Subscriber * still has the same privileges as Role_Publisher. */ - SUBSCRIBER: 2 + SUBSCRIBER: 2, } class RtcTokenBuilder { @@ -46,7 +46,7 @@ class RtcTokenBuilder { uid, role, tokenExpire, - privilegeExpire + privilegeExpire, ) } @@ -75,7 +75,7 @@ class RtcTokenBuilder { account, role, tokenExpire, - privilegeExpire = 0 + privilegeExpire = 0, ) { let token = new AccessToken(appId, appCertificate, 0, tokenExpire) @@ -146,7 +146,7 @@ class RtcTokenBuilder { joinChannelPrivilegeExpire, pubAudioPrivilegeExpire, pubVideoPrivilegeExpire, - pubDataStreamPrivilegeExpire + pubDataStreamPrivilegeExpire, ) { return this.BuildTokenWithUserAccountAndPrivilege( appId, @@ -157,7 +157,7 @@ class RtcTokenBuilder { joinChannelPrivilegeExpire, pubAudioPrivilegeExpire, pubVideoPrivilegeExpire, - pubDataStreamPrivilegeExpire + pubDataStreamPrivilegeExpire, ) } @@ -216,7 +216,7 @@ class RtcTokenBuilder { joinChannelPrivilegeExpire, pubAudioPrivilegeExpire, pubVideoPrivilegeExpire, - pubDataStreamPrivilegeExpire + pubDataStreamPrivilegeExpire, ) { let token = new AccessToken(appId, appCertificate, 0, tokenExpire) @@ -266,6 +266,66 @@ class RtcTokenBuilder { return token.build() } + + /** + * Build an RTC and RTM token with account. + * @param {*} appId The App ID issued to you by Agora. + * @param {*} appCertificate Certificate of the application that you registered in the Agora Dashboard. + * @param {*} channelName The 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 {*} rtcAccount The RTC user's account, max length is 255 Bytes. + * @param {*} rtcRole See #userRole. + * - Role.PUBLISHER; RECOMMENDED. Use this role for a voice/video call or a live broadcast. + * - Role.SUBSCRIBER: 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 Co-host for you. Otherwise, Role_Subscriber still has the same privileges as Role_Publisher. + * @param {*} rtcTokenExpire epresented 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 tokenExpire 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. + */ + static buildTokenWithRtm2( + appId, + appCertificate, + channelName, + rtcAccount, + rtcRole, + rtcTokenExpire, + joinChannelPrivilegeExpire, + pubAudioPrivilegeExpire, + pubVideoPrivilegeExpire, + pubDataStreamPrivilegeExpire, + rtmUserId, + rtmTokenExpire, + ) { + let token = new AccessToken(appId, appCertificate, 0, rtcTokenExpire) + + let serviceRtc = new ServiceRtc(channelName, rtcAccount) + serviceRtc.add_privilege(ServiceRtc.kPrivilegeJoinChannel, joinChannelPrivilegeExpire) + if (rtcRole == Role.PUBLISHER) { + serviceRtc.add_privilege(ServiceRtc.kPrivilegePublishAudioStream, pubAudioPrivilegeExpire) + serviceRtc.add_privilege(ServiceRtc.kPrivilegePublishVideoStream, pubVideoPrivilegeExpire) + serviceRtc.add_privilege(ServiceRtc.kPrivilegePublishDataStream, pubDataStreamPrivilegeExpire) + } + token.add_service(serviceRtc) + + let serviceRtm = new ServiceRtm(rtmUserId) + serviceRtm.add_privilege(ServiceRtm.kPrivilegeLogin, rtmTokenExpire) + token.add_service(serviceRtm) + + return token.build() + } } export { Role, RtcTokenBuilder } diff --git a/DynamicKey/AgoraDynamicKey/go/sample/rtctokenbuilder2/sample.go b/DynamicKey/AgoraDynamicKey/go/sample/rtctokenbuilder2/sample.go index 1cadbc89..f0485f09 100644 --- a/DynamicKey/AgoraDynamicKey/go/sample/rtctokenbuilder2/sample.go +++ b/DynamicKey/AgoraDynamicKey/go/sample/rtctokenbuilder2/sample.go @@ -66,4 +66,11 @@ func main() { } else { fmt.Printf("Token with RTM: %s\n", result) } + + result, err = rtctokenbuilder.BuildTokenWithRtm2(appId, appCertificate, channelName, uidStr, rtctokenbuilder.RolePublisher, tokenExpirationInSeconds, privilegeExpirationInSeconds, pubAudioPrivilegeExpireInSeconds, pubVideoPrivilegeExpireInSeconds, pubDataStreamPrivilegeExpireInSeconds, uidStr, tokenExpirationInSeconds) + if err != nil { + fmt.Println(err) + } else { + fmt.Printf("Token with RTM: %s\n", result) + } } diff --git a/DynamicKey/AgoraDynamicKey/go/src/rtctokenbuilder2/rtctokenbuilder.go b/DynamicKey/AgoraDynamicKey/go/src/rtctokenbuilder2/rtctokenbuilder.go index 5319ef48..7e987d7c 100644 --- a/DynamicKey/AgoraDynamicKey/go/src/rtctokenbuilder2/rtctokenbuilder.go +++ b/DynamicKey/AgoraDynamicKey/go/src/rtctokenbuilder2/rtctokenbuilder.go @@ -219,3 +219,49 @@ func BuildTokenWithRtm(appId string, appCertificate string, channelName string, return token.Build() } + +// BuildTokenWithRtm2 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: RolePublisher: A broadcaster/host in a live-broadcast profile. +// RoleSubscriber: 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. +func BuildTokenWithRtm2(appId string, appCertificate string, channelName string, rtcAccount string, rtcRole Role, rtcTokenExpire uint32, + joinChannelPrivilegeExpire uint32, pubAudioPrivilegeExpire uint32, pubVideoPrivilegeExpire uint32, pubDataStreamPrivilegeExpire uint32, + rtmUserId string, rtmTokenExpire uint32) (string, error) { + token := accesstoken.NewAccessToken(appId, appCertificate, rtcTokenExpire) + + serviceRtc := accesstoken.NewServiceRtc(channelName, rtcAccount) + serviceRtc.AddPrivilege(accesstoken.PrivilegeJoinChannel, joinChannelPrivilegeExpire) + if rtcRole == RolePublisher { + serviceRtc.AddPrivilege(accesstoken.PrivilegePublishAudioStream, pubAudioPrivilegeExpire) + serviceRtc.AddPrivilege(accesstoken.PrivilegePublishVideoStream, pubVideoPrivilegeExpire) + serviceRtc.AddPrivilege(accesstoken.PrivilegePublishDataStream, pubDataStreamPrivilegeExpire) + } + token.AddService(serviceRtc) + + serviceRtm := accesstoken.NewServiceRtm(rtmUserId) + serviceRtm.AddPrivilege(accesstoken.PrivilegeLogin, rtmTokenExpire) + token.AddService(serviceRtm) + + return token.Build() +} diff --git a/DynamicKey/AgoraDynamicKey/java/pom.xml b/DynamicKey/AgoraDynamicKey/java/pom.xml index 69619020..21600bef 100644 --- a/DynamicKey/AgoraDynamicKey/java/pom.xml +++ b/DynamicKey/AgoraDynamicKey/java/pom.xml @@ -5,7 +5,7 @@ io.agora authentication - 2.1.2 + 2.1.3 Agora Authentication Agora Authentication https://github.com/AgoraIO/Tools diff --git a/DynamicKey/AgoraDynamicKey/java/src/main/java/io/agora/media/RtcTokenBuilder2.java b/DynamicKey/AgoraDynamicKey/java/src/main/java/io/agora/media/RtcTokenBuilder2.java index 152499e5..293a75da 100644 --- a/DynamicKey/AgoraDynamicKey/java/src/main/java/io/agora/media/RtcTokenBuilder2.java +++ b/DynamicKey/AgoraDynamicKey/java/src/main/java/io/agora/media/RtcTokenBuilder2.java @@ -244,4 +244,59 @@ public String buildTokenWithRtm(String appId, String appCertificate, String chan return ""; } } + + /** + * 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 64 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 String buildTokenWithRtm2(String appId, String appCertificate, String channelName, String rtcAccount, Role rtcRole, int rtcTokenExpire, + int joinChannelPrivilegeExpire, int pubAudioPrivilegeExpire, int pubVideoPrivilegeExpire, int pubDataStreamPrivilegeExpire, String rtmUserId, + int rtmTokenExpire) { + AccessToken2 accessToken = new AccessToken2(appId, appCertificate, rtcTokenExpire); + AccessToken2.Service serviceRtc = new AccessToken2.ServiceRtc(channelName, rtcAccount); + + serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtc.PRIVILEGE_JOIN_CHANNEL, joinChannelPrivilegeExpire); + if (rtcRole == Role.ROLE_PUBLISHER) { + serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtc.PRIVILEGE_PUBLISH_AUDIO_STREAM, pubAudioPrivilegeExpire); + serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtc.PRIVILEGE_PUBLISH_VIDEO_STREAM, pubVideoPrivilegeExpire); + serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtc.PRIVILEGE_PUBLISH_DATA_STREAM, pubDataStreamPrivilegeExpire); + } + accessToken.addService(serviceRtc); + + AccessToken2.Service serviceRtm = new AccessToken2.ServiceRtm(rtmUserId); + + serviceRtm.addPrivilegeRtm(AccessToken2.PrivilegeRtm.PRIVILEGE_LOGIN, rtmTokenExpire); + accessToken.addService(serviceRtm); + + try { + return accessToken.build(); + } catch (Exception e) { + e.printStackTrace(); + return ""; + } + } } diff --git a/DynamicKey/AgoraDynamicKey/java/src/main/java/io/agora/sample/RtcTokenBuilder2Sample.java b/DynamicKey/AgoraDynamicKey/java/src/main/java/io/agora/sample/RtcTokenBuilder2Sample.java index 34cfb45d..ab95afb4 100644 --- a/DynamicKey/AgoraDynamicKey/java/src/main/java/io/agora/sample/RtcTokenBuilder2Sample.java +++ b/DynamicKey/AgoraDynamicKey/java/src/main/java/io/agora/sample/RtcTokenBuilder2Sample.java @@ -48,5 +48,9 @@ public static void main(String[] args) { privilegeExpirationInSeconds); System.out.printf("Token with RTM: %s\n", result); + result = token.buildTokenWithRtm2(appId, appCertificate, channelName, account, Role.ROLE_PUBLISHER, tokenExpirationInSeconds, + joinChannelPrivilegeExpireInSeconds, pubAudioPrivilegeExpireInSeconds, pubVideoPrivilegeExpireInSeconds, pubDataStreamPrivilegeExpireInSeconds, + account, tokenExpirationInSeconds); + System.out.printf("Token with RTM: %s\n", result); } } diff --git a/DynamicKey/AgoraDynamicKey/lua/examples/rtc_token_builder.lua b/DynamicKey/AgoraDynamicKey/lua/examples/rtc_token_builder.lua index ab71cd67..a5897c75 100644 --- a/DynamicKey/AgoraDynamicKey/lua/examples/rtc_token_builder.lua +++ b/DynamicKey/AgoraDynamicKey/lua/examples/rtc_token_builder.lua @@ -114,3 +114,26 @@ if status then else print("Error: " .. token) end + +status, token = pcall(function() + return rtc_token_builder.build_token_with_rtm2( + app_id, + app_certificate, + channel_name, + uid_str, + rtc_token_builder.ROLE_PUBLISHER, + token_expiration_in_seconds, + join_channel_privilege_expire_in_seconds, + pub_audio_privilege_expire_in_seconds, + pub_video_privilege_expire_in_seconds, + pub_data_stream_privilege_expire_in_seconds, + uid_str, + token_expiration_in_seconds + ) +end) + +if status then + print("Token with RTM: " .. token) +else + print("Error: " .. token) +end diff --git a/DynamicKey/AgoraDynamicKey/lua/src/rtc_token_builder.lua b/DynamicKey/AgoraDynamicKey/lua/src/rtc_token_builder.lua index 606f8efd..e820e53d 100644 --- a/DynamicKey/AgoraDynamicKey/lua/src/rtc_token_builder.lua +++ b/DynamicKey/AgoraDynamicKey/lua/src/rtc_token_builder.lua @@ -186,12 +186,60 @@ local function build_token_with_rtm(app_id, app_certificate, channel_name, accou return token:build() end +-- Build the RTC and RTM token with account. +-- +-- app_id: 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. +-- app_certificate: Certificate of the application that you registered in +-- the Agora Dashboard. See Get an App Certificate. +-- channel_name: Unique channel name for the AgoraRTC session in the string format +-- rtc_account: The RTC user's account, max length is 255 Bytes. +-- rtc_role: RolePublisher: A broadcaster/host in a live-broadcast profile. +-- RoleSubscriber: An audience(default) in a live-broadcast profile. +-- 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). +-- 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). +-- 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). +-- 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). +-- 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). +-- rtm_user_id: The RTM user's account, max length is 255 Bytes. +-- 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. +local function build_token_with_rtm2(app_id, app_certificate, channel_name, rtc_account, rtc_role, rtc_token_expire, + join_channel_privilege_expire, pub_audio_privilege_expire, + pub_video_privilege_expire, pub_data_stream_privilege_expire, + rtm_user_id, rtm_token_expire) + local token = access_token.new_access_token(app_id, app_certificate, rtc_token_expire) + + local service_rtc = access_token.new_service_rtc(channel_name, rtc_account) + service_rtc.service:add_privilege(access_token.PRIVILEGE_JOIN_CHANNEL, join_channel_privilege_expire) + if rtc_role == ROLE_PUBLISHER then + service_rtc.service:add_privilege(access_token.PRIVILEGE_PUBLISH_AUDIO_STREAM, pub_audio_privilege_expire) + service_rtc.service:add_privilege(access_token.PRIVILEGE_PUBLISH_VIDEO_STREAM, pub_video_privilege_expire) + service_rtc.service:add_privilege(access_token.PRIVILEGE_PUBLISH_DATA_STREAM, pub_data_stream_privilege_expire) + end + token:add_service(service_rtc) + + local service_rtm = access_token.new_service_rtm(rtm_user_id) + service_rtm.service:add_privilege(access_token.PRIVILEGE_LOGIN, rtm_token_expire) + token:add_service(service_rtm) + + return token:build() +end + return { build_token_with_uid = build_token_with_uid, build_token_with_user_account = build_token_with_user_account, build_token_with_uid_and_privilege = build_token_with_uid_and_privilege, build_token_with_user_account_and_privilege = build_token_with_user_account_and_privilege, build_token_with_rtm = build_token_with_rtm, + build_token_with_rtm2 = build_token_with_rtm2, ROLE_PUBLISHER = ROLE_PUBLISHER, ROLE_SUBSCRIBER = ROLE_SUBSCRIBER, } diff --git a/DynamicKey/AgoraDynamicKey/nodejs/index.d.ts b/DynamicKey/AgoraDynamicKey/nodejs/index.d.ts index 961a6d66..602d8012 100644 --- a/DynamicKey/AgoraDynamicKey/nodejs/index.d.ts +++ b/DynamicKey/AgoraDynamicKey/nodejs/index.d.ts @@ -357,6 +357,49 @@ export namespace RtcTokenBuilder { tokenExpire: number, privilegeExpire: number ): string + + /** + * Build an RTC and RTM token with account. + * @param {*} appId The App ID issued to you by Agora. + * @param {*} appCertificate Certificate of the application that you registered in the Agora Dashboard. + * @param {*} channelName The 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 {*} rtcAccount The RTC user's account, max length is 255 Bytes. + * @param {*} rtcRole See #userRole. + * - Role.PUBLISHER; RECOMMENDED. Use this role for a voice/video call or a live broadcast. + * - Role.SUBSCRIBER: 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 Co-host for you. Otherwise, Role_Subscriber still has the same privileges as Role_Publisher. + * @param {*} rtcTokenExpire epresented 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 tokenExpire 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. + */ + export function buildTokenWithRtm2( + appId: string, + appCertificate: string, + channelName: string, + rtcAccount: string | number, + rtcRole: number, + rtcTokenExpire: number, + joinChannelPrivilegeExpire: number, + pubAudioPrivilegeExpire: number, + pubVideoPrivilegeExpire: number, + pubDataStreamPrivilegeExpire: number, + rtmUserId: string | number, + rtmTokenExpire: number + ): string } export namespace RtmTokenBuilder { diff --git a/DynamicKey/AgoraDynamicKey/nodejs/package.json b/DynamicKey/AgoraDynamicKey/nodejs/package.json index 36555c28..8c06ac6f 100644 --- a/DynamicKey/AgoraDynamicKey/nodejs/package.json +++ b/DynamicKey/AgoraDynamicKey/nodejs/package.json @@ -1,6 +1,6 @@ { "name": "agora-token", - "version": "2.0.4", + "version": "2.0.5", "description": "", "homepage": "https://github.com/AgoraIO/Tools/tree/master/DynamicKey/AgoraDynamicKey/nodejs", "main": "index.js", diff --git a/DynamicKey/AgoraDynamicKey/nodejs/sample/RtcTokenBuilder2Sample.js b/DynamicKey/AgoraDynamicKey/nodejs/sample/RtcTokenBuilder2Sample.js index 8165aa6b..4cb31772 100644 --- a/DynamicKey/AgoraDynamicKey/nodejs/sample/RtcTokenBuilder2Sample.js +++ b/DynamicKey/AgoraDynamicKey/nodejs/sample/RtcTokenBuilder2Sample.js @@ -84,3 +84,19 @@ const tokenWithRtm = RtcTokenBuilder.buildTokenWithRtm( privilegeExpirationInSecond ) console.log('Token with RTM:', tokenWithRtm) + +const tokenWithRtm2 = RtcTokenBuilder.buildTokenWithRtm2( + appId, + appCertificate, + channelName, + account, + role, + tokenExpirationInSecond, + joinChannelPrivilegeExpireInSeconds, + pubAudioPrivilegeExpireInSeconds, + pubVideoPrivilegeExpireInSeconds, + pubDataStreamPrivilegeExpireInSeconds, + account, + tokenExpirationInSecond +) +console.log('Token with RTM:', tokenWithRtm2) diff --git a/DynamicKey/AgoraDynamicKey/nodejs/src/RtcTokenBuilder2.js b/DynamicKey/AgoraDynamicKey/nodejs/src/RtcTokenBuilder2.js index 709d36a5..5cb98f51 100644 --- a/DynamicKey/AgoraDynamicKey/nodejs/src/RtcTokenBuilder2.js +++ b/DynamicKey/AgoraDynamicKey/nodejs/src/RtcTokenBuilder2.js @@ -268,6 +268,66 @@ class RtcTokenBuilder { return token.build() } + + /** + * Build an RTC and RTM token with account. + * @param {*} appId The App ID issued to you by Agora. + * @param {*} appCertificate Certificate of the application that you registered in the Agora Dashboard. + * @param {*} channelName The 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 {*} rtcAccount The RTC user's account, max length is 255 Bytes. + * @param {*} rtcRole See #userRole. + * - Role.PUBLISHER; RECOMMENDED. Use this role for a voice/video call or a live broadcast. + * - Role.SUBSCRIBER: 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 Co-host for you. Otherwise, Role_Subscriber still has the same privileges as Role_Publisher. + * @param {*} rtcTokenExpire epresented 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 tokenExpire 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. + */ + static buildTokenWithRtm2( + appId, + appCertificate, + channelName, + rtcAccount, + rtcRole, + rtcTokenExpire, + joinChannelPrivilegeExpire, + pubAudioPrivilegeExpire, + pubVideoPrivilegeExpire, + pubDataStreamPrivilegeExpire, + rtmUserId, + rtmTokenExpire + ) { + let token = new AccessToken(appId, appCertificate, 0, rtcTokenExpire) + + let serviceRtc = new ServiceRtc(channelName, rtcAccount) + serviceRtc.add_privilege(ServiceRtc.kPrivilegeJoinChannel, joinChannelPrivilegeExpire) + if (rtcRole == Role.PUBLISHER) { + serviceRtc.add_privilege(ServiceRtc.kPrivilegePublishAudioStream, pubAudioPrivilegeExpire) + serviceRtc.add_privilege(ServiceRtc.kPrivilegePublishVideoStream, pubVideoPrivilegeExpire) + serviceRtc.add_privilege(ServiceRtc.kPrivilegePublishDataStream, pubDataStreamPrivilegeExpire) + } + token.add_service(serviceRtc) + + let serviceRtm = new ServiceRtm(rtmUserId) + serviceRtm.add_privilege(ServiceRtm.kPrivilegeLogin, rtmTokenExpire) + token.add_service(serviceRtm) + + return token.build() + } } module.exports.RtcTokenBuilder = RtcTokenBuilder diff --git a/DynamicKey/AgoraDynamicKey/php/sample/RtcTokenBuilder2Sample.php b/DynamicKey/AgoraDynamicKey/php/sample/RtcTokenBuilder2Sample.php index 490d44a7..c6859198 100644 --- a/DynamicKey/AgoraDynamicKey/php/sample/RtcTokenBuilder2Sample.php +++ b/DynamicKey/AgoraDynamicKey/php/sample/RtcTokenBuilder2Sample.php @@ -37,3 +37,19 @@ $token = RtcTokenBuilder2::buildTokenWithRtm($appId, $appCertificate, $channelName, $uidStr, RtcTokenBuilder2::ROLE_PUBLISHER, $tokenExpirationInSeconds, $privilegeExpirationInSeconds); echo 'Token with RTM: ' . $token . PHP_EOL; + +$token = RtcTokenBuilder2::buildTokenWithRtm2( + $appId, + $appCertificate, + $channelName, + $uidStr, + RtcTokenBuilder2::ROLE_PUBLISHER, + $tokenExpirationInSeconds, + $joinChannelPrivilegeExpireInSeconds, + $pubAudioPrivilegeExpireInSeconds, + $pubVideoPrivilegeExpireInSeconds, + $pubDataStreamPrivilegeExpireInSeconds, + $uidStr, + $tokenExpirationInSeconds +); +echo 'Token with RTM: ' . $token . PHP_EOL; diff --git a/DynamicKey/AgoraDynamicKey/php/src/RtcTokenBuilder2.php b/DynamicKey/AgoraDynamicKey/php/src/RtcTokenBuilder2.php index 4d7f923c..3f1e9395 100644 --- a/DynamicKey/AgoraDynamicKey/php/src/RtcTokenBuilder2.php +++ b/DynamicKey/AgoraDynamicKey/php/src/RtcTokenBuilder2.php @@ -247,4 +247,62 @@ public static function buildTokenWithRtm($appId, $appCertificate, $channelName, return $token->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 $tokenExpire 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 function buildTokenWithRtm2( + $appId, + $appCertificate, + $channelName, + $rtcAccount, + $rtcRole, + $rtcTokenExpire, + $joinChannelPrivilegeExpire, + $pubAudioPrivilegeExpire, + $pubVideoPrivilegeExpire, + $pubDataStreamPrivilegeExpire, + $rtmUserId, + $rtmTokenExpire + ) { + $token = new AccessToken2($appId, $appCertificate, $rtcTokenExpire); + $serviceRtc = new ServiceRtc($channelName, $rtcAccount); + + $serviceRtc->addPrivilege($serviceRtc::PRIVILEGE_JOIN_CHANNEL, $joinChannelPrivilegeExpire); + if ($rtcRole == self::ROLE_PUBLISHER) { + $serviceRtc->addPrivilege($serviceRtc::PRIVILEGE_PUBLISH_AUDIO_STREAM, $pubAudioPrivilegeExpire); + $serviceRtc->addPrivilege($serviceRtc::PRIVILEGE_PUBLISH_VIDEO_STREAM, $pubVideoPrivilegeExpire); + $serviceRtc->addPrivilege($serviceRtc::PRIVILEGE_PUBLISH_DATA_STREAM, $pubDataStreamPrivilegeExpire); + } + $token->addService($serviceRtc); + + $serviceRtm = new ServiceRtm($rtmUserId); + + $serviceRtm->addPrivilege($serviceRtm::PRIVILEGE_LOGIN, $rtmTokenExpire); + $token->addService($serviceRtm); + + return $token->build(); + } } diff --git a/DynamicKey/AgoraDynamicKey/python/sample/RtcTokenBuilder2Sample.py b/DynamicKey/AgoraDynamicKey/python/sample/RtcTokenBuilder2Sample.py index 556739ce..ffd12069 100644 --- a/DynamicKey/AgoraDynamicKey/python/sample/RtcTokenBuilder2Sample.py +++ b/DynamicKey/AgoraDynamicKey/python/sample/RtcTokenBuilder2Sample.py @@ -53,6 +53,12 @@ def main(): token = RtcTokenBuilder.build_token_with_rtm(app_id, app_certificate, channel_name, account, Role_Publisher, token_expiration_in_seconds, privilege_expiration_in_seconds) print("Token with RTM: {}".format(token)) + token = RtcTokenBuilder.build_token_with_rtm2( + app_id, app_certificate, channel_name, account, Role_Publisher, 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) + print("Token with RTM: {}".format(token)) + if __name__ == "__main__": main() diff --git a/DynamicKey/AgoraDynamicKey/python/src/RtcTokenBuilder2.py b/DynamicKey/AgoraDynamicKey/python/src/RtcTokenBuilder2.py index 8fc1ef44..6080d853 100755 --- a/DynamicKey/AgoraDynamicKey/python/src/RtcTokenBuilder2.py +++ b/DynamicKey/AgoraDynamicKey/python/src/RtcTokenBuilder2.py @@ -220,3 +220,49 @@ def build_token_with_rtm(app_id, app_certificate, channel_name, account, role, t token.add_service(rtm_service) return token.build() + + @staticmethod + def build_token_with_rtm2(app_id, app_certificate, channel_name, rtc_account, rtc_role, rtc_token_expire, + join_channel_privilege_expire, pub_audio_privilege_expire, pub_video_privilege_expire, pub_data_stream_privilege_expire, + rtm_user_id, rtm_token_expire): + """ + Build the RTC and RTM token with account. + :param app_id: 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 app_certificate: Certificate of the application that you registered in the Agora Dashboard. + See Get an App Certificate. + :param channel_name: Unique channel name for the AgoraRTC session in the string format. + :param rtc_account: The RTC user's account, max length is 255 Bytes. + :param rtc_role: Role_Publisher: A broadcaster/host in a live-broadcast profile. + Role_Subscriber: An audience(default) in a live-broadcast profile. + :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. + """ + token = AccessToken(app_id, app_certificate, expire=rtc_token_expire) + rtc_service = ServiceRtc(channel_name, rtc_account) + + rtc_service.add_privilege(ServiceRtc.kPrivilegeJoinChannel, join_channel_privilege_expire) + if rtc_role == Role_Publisher: + rtc_service.add_privilege(ServiceRtc.kPrivilegePublishAudioStream, pub_audio_privilege_expire) + rtc_service.add_privilege(ServiceRtc.kPrivilegePublishVideoStream, pub_video_privilege_expire) + rtc_service.add_privilege(ServiceRtc.kPrivilegePublishDataStream, pub_data_stream_privilege_expire) + token.add_service(rtc_service) + + rtm_service = ServiceRtm(rtm_user_id) + + rtm_service.add_privilege(ServiceRtm.kPrivilegeLogin, rtm_token_expire) + token.add_service(rtm_service) + + return token.build() diff --git a/DynamicKey/AgoraDynamicKey/python3/sample/RtcTokenBuilder2Sample.py b/DynamicKey/AgoraDynamicKey/python3/sample/RtcTokenBuilder2Sample.py index 01af7c62..66b81697 100644 --- a/DynamicKey/AgoraDynamicKey/python3/sample/RtcTokenBuilder2Sample.py +++ b/DynamicKey/AgoraDynamicKey/python3/sample/RtcTokenBuilder2Sample.py @@ -53,6 +53,11 @@ def main(): token_expiration_in_seconds, privilege_expiration_in_seconds) print("Token with RTM: {}".format(token)) + token = RtcTokenBuilder.build_token_with_rtm2( + app_id, app_certificate, channel_name, account, Role_Publisher, 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) + print("Token with RTM: {}".format(token)) if __name__ == "__main__": main() diff --git a/DynamicKey/AgoraDynamicKey/python3/src/RtcTokenBuilder2.py b/DynamicKey/AgoraDynamicKey/python3/src/RtcTokenBuilder2.py index 4fd10031..a88c592e 100755 --- a/DynamicKey/AgoraDynamicKey/python3/src/RtcTokenBuilder2.py +++ b/DynamicKey/AgoraDynamicKey/python3/src/RtcTokenBuilder2.py @@ -215,3 +215,49 @@ def build_token_with_rtm(app_id, app_certificate, channel_name, account, role, t token.add_service(rtm_service) return token.build() + + @staticmethod + def build_token_with_rtm2(app_id, app_certificate, channel_name, rtc_account, rtc_role, rtc_token_expire, + join_channel_privilege_expire, pub_audio_privilege_expire, pub_video_privilege_expire, pub_data_stream_privilege_expire, + rtm_user_id, rtm_token_expire): + """ + Build the RTC and RTM token with account. + :param app_id: 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 app_certificate: Certificate of the application that you registered in the Agora Dashboard. + See Get an App Certificate. + :param channel_name: Unique channel name for the AgoraRTC session in the string format. + :param rtc_account: The RTC user's account, max length is 255 Bytes. + :param rtc_role: Role_Publisher: A broadcaster/host in a live-broadcast profile. + Role_Subscriber: An audience(default) in a live-broadcast profile. + :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. + """ + token = AccessToken(app_id, app_certificate, expire=rtc_token_expire) + rtc_service = ServiceRtc(channel_name, rtc_account) + + rtc_service.add_privilege(ServiceRtc.kPrivilegeJoinChannel, join_channel_privilege_expire) + if rtc_role == Role_Publisher: + rtc_service.add_privilege(ServiceRtc.kPrivilegePublishAudioStream, pub_audio_privilege_expire) + rtc_service.add_privilege(ServiceRtc.kPrivilegePublishVideoStream, pub_video_privilege_expire) + rtc_service.add_privilege(ServiceRtc.kPrivilegePublishDataStream, pub_data_stream_privilege_expire) + token.add_service(rtc_service) + + rtm_service = ServiceRtm(rtm_user_id) + + rtm_service.add_privilege(ServiceRtm.kPrivilegeLogin, rtm_token_expire) + token.add_service(rtm_service) + + return token.build() diff --git a/DynamicKey/AgoraDynamicKey/ruby/lib/dynamic_key2/rtc_token_builder.rb b/DynamicKey/AgoraDynamicKey/ruby/lib/dynamic_key2/rtc_token_builder.rb index 2372a5e6..bc41eada 100644 --- a/DynamicKey/AgoraDynamicKey/ruby/lib/dynamic_key2/rtc_token_builder.rb +++ b/DynamicKey/AgoraDynamicKey/ruby/lib/dynamic_key2/rtc_token_builder.rb @@ -40,8 +40,7 @@ def self.build_token_with_uid(app_id, app_certificate, channel_name, uid, role, # app_certificate: Certificate of the application that you registered in the Agora Dashboard. # See Get an App Certificate. # channel_name: Unique channel name for the AgoraRTC session in the string format. - # uid: User ID. A 32-bit unsigned integer with a value ranging from 1 to (2^32-1). - # uid must be unique. + # account: The user's account, max length is 255 Bytes. # role: ROLE_PUBLISHER: A broadcaster/host in a live-broadcast profile. # ROLE_SUBSCRIBER: An audience(default) in a live-broadcast profile. # token_expire: represented by the number of seconds elapsed since now. If, for example, @@ -182,8 +181,7 @@ def self.build_token_with_user_account_and_privilege(app_id, app_certificate, ch # app_certificate: Certificate of the application that you registered in the Agora Dashboard. # See Get an App Certificate. # channel_name: Unique channel name for the AgoraRTC session in the string format. - # uid: User ID. A 32-bit unsigned integer with a value ranging from 1 to (2^32-1). - # uid must be unique. + # account: The user's account, max length is 255 Bytes. # role: ROLE_PUBLISHER: A broadcaster/host in a live-broadcast profile. # ROLE_SUBSCRIBER: An audience(default) in a live-broadcast profile. # token_expire: represented by the number of seconds elapsed since now. If, for example, @@ -211,5 +209,51 @@ def self.build_token_with_rtm(app_id, app_certificate, channel_name, account, ro access_token.build end + + # Build the RTC and RTM token with account. + # + # app_id: 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. + # app_certificate: Certificate of the application that you registered in the Agora Dashboard. + # See Get an App Certificate. + # channel_name: Unique channel name for the AgoraRTC session in the string format. + # rtc_account: The RTC user's account, max length is 255 Bytes. + # rtc_role: ROLE_PUBLISHER: A broadcaster/host in a live-broadcast profile. + # ROLE_SUBSCRIBER: An audience(default) in a live-broadcast profile. + # 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 token_expire as 600(seconds). + # 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). + # 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). + # 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). + # 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). + # rtm_user_id: The RTM user's account, max length is 255 Bytes. + # 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. + def self.build_token_with_rtm2(app_id, app_certificate, channel_name, rtc_account, rtc_role, rtc_token_expire, + join_channel_privilege_expire, pub_audio_privilege_expire, pub_video_privilege_expire, pub_data_stream_privilege_expire, + rtm_user_id, rtm_token_expire) + access_token = AgoraDynamicKey2::AccessToken.new(app_id, app_certificate, rtc_token_expire) + service_rtc = AgoraDynamicKey2::ServiceRtc.new(channel_name, rtc_account) + + service_rtc.add_privilege(AgoraDynamicKey2::ServiceRtc::PRIVILEGE_JOIN_CHANNEL, join_channel_privilege_expire) + if rtc_role == ROLE_PUBLISHER + service_rtc.add_privilege(AgoraDynamicKey2::ServiceRtc::PRIVILEGE_PUBLISH_AUDIO_STREAM, pub_audio_privilege_expire) + service_rtc.add_privilege(AgoraDynamicKey2::ServiceRtc::PRIVILEGE_PUBLISH_VIDEO_STREAM, pub_video_privilege_expire) + service_rtc.add_privilege(AgoraDynamicKey2::ServiceRtc::PRIVILEGE_PUBLISH_DATA_STREAM, pub_data_stream_privilege_expire) + end + access_token.add_service(service_rtc) + + service_rtm = AgoraDynamicKey2::ServiceRtm.new(rtm_user_id) + + service_rtm.add_privilege(AgoraDynamicKey2::ServiceRtm::PRIVILEGE_JOIN_LOGIN, rtm_token_expire) + access_token.add_service(service_rtm) + + access_token.build + end end end diff --git a/DynamicKey/AgoraDynamicKey/ruby/sample/rtc_token_builder2_sample.rb b/DynamicKey/AgoraDynamicKey/ruby/sample/rtc_token_builder2_sample.rb index 9245d2b3..1e798aa7 100644 --- a/DynamicKey/AgoraDynamicKey/ruby/sample/rtc_token_builder2_sample.rb +++ b/DynamicKey/AgoraDynamicKey/ruby/sample/rtc_token_builder2_sample.rb @@ -53,3 +53,10 @@ AgoraDynamicKey2::RtcTokenBuilder::ROLE_PUBLISHER, token_expiration_in_seconds, privilege_expiration_in_seconds ) puts "Token with RTM: #{token}" + +token = AgoraDynamicKey2::RtcTokenBuilder.build_token_with_rtm2( + app_id, app_certificate, channel_name, account, AgoraDynamicKey2::RtcTokenBuilder::ROLE_PUBLISHER, + 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 +) +puts "Token with RTM: #{token}" diff --git a/DynamicKey/AgoraDynamicKey/rust/examples/rtc_token_builder.rs b/DynamicKey/AgoraDynamicKey/rust/examples/rtc_token_builder.rs index af78cfba..ae9d518b 100644 --- a/DynamicKey/AgoraDynamicKey/rust/examples/rtc_token_builder.rs +++ b/DynamicKey/AgoraDynamicKey/rust/examples/rtc_token_builder.rs @@ -92,4 +92,22 @@ fn main() { Ok(result) => println!("Token with RTM: {}", result), Err(err) => println!("{}", err), } + + match rtc_token_builder::build_token_with_rtm2( + &app_id, + &app_certificate, + &channel_name, + &uid_str, + rtc_token_builder::ROLE_PUBLISHER, + token_expiration_in_seconds, + join_channel_privilege_expire_in_seconds, + pub_audio_privilege_expire_in_seconds, + pub_video_privilege_expire_in_seconds, + pub_data_stream_privilege_expire_in_seconds, + &uid_str, + token_expiration_in_seconds, + ) { + Ok(result) => println!("Token with RTM: {}", result), + Err(err) => println!("{}", err), + } } diff --git a/DynamicKey/AgoraDynamicKey/rust/src/rtc_token_builder.rs b/DynamicKey/AgoraDynamicKey/rust/src/rtc_token_builder.rs index 74704cbb..b9f04ee0 100644 --- a/DynamicKey/AgoraDynamicKey/rust/src/rtc_token_builder.rs +++ b/DynamicKey/AgoraDynamicKey/rust/src/rtc_token_builder.rs @@ -192,3 +192,49 @@ pub fn build_token_with_rtm( return token.build(); } + +// Build the RTC and RTM token with account. +// +// app_id: 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. +// app_certificate: Certificate of the application that you registered in +// the Agora Dashboard. See Get an App Certificate. +// channel_name: Unique channel name for the AgoraRTC session in the string format +// rtc_account: The RTC user's account, max length is 255 Bytes. +// rtc_role: RolePublisher: A broadcaster/host in a live-broadcast profile. +// RoleSubscriber: An audience(default) in a live-broadcast profile. +// 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). +// 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). +// 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). +// 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). +// 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). +// rtm_user_id: The RTM user's account, max length is 255 Bytes. +// 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. +pub fn build_token_with_rtm2( + app_id: &str, app_certificate: &str, channel_name: &str, rtc_account: &str, rtc_role: Role, rtc_token_expire: u32, join_channel_privilege_expire: u32, + pub_audio_privilege_expire: u32, pub_video_privilege_expire: u32, pub_data_stream_privilege_expire: u32, rtm_user_id: &str, rtm_token_expire: u32, +) -> Result> { + let mut token = access_token::new_access_token(app_id, app_certificate, rtc_token_expire); + + let mut service_rtc = access_token::new_service_rtc(channel_name, rtc_account); + service_rtc.service.add_privilege(access_token::PRIVILEGE_JOIN_CHANNEL, join_channel_privilege_expire); + if rtc_role == ROLE_PUBLISHER { + service_rtc.service.add_privilege(access_token::PRIVILEGE_PUBLISH_AUDIO_STREAM, pub_audio_privilege_expire); + service_rtc.service.add_privilege(access_token::PRIVILEGE_PUBLISH_VIDEO_STREAM, pub_video_privilege_expire); + service_rtc.service.add_privilege(access_token::PRIVILEGE_PUBLISH_DATA_STREAM, pub_data_stream_privilege_expire); + } + token.add_service(Box::new(service_rtc)); + + let mut service_rtm = access_token::new_service_rtm(rtm_user_id); + service_rtm.service.add_privilege(access_token::PRIVILEGE_LOGIN, rtm_token_expire); + token.add_service(Box::new(service_rtm)); + + return token.build(); +}