Skip to content

Commit 0fc7f23

Browse files
author
lilang
committed
generate_token demo
1 parent dafc973 commit 0fc7f23

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"time"
9+
10+
"github.com/dgrijalva/jwt-go"
11+
"github.com/tencentyun/cos-go-sdk-v5"
12+
)
13+
14+
type JwtTokens struct {
15+
// base info
16+
Type string `json:"Type"`
17+
AppId string `json:"AppId"`
18+
BucketId string `json:"BucketId"`
19+
Issuer string `json:"Issuer"`
20+
// time info
21+
IssuedTimeStamp int64 `json:"IssuedTimeStamp"`
22+
NotBeforeTimeStamp int64 `json:"NotBeforeTimeStamp"`
23+
ExpireTimeStamp int64 `json:"ExpireTimeStamp"`
24+
// other info
25+
Random int64 `json:"Random"`
26+
// times info
27+
UsageLimit int `json:"UsageLimit"`
28+
// secret info
29+
ProtectSchema string `json:"ProtectSchema"`
30+
PublicKey string `json:"PublicKey"`
31+
ProtectContentKey int `json:"ProtectContentKey"`
32+
RequestAppId string `json:"RequestAppId"`
33+
RequestBucket string `json:"RequestBucket"`
34+
}
35+
36+
// 定义secret
37+
var Secret = []byte("xxx")
38+
39+
func (token JwtTokens) Valid() error {
40+
return nil
41+
}
42+
43+
// 生成jwt
44+
func GenerateToken() (string, error) {
45+
t := time.Now()
46+
now := t.Unix()
47+
payLoad := JwtTokens{
48+
// 固定为 CosCiToken, 必填参数
49+
Type: "CosCiToken",
50+
// app id,必填参数
51+
AppId: "1234567890",
52+
// 播放文件所在的BucketId, 必填参数
53+
BucketId: "test-1234567890",
54+
// 固定为client,必填参数
55+
Issuer: "client",
56+
// token颁发时间戳,必填参数
57+
IssuedTimeStamp: now,
58+
// token可用时间戳,必填参数
59+
NotBeforeTimeStamp: now,
60+
// token过期时间戳,非必填参数,默认1天过期
61+
ExpireTimeStamp: t.Add(time.Hour * 24 * 6).Unix(),
62+
// token使用次数限制,非必填参数,默认限制100次
63+
UsageLimit: 20,
64+
// 保护模式,填写为 rsa1024 ,则表示使用 RSA 非对称加密的方式保护,公私钥对长度为 1024 bit
65+
ProtectSchema: "rsa1024",
66+
// 公钥。1024 bit 的 RSA 公钥,需使用 Base64 进行编码
67+
PublicKey: "xxx",
68+
// 是否加密解密密钥(播放时解密ts视频流的密钥),1表示对解密密钥加密,0表示不对解密密钥加密。
69+
ProtectContentKey: 0,
70+
}
71+
//使用指定的签名方法创建签名对象
72+
token := jwt.NewWithClaims(jwt.SigningMethodHS256, payLoad)
73+
74+
//使用指定的secret签名并获得完成的编码后的字符串token
75+
return token.SignedString(Secret)
76+
}
77+
78+
type URLToken struct {
79+
SessionToken string `url:"x-cos-security-token,omitempty" header:"-"`
80+
}
81+
82+
func GetURL() {
83+
// 替换成您的密钥
84+
tak := "xxx"
85+
tsk := "xxx"
86+
token := &URLToken{
87+
SessionToken: "",
88+
}
89+
90+
// 替换成您的桶名称
91+
bucketName := "test-1234567890"
92+
// 替换成您桶所在的region
93+
region := "ap-chongqing"
94+
// 替换成您需要播放的视频名称
95+
objectName := "hls_test/no_uri_key.m3u8"
96+
// 固定为pm3u8
97+
name := "pm3u8"
98+
99+
u, _ := url.Parse("http://" + bucketName + ".ci." + region + ".myqcloud.com")
100+
b := &cos.BaseURL{BucketURL: u}
101+
c := cos.NewClient(b, &http.Client{})
102+
ctx := context.Background()
103+
104+
// 获取预签名
105+
presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, token)
106+
if err != nil {
107+
fmt.Printf("Error: %v\n", err)
108+
return
109+
}
110+
fmt.Println(presignedURL.String())
111+
// 生成token
112+
generateToken, _ := GenerateToken()
113+
resultUrl := presignedURL.String() + "&tokenType=JwtToken&expires=3600&object=" + objectName + "&token=" + generateToken
114+
fmt.Println(resultUrl)
115+
}
116+
117+
func main() {
118+
GetURL()
119+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.12
55
require (
66
github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409
77
github.com/clbanning/mxj v1.8.4
8+
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
89
github.com/google/go-querystring v1.0.0
910
github.com/google/uuid v1.1.1
1011
github.com/mitchellh/mapstructure v1.4.3

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mo
22
github.com/clbanning/mxj v1.8.4 h1:HuhwZtbyvyOw+3Z1AowPkU87JkJUSv751ELWaiTpj8I=
33
github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
44
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
6+
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
57
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
68
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
79
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=

0 commit comments

Comments
 (0)