Skip to content

Commit

Permalink
added extra parameter for rtm BuildToken. empty string gives same res…
Browse files Browse the repository at this point in the history
…ults as previously, other value adds rtc join and stream message priviliges go to output
  • Loading branch information
maxxfrazer committed Jul 25, 2023
1 parent 285edad commit 63039be
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/rtmtokenbuilder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
26 changes: 19 additions & 7 deletions rtmtokenbuilder/RtmTokenBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 18 additions & 1 deletion rtmtokenbuilder/rtmtokenbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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])
}

0 comments on commit 63039be

Please sign in to comment.