Skip to content

Commit

Permalink
feat(): generate token by Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshinexcode committed Dec 19, 2024
1 parent f2e9075 commit 6271ab8
Show file tree
Hide file tree
Showing 21 changed files with 1,434 additions and 0 deletions.
19 changes: 19 additions & 0 deletions DynamicKey/AgoraDynamicKey/lua/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "AgoraDynamicKey-lua",
"image": "nickblah/lua:5.4.7-luarocks-ubuntu",
"customizations": {
"vscode": {
"extensions": [
"sumneko.lua"
]
}
},
"features": {
"ghcr.io/devcontainers/features/git:1": {}
},
"workspaceMount": "source=${localWorkspaceFolder},target=/app,type=bind",
"workspaceFolder": "/app",
"postCreateCommand": {
"install dependencies": "apt-get update && apt-get install -y build-essential zlib1g-dev libssl-dev && luarocks make agora-token-0.1.0-1.rockspec"
}
}
2 changes: 2 additions & 0 deletions DynamicKey/AgoraDynamicKey/lua/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.vscode
13 changes: 13 additions & 0 deletions DynamicKey/AgoraDynamicKey/lua/.luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"workspace": {
"library": [
"/usr/local"
]
},
"diagnostics": {
"globals": [
"error",
"os"
]
}
}
31 changes: 31 additions & 0 deletions DynamicKey/AgoraDynamicKey/lua/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Authenticate Users with a token

To enhance communication security, Agora uses tokens to authenticate users before they access the Agora service, or joining an RTC channel.

## Code structure

Under the `lua` directory:

* `/src/` contains the source code for generating a token, where `rtc_token_builder` is used for generating an RTC token, and `rtm_token_builder` is used for generating an RTM token.
* `/examples/` contains the sample code for generating a token, where `rtc_token_builder` is used for generating an RTC token, and `rtm_token_builder` is used for generating an RTM token.

## Generate a token with the sample code

This section takes `rtc_token_builder` as an example to show how to generate a token with the sample code.

Before proceeding, ensure that you have installed the latest version of Lua.

