-
Notifications
You must be signed in to change notification settings - Fork 179
/
authorization.go
49 lines (42 loc) · 1.02 KB
/
authorization.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
package api
import (
"net/http"
"strings"
"github.com/convox/rack/pkg/structs"
"github.com/convox/stdapi"
)
func (s *Server) Authorize(next stdapi.HandlerFunc) stdapi.HandlerFunc {
return func(c *stdapi.Context) error {
switch c.Request().Method {
case http.MethodGet:
if !CanRead(c) {
return stdapi.Errorf(401, "you are unauthorized to access this")
}
default:
if !CanWrite(c) {
return stdapi.Errorf(401, "you are unauthorized to access this")
}
}
return next(c)
}
}
func CanRead(c *stdapi.Context) bool {
if d := c.Get(structs.ConvoxRoleParam); d != nil {
v, _ := d.(string)
return strings.Contains(v, "r")
}
return false
}
func CanWrite(c *stdapi.Context) bool {
if d := c.Get(structs.ConvoxRoleParam); d != nil {
v, _ := d.(string)
return strings.Contains(v, "w")
}
return false
}
func SetReadRole(c *stdapi.Context) {
c.Set(structs.ConvoxRoleParam, structs.ConvoxRoleRead)
}
func SetReadWriteRole(c *stdapi.Context) {
c.Set(structs.ConvoxRoleParam, structs.ConvoxRoleReadWrite)
}