Skip to content

Commit c381f05

Browse files
authored
Merge pull request #420 from AgoraIO/feat_ADC-5107
2 parents e9f86c3 + fada29c commit c381f05

28 files changed

Lines changed: 1022 additions & 23 deletions

File tree

DynamicKey/AgoraDynamicKey/cpp/sample/RtcTokenBuilder2Sample.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,11 @@ int main(int argc, char const *argv[]) {
6060
privilege_expiration_in_seconds);
6161
std::cout << "Token With RTM:" << result << std::endl;
6262

63+
result = RtcTokenBuilder2::BuildTokenWithRtm2(app_id, app_certificate, channel_name, account, UserRole::kRolePublisher, token_expiration_in_seconds,
64+
join_channel_privilege_expiration_in_seconds, pub_audio_privilege_expiration_in_seconds,
65+
pub_video_privilege_expiration_in_seconds, pub_data_stream_privilege_expiration_in_seconds, account,
66+
token_expiration_in_seconds);
67+
std::cout << "Token With RTM:" << result << std::endl;
68+
6369
return 0;
6470
}

DynamicKey/AgoraDynamicKey/cpp/src/RtcTokenBuilder2.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,51 @@ class RtcTokenBuilder2 {
273273
*/
274274
static std::string BuildTokenWithRtm(const std::string& app_id, const std::string& app_certificate, const std::string& channel_name,
275275
const std::string& user_account, UserRole role, uint32_t token_expire, uint32_t privilege_expire = 0);
276+
277+
/**
278+
* Builds an RTC and RTM token with account.
279+
*
280+
* @param app_id The App ID issued to you by Agora.
281+
* @param app_certificate Certificate of the application that you registered in
282+
* the Agora Dashboard.
283+
* @param channel_name Unique channel name for the AgoraRTC session in the
284+
* string format. The string length must be less than 64 bytes. Supported
285+
* character scopes are:
286+
* - The 26 lowercase English letters: a to z.
287+
* - The 26 uppercase English letters: A to Z.
288+
* - The 10 digits: 0 to 9.
289+
* - The space.
290+
* - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">",
291+
* "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
292+
* @param rtc_account The user account.
293+
* @param rtc_role See #userRole.
294+
* - Role_Publisher = 1: RECOMMENDED. Use this role for a voice/video call or a
295+
* live broadcast.
296+
* - Role_Subscriber = 2: ONLY use this role if your live-broadcast scenario
297+
* requires authentication for
298+
* [Co-host](https://docs.agora.io/en/video-calling/get-started/authentication-workflow?#co-host-token-authentication).
299+
* In order for this role to take effect, please contact our support team to
300+
* enable authentication for Hosting-in for you. Otherwise, Role_Subscriber
301+
* still has the same privileges as Role_Publisher.
302+
* @param rtc_token_expire represented by the number of seconds elapsed since now.
303+
* 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).
304+
* @param join_channel_privilege_expire represented by the number of seconds elapsed since now.
305+
* 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).
306+
* @param pub_audio_privilege_expire represented by the number of seconds elapsed since now.
307+
* If, for example, you want to enable publish audio privilege for 10 minutes, set pub_audio_privilege_expire as 600(seconds).
308+
* @param pub_video_privilege_expire represented by the number of seconds elapsed since now.
309+
* If, for example, you want to enable publish video privilege for 10 minutes, set pub_video_privilege_expire as 600(seconds).
310+
* @param pub_data_stream_privilege_expire represented by the number of seconds elapsed since now.
311+
* If, for example, you want to enable publish data stream privilege for 10 minutes, set pub_data_stream_privilege_expire as 600(seconds).
312+
* @param rtm_user_id The RTM user's account, max length is 255 Bytes.
313+
* @param rtm_token_expire represented by the number of seconds elapsed since now.
314+
* 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).
315+
@return The RTC and RTM token.
316+
*/
317+
static std::string BuildTokenWithRtm2(const std::string& app_id, const std::string& app_certificate, const std::string& channel_name,
318+
const std::string& rtc_account, UserRole rtc_role, uint32_t rtc_token_expire, uint32_t join_channel_privilege_expire,
319+
uint32_t pub_audio_privilege_expire, uint32_t pub_video_privilege_expire, uint32_t pub_data_stream_privilege_expire,
320+
const std::string& rtm_user_id, uint32_t rtm_token_expire);
276321
};
277322

