From 63039be1fc9bbab15038215f0eae4f39da9b2a6d Mon Sep 17 00:00:00 2001 From: Max Cobb Date: Tue, 25 Jul 2023 15:16:17 +0100 Subject: [PATCH] added extra parameter for rtm BuildToken. empty string gives same results as previously, other value adds rtc join and stream message priviliges go to output --- examples/rtmtokenbuilder/main.go | 2 +- rtmtokenbuilder/RtmTokenBuilder.go | 26 ++++++++++++++++++------- rtmtokenbuilder/rtmtokenbuilder_test.go | 19 +++++++++++++++++- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/examples/rtmtokenbuilder/main.go b/examples/rtmtokenbuilder/main.go index 68275e6..87564c3 100644 --- a/examples/rtmtokenbuilder/main.go +++ b/examples/rtmtokenbuilder/main.go @@ -12,6 +12,6 @@ func main() { userId := "test_user" expire := uint32(600) - token, _ := rtmtokenbuilder.BuildToken(appID, appCertificate, userId, expire) + token, _ := rtmtokenbuilder.BuildToken(appID, appCertificate, userId, expire, "") fmt.Println("BuildToken: " + token) } diff --git a/rtmtokenbuilder/RtmTokenBuilder.go b/rtmtokenbuilder/RtmTokenBuilder.go index 06abdcb..b288931 100644 --- a/rtmtokenbuilder/RtmTokenBuilder.go +++ b/rtmtokenbuilder/RtmTokenBuilder.go @@ -4,19 +4,31 @@ import ( accesstoken "github.com/AgoraIO-Community/go-tokenbuilder/accesstoken" ) -// Build the RTM token. +// BuildToken creates an access token with the specified parameters. +// If streamName is provided, it adds the RTC service with the given streamName. // -// 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. -// appCertificate: Certificate of the application that you registered in the Agora Dashboard. See Get an App Certificate. -// userId: The user's account, max length is 64 Bytes. -// 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 expireTimestamp as 600(seconds). +// Parameters: +// - appId: The application ID for the access token. +// - appCertificate: The application certificate for the access token. +// - userId: The user ID for the access token. +// - expire: The expiration time in seconds for the access token. +// - streamName: The name of the stream (optional). // -// return The RTM token. -func BuildToken(appId string, appCertificate string, userId string, expire uint32) (string, error) { +// Returns: +// - The generated access token as a string. +// - An error if there was an issue generating the token. +func BuildToken(appId string, appCertificate string, userId string, expire uint32, streamName string) (string, error) { token := accesstoken.NewAccessToken(appId, appCertificate, expire) serviceRtm := accesstoken.NewServiceRtm(userId) serviceRtm.AddPrivilege(accesstoken.PrivilegeLogin, expire) + if streamName != "" { + // Adds stream data priviliges if streamName != "". Can be "*" for all channels. + rtcStreamService := accesstoken.NewServiceRtc(streamName, userId) + rtcStreamService.AddPrivilege(accesstoken.PrivilegeJoinChannel, expire) + rtcStreamService.AddPrivilege(accesstoken.PrivilegePublishDataStream, expire) + token.AddService(rtcStreamService) + } token.AddService(serviceRtm) return token.Build() diff --git a/rtmtokenbuilder/rtmtokenbuilder_test.go b/rtmtokenbuilder/rtmtokenbuilder_test.go index b0c58d3..4f8177b 100644 --- a/rtmtokenbuilder/rtmtokenbuilder_test.go +++ b/rtmtokenbuilder/rtmtokenbuilder_test.go @@ -14,7 +14,7 @@ const ( ) func Test_BuildToken(t *testing.T) { - token, err := BuildToken(DataMockAppId, DataMockAppCertificate, DataMockUserId, DataMockExpire) + token, err := BuildToken(DataMockAppId, DataMockAppCertificate, DataMockUserId, DataMockExpire, "") accesstoken.AssertNil(t, err) accessToken := accesstoken.CreateAccessToken() @@ -27,3 +27,20 @@ func Test_BuildToken(t *testing.T) { accesstoken.AssertEqual(t, uint16(accesstoken.ServiceTypeRtm), accessToken.Services[accesstoken.ServiceTypeRtm].(*accesstoken.ServiceRtm).Type) accesstoken.AssertEqual(t, DataMockExpire, accessToken.Services[accesstoken.ServiceTypeRtm].(*accesstoken.ServiceRtm).Privileges[accesstoken.PrivilegeLogin]) } + +func Test_BuildTokenWithStream(t *testing.T) { + token, err := BuildToken(DataMockAppId, DataMockAppCertificate, DataMockUserId, DataMockExpire, "*") + accesstoken.AssertNil(t, err) + + accessToken := accesstoken.CreateAccessToken() + accessToken.Parse(token) + + accesstoken.AssertEqual(t, DataMockAppId, accessToken.AppId) + accesstoken.AssertEqual(t, DataMockExpire, accessToken.Expire) + accesstoken.AssertEqual(t, true, accessToken.Services[accesstoken.ServiceTypeRtm] != nil) + accesstoken.AssertEqual(t, DataMockUserId, accessToken.Services[accesstoken.ServiceTypeRtm].(*accesstoken.ServiceRtm).UserId) + accesstoken.AssertEqual(t, uint16(accesstoken.ServiceTypeRtm), accessToken.Services[accesstoken.ServiceTypeRtm].(*accesstoken.ServiceRtm).Type) + accesstoken.AssertEqual(t, DataMockExpire, accessToken.Services[accesstoken.ServiceTypeRtm].(*accesstoken.ServiceRtm).Privileges[accesstoken.PrivilegeLogin]) + accesstoken.AssertEqual(t, DataMockExpire, accessToken.Services[accesstoken.ServiceTypeRtc].(*accesstoken.ServiceRtc).Privileges[accesstoken.PrivilegePublishDataStream]) + accesstoken.AssertEqual(t, DataMockExpire, accessToken.Services[accesstoken.ServiceTypeRtc].(*accesstoken.ServiceRtc).Privileges[accesstoken.PrivilegeJoinChannel]) +}