Skip to content

Commit 3cb0aa9

Browse files
author
lilang
committed
Merge branch 'dev_token' into 'master' (merge request !79)
dev_token
2 parents dafc973 + 66a66a1 commit 3cb0aa9

File tree

3 files changed

+119
-0
lines changed

3 files changed

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

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)