278323
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
349394

350395
return token.Build();
351396
}
397+
398+
inline std::string RtcTokenBuilder2::BuildTokenWithRtm2(const std::string& app_id, const std::string& app_certificate, const std::string& channel_name,
399+
const std::string& rtc_account, UserRole rtc_role, uint32_t rtc_token_expire,
400+
uint32_t join_channel_privilege_expire, uint32_t pub_audio_privilege_expire,
401+
uint32_t pub_video_privilege_expire, uint32_t pub_data_stream_privilege_expire,
402+
const std::string& rtm_user_id, uint32_t rtm_token_expire) {
403+
std::unique_ptr<Service> rtc_service(new ServiceRtc(channel_name, rtc_account));
404+
AccessToken2 token(app_id, app_certificate, 0, rtc_token_expire);
405+
406+
rtc_service->AddPrivilege(ServiceRtc::kPrivilegeJoinChannel, join_channel_privilege_expire);
407+
if (rtc_role == UserRole::kRolePublisher) {
408+
rtc_service->AddPrivilege(ServiceRtc::kPrivilegePublishAudioStream, pub_audio_privilege_expire);
409+
rtc_service->AddPrivilege(ServiceRtc::kPrivilegePublishVideoStream, pub_video_privilege_expire);
410+
rtc_service->AddPrivilege(ServiceRtc::kPrivilegePublishDataStream, pub_data_stream_privilege_expire);
411+
}
412+
token.AddService(std::move(rtc_service));
413+
414+
std::unique_ptr<Service> rtm_service(new ServiceRtm(rtm_user_id));
415+
rtm_service->AddPrivilege(ServiceRtm::kPrivilegeLogin, rtm_token_expire);
416+
417+
token.AddService(std::move(rtm_service));
418+
419+
return token.Build();
420+
}
352421
} // namespace tools
353422
} // namespace agora