1. Download or clone the [Tools](https://github.com/AgoraIO/Tools) repository.

2. Open the `DynamicKey/AgoraDynamicKey/lua/examples/rtc_token_builder.lua` file, replace the value of `app_id`, `app_certificate`, `channel_name`, and `uid` with your own, and comment out the code snippets of `build_token_with_user_account`.

3. Open your Terminal, navigate to the same directory, and run the following command.

```
luarocks make agora-token-0.1.0-1.rockspec
lua examples/rtc_token_builder.lua
```

## Reference

For a complete authentication flow between the app server and app client, see [Authenticate Your Users with Tokens]().
30 changes: 30 additions & 0 deletions DynamicKey/AgoraDynamicKey/lua/agora-token-0.1.0-1.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package = "agora-token"
version = "0.1.0-1"
source = {
url = "git+https://github.com/AgoraIO/Tools",
}
description = {
summary = "Agora Token for lua",
homepage = "https://github.com/AgoraIO/Tools/tree/master/DynamicKey/AgoraDynamicKey/lua",
license = "MIT",
}
dependencies = {
"base64",
"luaossl",
"luaunit",
"lua-zlib",
"md5"
}
build = {
type = "builtin",
modules = {
["agora_token.access_token"] = "src/access_token.lua",
["agora_token.apaas_token_builder"] = "src/apaas_token_builder.lua",
["agora_token.chat_token_builder"] = "src/chat_token_builder.lua",
["agora_token.education_token_builder"] = "src/education_token_builder.lua",
["agora_token.fpa_token_builder"] = "src/fpa_token_builder.lua",
["agora_token.rtc_token_builder"] = "src/rtc_token_builder.lua",
["agora_token.rtm_token_builder"] = "src/rtm_token_builder.lua",
["agora_token.utils"] = "src/utils.lua"
}
}
45 changes: 45 additions & 0 deletions DynamicKey/AgoraDynamicKey/lua/examples/apaas_token_builder.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local apaas_token_builder = require("agora_token.apaas_token_builder")

-- Need to set environment variable AGORA_APP_ID
local app_id = os.getenv("AGORA_APP_ID") or ""
-- Need to set environment variable AGORA_APP_CERTIFICATE
local app_certificate = os.getenv("AGORA_APP_CERTIFICATE") or ""

local room_uuid = "123"
local user_uuid = "2882341273"
local role = 1
local expire = 600

print("App Id: " .. app_id)
print("App Certificate: " .. app_certificate)
if app_id == "" or app_certificate == "" then
print("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE")
return
end

local status, token = pcall(function()
return apaas_token_builder.build_room_user_token(app_id, app_certificate, room_uuid, user_uuid, role, expire)
end)
if status then
print("Apaas room user token: " .. token)
else
print("Error: " .. token)
end

status, token = pcall(function()
return apaas_token_builder.build_user_token(app_id, app_certificate, user_uuid, expire)
end)
if status then
print("Apaas user token: " .. token)
else
print("Error: " .. token)
end

status, token = pcall(function()
return apaas_token_builder.build_app_token(app_id, app_certificate, expire)
end)
if status then
print("Apaas app token: " .. token)
else
print("Error: " .. token)
end
34 changes: 34 additions & 0 deletions DynamicKey/AgoraDynamicKey/lua/examples/chat_token_builder.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local chat_token_builder = require("agora_token.chat_token_builder")

-- Need to set environment variable AGORA_APP_ID
local app_id = os.getenv("AGORA_APP_ID") or ""
-- Need to set environment variable AGORA_APP_CERTIFICATE
local app_certificate = os.getenv("AGORA_APP_CERTIFICATE") or ""

local user_uuid = "a7180cb0-1d4a-11ed-9210-89ff47c9da5e"
local expire = 600

print("App Id: " .. app_id)
print("App Certificate: " .. app_certificate)
if app_id == "" or app_certificate == "" then
print("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE")
return
end

local status, token = pcall(function()
return chat_token_builder.build_chat_app_token(app_id, app_certificate, expire)
end)
if status then
print("ChatAppToken: " .. token)
else
print("Error: " .. token)
end

status, token = pcall(function()
return chat_token_builder.build_chat_user_token(app_id, app_certificate, user_uuid, expire)
end)
if status then
print("ChatUserToken: " .. token)
else
print("Error: " .. token)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local education_token_builder = require("agora_token.education_token_builder")

-- Need to set environment variable AGORA_APP_ID
local app_id = os.getenv("AGORA_APP_ID") or ""
-- Need to set environment variable AGORA_APP_CERTIFICATE
local app_certificate = os.getenv("AGORA_APP_CERTIFICATE") or ""

local room_uuid = "123"
local user_uuid = "2882341273"
local role = 1
local expire = 600

print("App Id: " .. app_id)
print("App Certificate: " .. app_certificate)
if app_id == "" or app_certificate == "" then
print("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE")
return
end

local status, token = pcall(function()
return education_token_builder.build_room_user_token(app_id, app_certificate, room_uuid, user_uuid, role, expire)
end)
if status then
print("Education room user token: " .. token)
else
print("Error: " .. token)
end

status, token = pcall(function()
return education_token_builder.build_user_token(app_id, app_certificate, user_uuid, expire)
end)
if status then
print("Education user token: " .. token)
else
print("Error: " .. token)
end

status, token = pcall(function()
return education_token_builder.build_app_token(app_id, app_certificate, expire)
end)
if status then
print("Education app token: " .. token)
else
print("Error: " .. token)
end
23 changes: 23 additions & 0 deletions DynamicKey/AgoraDynamicKey/lua/examples/fpa_token_builder.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local fpa_token_builder = require("agora_token.fpa_token_builder")

-- Need to set environment variable AGORA_APP_ID
local app_id = os.getenv("AGORA_APP_ID") or ""
-- Need to set environment variable AGORA_APP_CERTIFICATE
local app_certificate = os.getenv("AGORA_APP_CERTIFICATE") or ""

print("App Id: " .. app_id)
print("App Certificate: " .. app_certificate)

if app_id == "" or app_certificate == "" then
print("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE")
return
end

local status, token = pcall(function()
return fpa_token_builder.build_token(app_id, app_certificate)
end)
if status then
print("Token with FPA service: " .. token)
else
print("Error: " .. token)
end
116 changes: 116 additions & 0 deletions DynamicKey/AgoraDynamicKey/lua/examples/rtc_token_builder.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
local rtc_token_builder = require("agora_token.rtc_token_builder")

-- Need to set environment variable AGORA_APP_ID
local app_id = os.getenv("AGORA_APP_ID") or ""
-- Need to set environment variable AGORA_APP_CERTIFICATE
local app_certificate = os.getenv("AGORA_APP_CERTIFICATE") or ""

local channel_name = "7d72365eb983485397e3e3f9d460bdda"
local uid = 2882341273
local uid_str = "2882341273"
local token_expiration_in_seconds = 3600
local privilege_expiration_in_seconds = 3600
local join_channel_privilege_expire_in_seconds = 3600
local pub_audio_privilege_expire_in_seconds = 3600
local pub_video_privilege_expire_in_seconds = 3600
local pub_data_stream_privilege_expire_in_seconds = 3600

print("App Id: " .. app_id)
print("App Certificate: " .. app_certificate)
if app_id == nil or app_certificate == nil or app_id == "" or app_certificate == "" then
print("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE")
return
end

local status, token = pcall(function()
return rtc_token_builder.build_token_with_uid(
app_id,
app_certificate,
channel_name,
uid,
rtc_token_builder.ROLE_PUBLISHER,
token_expiration_in_seconds,
privilege_expiration_in_seconds)
end)

if status then
print("Token with int uid: " .. token)
else
print("Error: " .. token)
end

status, token = pcall(function()
return rtc_token_builder.build_token_with_user_account(
app_id,
app_certificate,
channel_name,
uid_str,
rtc_token_builder.ROLE_PUBLISHER,
token_expiration_in_seconds,
privilege_expiration_in_seconds
)
end)

if status then
print("Token with user account: " .. token)
else
print("Error: " .. token)
end

status, token = pcall(function()
return rtc_token_builder.build_token_with_uid_and_privilege(
app_id,
app_certificate,
channel_name,
uid,
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
)
end)

if status then
print("Token with int uid and privilege: " .. token)
else
print("Error: " .. token)
end

status, token = pcall(function()
return rtc_token_builder.build_token_with_user_account_and_privilege(
app_id,
app_certificate,
channel_name,
uid_str,
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
)
end)

if status then
print("Token with user account and privilege: " .. token)
else
print("Error: " .. token)
end

status, token = pcall(function()
return rtc_token_builder.build_token_with_rtm(
app_id,
app_certificate,
channel_name,
uid_str,
rtc_token_builder.ROLE_PUBLISHER,
token_expiration_in_seconds,
privilege_expiration_in_seconds
)
end)

if status then
print("Token with RTM: " .. token)
else
print("Error: " .. token)
end
27 changes: 27 additions & 0 deletions DynamicKey/AgoraDynamicKey/lua/examples/rtm_token_builder.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
local rtm_token_builder = require("agora_token.rtm_token_builder")

-- Need to set environment variable AGORA_APP_ID
local app_id = os.getenv("AGORA_APP_ID") or ""
-- Need to set environment variable AGORA_APP_CERTIFICATE
local app_certificate = os.getenv("AGORA_APP_CERTIFICATE") or ""

local user_id = "test_user_id"
local expiration_seconds = 3600

print("App Id: " .. app_id)
print("App Certificate: " .. app_certificate)

if app_id == "" or app_certificate == "" then
print("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE")
return
end

local status, token = pcall(function()
return rtm_token_builder.build_token(app_id, app_certificate, user_id, expiration_seconds)
end)

if status then
print("Rtm Token: " .. token)
else
print("Error: " .. token)
end
Loading

0 comments on commit 6271ab8

Please sign in to comment.