-
Notifications
You must be signed in to change notification settings - Fork 0
/
permission.go
75 lines (65 loc) · 1.73 KB
/
permission.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
package main
import (
"net/http"
"github.com/dgrijalva/jwt-go"
"github.com/hoenirvili/rester"
"github.com/hoenirvili/rester/permission"
"github.com/hoenirvili/rester/request"
"github.com/hoenirvili/rester/resource"
"github.com/hoenirvili/rester/response"
"github.com/hoenirvili/rester/route"
"github.com/hoenirvili/rester/token"
)
type jsonResponse struct {
Message string `json:"message"`
}
type root struct{}
func (r *root) index(req request.Request) resource.Response {
return response.Payload(&jsonResponse{"Hello World !"})
}
func (r *root) admin(req request.Request) resource.Response {
return response.Payload(&jsonResponse{"Hello Admin !"})
}
func (r *root) Routes() route.Routes {
return route.Routes{
// This will allow anyone per default
{
URL: "/",
Method: resource.Get,
Handler: r.index,
},
// Only admins
{
URL: "/admin",
Method: resource.Get,
Allow: permission.Admin,
Handler: r.admin,
},
}
}
var pub = []byte(`
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6X5gMNrebNra73WI7jIX
jQSTiBEvI79hMlT+b2qrxDKtq0wY8CaNkr9k5sFyzE/ImGo4NHz3rArkKIQU5uSy
Y0xjJvN0uhqmNGv3F+yXf/ak9BIyiq6TM45j15KVbw+dLHSZJhN8ZZ96C36dOyB2
JPInjkU+pMzJr/Dp6KBL5dDqYqSQzmojb0KxyNyn1VGvurG5e3TFCH4377bg+H0Z
C48AKkhokFvhhI7MJpFhfoQaGqAgg3kOguWOpngDcaTwschjwZ2rW9/qguf6iSsG
77+5RyJrlkZbbpaQl83gEs2EMzykmhzfhDJWXsEA0+HH4ns8XDgqlIodIkTmf2/5
lQIDAQAB
-----END PUBLIC KEY-----
`)[1:]
func main() {
key, err := jwt.ParseRSAPublicKeyFromPEM(pub)
if err != nil {
panic(err)
}
token := token.NewJWT(key)
rester := rester.New(
rester.WithTokenValidator(token),
)
rester.Resource("/", new(root))
rester.Build()
if err := http.ListenAndServe(":8080", rester); err != nil {
panic(err)
}
}