DynamicKey/AgoraDynamicKey/csharp/src/AgoraIO/TokenBuilders/RtcTokenBuilder2.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,54 @@ public static string buildTokenWithRtm(string appId, string appCertificate, stri
234234

235235
return accessToken.build();
236236
}
237+
238+
/**
239+
* Build the RTC and RTM token with account.
240+
*
241+
* @param appId: The App ID issued to you by Agora. Apply for a new App ID from
242+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
243+
* @param appCertificate: Certificate of the application that you registered in
244+
* the Agora Dashboard. See Get an App Certificate.
245+
* @param channelName: Unique channel name for the AgoraRTC session in the string format
246+
* @param rtcAccount: The RTC user's account, max length is 255 Bytes.
247+
* @param rtcRole: ROLE_PUBLISHER: A broadcaster/host in a live-broadcast profile.
248+
* ROLE_SUBSCRIBER: An audience(default) in a live-broadcast profile.
249+
* @param rtcTokenExpire: represented by the number of seconds elapsed since now.
250+
* If, for example, you want to access the Agora Service within 10 minutes after the token is generated, set rtcTokenExpire as 600(seconds).
251+
* @param joinChannelPrivilegeExpire: represented by the number of seconds elapsed since now.
252+
* If, for example, you want to join channel and expect stay in the channel for 10 minutes, set joinChannelPrivilegeExpire as 600(seconds).
253+
* @param pubAudioPrivilegeExpire: represented by the number of seconds elapsed since now.
254+
* If, for example, you want to enable publish audio privilege for 10 minutes, set pubAudioPrivilegeExpire as 600(seconds).
255+
* @param pubVideoPrivilegeExpire: represented by the number of seconds elapsed since now.
256+
* If, for example, you want to enable publish video privilege for 10 minutes, set pubVideoPrivilegeExpire as 600(seconds).
257+
* @param pubDataStreamPrivilegeExpire: represented by the number of seconds elapsed since now.
258+
* If, for example, you want to enable publish data stream privilege for 10 minutes, set pubDataStreamPrivilegeExpire as 600(seconds).
259+
* @param rtmUserId: The RTM user's account, max length is 255 Bytes.
260+
* @param rtmTokenExpire: represented by the number of seconds elapsed since now.
261+
If, for example, you want to access the Agora Service within 10 minutes after the token is generated, set rtmTokenExpire as 600(seconds).
262+
* @return The RTC and RTM token.
263+
*/
264+
public static string buildTokenWithRtm2(string appId, string appCertificate, string channelName, string rtcAccount, Role rtcRole, uint rtcTokenExpire,
265+
uint joinChannelPrivilegeExpire, uint pubAudioPrivilegeExpire, uint pubVideoPrivilegeExpire, uint pubDataStreamPrivilegeExpire, string rtmUserId, uint rtmTokenExpire)
266+
{
267+
AccessToken2 accessToken = new AccessToken2(appId, appCertificate, rtcTokenExpire);
268+
AccessToken2.Service serviceRtc = new AccessToken2.ServiceRtc(channelName, rtcAccount);
269+
270+
serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtcEnum.PRIVILEGE_JOIN_CHANNEL, joinChannelPrivilegeExpire);
271+
if (Role.RolePublisher == rtcRole)
272+
{
273+
serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtcEnum.PRIVILEGE_PUBLISH_AUDIO_STREAM, pubAudioPrivilegeExpire);
274+
serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtcEnum.PRIVILEGE_PUBLISH_VIDEO_STREAM, pubVideoPrivilegeExpire);
275+
serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtcEnum.PRIVILEGE_PUBLISH_DATA_STREAM, pubDataStreamPrivilegeExpire);
276+
}
277+
accessToken.addService(serviceRtc);
278+
279+
AccessToken2.Service serviceRtm = new AccessToken2.ServiceRtm(rtmUserId);
280+
281+
serviceRtm.addPrivilegeRtm(AccessToken2.PrivilegeRtmEnum.PRIVILEGE_LOGIN, rtmTokenExpire);
282+
accessToken.addService(serviceRtm);
283+
284+
return accessToken.build();
285+
}
237286
}
238287
}

DynamicKey/AgoraDynamicKey/csharp/test/AgoraIO.Tests/RtcTokenBuilder2Test.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public class RtcTokenBuilder2Test
1414
private uint _uid = 2882341273;
1515
private uint _tokenExpirationInSeconds = 3600;
1616
private uint _privilegeExpirationInSeconds = 3600;
17+
private uint _joinChannelPrivilegeExpireInSeconds = 3600;
18+
private uint _pubAudioPrivilegeExpireInSeconds = 3600;
19+
private uint _pubVideoPrivilegeExpireInSeconds = 3600;
20+
private uint _pubDataStreamPrivilegeExpireInSeconds = 3600;
21+
1722
protected readonly ITestOutputHelper Output;
1823

1924
public RtcTokenBuilder2Test(ITestOutputHelper tempOutput)
@@ -47,5 +52,15 @@ public void testbuildTokenWithRtm()
4752
Output.WriteLine(">> token");
4853
Output.WriteLine(token);
4954
}
55+
56+
[Fact]
57+
public void testbuildTokenWithRtm2()
58+
{
59+
string token = RtcTokenBuilder2.buildTokenWithRtm2(_appId, _appCertificate, _channelName, _account, RtcTokenBuilder2.Role.RolePublisher, _tokenExpirationInSeconds,
60+
_joinChannelPrivilegeExpireInSeconds, _pubAudioPrivilegeExpireInSeconds, _pubVideoPrivilegeExpireInSeconds, _pubDataStreamPrivilegeExpireInSeconds, _account, _tokenExpirationInSeconds);
61+
62+
Output.WriteLine(">> token");
63+
Output.WriteLine(token);
64+
}
5065
}
5166
}

0 commit comments

Comments
 (0)