-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
100 lines (83 loc) · 2.74 KB
/
api.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
package main
import (
"errors"
"io"
"time"
)
const (
tokenLen = 64
cliTokenPrefix = "cli_"
cliTokenLen = tokenLen + len(cliTokenPrefix)
daemonTokenPrefix = "D_"
daemonTokenLen = tokenLen + len(daemonTokenPrefix)
)
var (
PermDeniedErr = errors.New("Permission denied")
TokenNotExistsErr = errors.New("Token not exists")
PluginNotExistsErr = errors.New("Plugin not exists")
ErrIsDir = errors.New("Plugin not exists")
)
type UserInfo struct {
Username string `json:"username"`
}
type Token struct {
Token string `json:"token"`
Root bool `json:"root"`
Expiration *time.Time `json:"expiration"`
}
type DaemonToken struct {
Token string `json:"token"`
Server string `json:"server"`
Expiration *time.Time `json:"expiration"`
}
type WebScriptId struct {
Id string `json:"id"`
Version string `json:"version"`
}
type WebScriptMeta struct {
WebScriptId
Name string `json:"name"`
Author string `json:"author"`
Desc string `json:"desc"`
}
type FileInfo struct {
Name string `json:"name"`
IsDir bool `json:"isdir"`
ModTime time.Time `json:"modTime"`
}
type DataAPI interface {
NewCliToken(expiration *time.Time)(token string, err error)
NewDaemonToken(server string, expiration *time.Time)(token string, err error)
RemoveCliToken(token string)(err error)
RemoveDaemonToken(token string)(err error)
ListTokens()(tokens []Token, err error)
ListDaemonTokens()(tokens []DaemonToken, err error)
AuthCli(token string)(ok bool)
AuthDaemon(token string, host string)(ok bool)
GetUserInfo(token string)(info UserInfo, err error)
CheckRootToken(token string)(ok bool)
SetRoot(token string, value bool)(err error)
CreateServer(id string)(err error)
RemoveServer(id string)(err error)
ListServers(token string)(servers []string, err error)
CheckPerm(token string, server string)(ok bool)
SetPerm(token string, server string, ok bool)(err error)
ListCliWebScripts(token string)(scripts []WebScriptId, err error)
AddCliWebScript(token string, plugin WebScriptId)(err error)
DelCliWebScript(token string, plugin string)(err error)
}
type FsAPI interface {
CreatePlugin(plugin WebScriptMeta)(err error)
DeletePlugin(plugin WebScriptId)(err error)
ListPlugins()(plugins []WebScriptMeta, err error)
ListPluginFiles(plugin WebScriptId, path string)(files []*FileInfo, err error)
GetPluginFile(plugin WebScriptId, path string)(r io.ReadSeekCloser, modTime time.Time, err error)
PutPluginFile(plugin WebScriptId, path string, r io.Reader)(err error)
DelPluginFile(plugin WebScriptId, path string)(err error)
}
func preProcessCliToken(clitoken string)(token string, ok bool){
if len(clitoken) != cliTokenLen || clitoken[:len(cliTokenPrefix)] != cliTokenPrefix {
return "", false
}
return clitoken[len(cliTokenPrefix):], true
}