-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #603 from taiwonaf/Feat/BE-14-implement-Video-call…
…-feature-Agora-token-generator Feat- implelented Agora token generator for Agora engine authentication
- Loading branch information
Showing
7 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package agora | ||
|
||
import "zuri.chat/zccore/utils" | ||
|
||
type AgoraToken struct { | ||
Token string `json:"token"` | ||
} | ||
|
||
type AgoraHandler struct { | ||
configs *utils.Configurations | ||
} | ||
|
||
func NewAgoraHandler(c *utils.Configurations) *AgoraHandler { | ||
return &AgoraHandler{configs: c} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package agora | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/AgoraIO-Community/go-tokenbuilder/rtctokenbuilder" | ||
"github.com/gorilla/mux" | ||
"zuri.chat/zccore/utils" | ||
) | ||
|
||
func (a *AgoraHandler) GetRtcToken(w http.ResponseWriter, r *http.Request) { | ||
|
||
// get param values | ||
channelName, tokentype, uidStr, role := ParseRtcParams(r) | ||
appId := a.configs.AppId | ||
appCertificate := a.configs.AppCerificate | ||
rtcToken, tokenErr := generateRtcToken(appId, appCertificate, channelName, uidStr, tokentype, role) | ||
|
||
if tokenErr != nil { | ||
utils.GetError(tokenErr, http.StatusBadRequest, w) | ||
return | ||
} | ||
response := AgoraToken{ | ||
Token: rtcToken, | ||
} | ||
utils.GetSuccess("Token generated successfully", response, w) | ||
|
||
} | ||
|
||
func ParseRtcParams(r *http.Request) (channelName, tokentype, uidStr string, role rtctokenbuilder.Role) { | ||
channelName = mux.Vars(r)["channelName"] | ||
tokentype = mux.Vars(r)["tokentype"] | ||
roleStr := mux.Vars(r)["role"] | ||
uidStr = mux.Vars(r)["uid"] | ||
|
||
if roleStr == "publisher" { | ||
role = rtctokenbuilder.RolePublisher | ||
} else { | ||
role = rtctokenbuilder.RoleSubscriber | ||
} | ||
|
||
return channelName, tokentype, uidStr, role | ||
} | ||
|
||
func generateRtcToken(appId, appCertificate, channelName, uidStr, tokentype string, role rtctokenbuilder.Role) (string, error) { | ||
expireTimestamp := time.Now().Add(2 * time.Hour).Unix() | ||
expireTime := uint32(expireTimestamp) | ||
if tokentype == "userAccount" { | ||
rtcToken, err := rtctokenbuilder.BuildTokenWithUserAccount(appId, appCertificate, channelName, uidStr, role, expireTime) | ||
if err != nil { | ||
return "", err | ||
} | ||
return rtcToken, nil | ||
|
||
} else if tokentype == "uid" { | ||
uid64, parseErr := strconv.ParseUint(uidStr, 10, 64) | ||
// check if conversion fails | ||
if parseErr != nil { | ||
err := fmt.Errorf("failed to parse uidStr: %s, to uint causing error: %s", uidStr, parseErr) | ||
return "", err | ||
} | ||
|
||
uid := uint32(uid64) // convert uid from uint64 to uint 32 | ||
rtcToken, err := rtctokenbuilder.BuildTokenWithUID(appId, appCertificate, channelName, uid, role, expireTime) | ||
if err != nil { | ||
return "", err | ||
} | ||
return rtcToken, nil | ||
|
||
} else { | ||
err := fmt.Errorf("failed to generate RTC token for Unknown Tokentype: %s", tokentype) | ||
return "", err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters