-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsandstorm.go
48 lines (39 loc) · 1.06 KB
/
sandstorm.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
package main
import (
"net/http"
"strings"
)
type Permission string
const HeaderLocalDevelopment = "X-Local-Development"
const HeaderSandstormPermissions = "X-Sandstorm-Permissions"
const HeaderSandstormUserId = "X-Sandstorm-User-Id"
const PermissionBookmarks = Permission("bookmarks")
const PermissionDownload = Permission("download")
type Permissions []Permission
type SandstormUserId string
func GetSandstormUserId(r *http.Request) *SandstormUserId {
sid := SandstormUserId(r.Header.Get(HeaderSandstormUserId))
if sid != "" {
return &sid
}
return nil
}
func (pp Permissions) Has(hp Permission) bool {
for _, p := range pp {
if hp == p {
return true
}
}
return false
}
func (s *Server) SandstormPermissions(r *http.Request) Permissions {
if s.isLocal {
return Permissions{PermissionBookmarks, PermissionDownload}
}
var ps Permissions
for _, strPerm := range strings.Split(r.Header.Get(HeaderSandstormPermissions), ",") {
// Why not just add it. If it's invalid it just won't match anything.
ps = append(ps, Permission(strPerm))
}
return ps
}