-
Notifications
You must be signed in to change notification settings - Fork 1
/
token.go
119 lines (98 loc) · 3.15 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package go_ts3
import (
"fmt"
"strings"
)
// tokenadd `manage_scope, write_scope`
// tokencustomset is currently not possible
type TokenAddRequest struct {
TokenType int `schema:"tokentype"`
TokenMajorId int `schema:"tokenid1"`
TokenMinorId int `schema:"tokenid2"`
TokenDescription string `schema:"tokendescription"`
TokenCustomSet string `schema:"tokencustomset,omitempty"`
}
func NewGroupToken(serverGroupId int, description string) TokenAddRequest {
return TokenAddRequest{
TokenType: 0,
TokenMajorId: serverGroupId,
TokenMinorId: 0,
TokenDescription: description,
}
}
func NewCustomSetGroupToken(serverGroupId int, description string, customSet map[string]string) TokenAddRequest {
return TokenAddRequest{
TokenType: 0,
TokenMajorId: serverGroupId,
TokenMinorId: 0,
TokenDescription: description,
TokenCustomSet: generateCustomSet(customSet),
}
}
func NewChannelToken(channelGroupId, channelId int, description string) TokenAddRequest {
return TokenAddRequest{
TokenType: 1,
TokenMajorId: channelGroupId,
TokenMinorId: channelId,
TokenDescription: description,
}
}
func NewCustomSetChannelToken(channelGroupId, channelId int, description string, customSet map[string]string) TokenAddRequest {
return TokenAddRequest{
TokenType: 1,
TokenMajorId: channelGroupId,
TokenMinorId: channelId,
TokenDescription: description,
TokenCustomSet: generateCustomSet(customSet),
}
}
func generateCustomSet(values map[string]string) string {
var entries []string
for ident, value := range values {
entries = append(entries, fmt.Sprintf("ident=%s value=%s", ident, value))
}
return strings.Join(entries, "\\p")
}
type TokenAddResponse struct {
Token string `schema:"token"`
}
func (c *TeamspeakHttpClient) TokenAdd(request TokenAddRequest) (*TokenAddResponse, error) {
var tokens []TokenAddResponse
err := c.requestWithParams("tokenadd", request, &tokens)
if err != nil {
return nil, err
}
return &tokens[0], nil
}
// tokendelete `manage_scope, write_scope`
type tokenDeleteRequest struct {
TokenKey string `schema:"token,required"`
}
func (c *TeamspeakHttpClient) TokenDelete(tokenKey string) error {
return c.requestWithParams("tokendelete", tokenDeleteRequest{TokenKey: tokenKey}, nil)
}
// tokenlist `manage_scope, write_scope, read_scope`
type Token struct {
Token string `json:"token"`
TokenDescription string `json:"token_description"`
TokenCreated int `json:"token_created,string"`
TokenType int `json:"token_type,string"`
TokenId1 int `json:"token_id1,string"`
TokenId2 int `json:"token_id2,string"`
TokenCustomset string `json:"token_customset"`
}
func (c *TeamspeakHttpClient) TokenList() (*[]Token, error) {
var tokens []Token
err := c.request("tokenlist", &tokens)
if err != nil {
return nil, err
}
return &tokens, nil
}
// tokenuse `manage_scope, write_scope`
type tokenUseRequest struct {
TokenKey string `schema:"token,required"`
}
func (c *TeamspeakHttpClient) TokenUse(tokenKey string) error {
return c.requestWithParams("tokenuse", tokenUseRequest{TokenKey: tokenKey}, nil)
}