-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth.go
48 lines (41 loc) · 951 Bytes
/
auth.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 middleware
import (
e "github.com/chances/party-server/errors"
"github.com/chances/party-server/session"
"github.com/gin-gonic/gin"
)
// AuthenticationRequired guards against unauthenticated sessions
func AuthenticationRequired() gin.HandlerFunc {
return func(c *gin.Context) {
if !session.IsLoggedIn(c) {
c.Error(e.Unauthorized)
c.Abort()
return
}
c.Next()
}
}
// GuestsOnly guards against unauthorized guest sessions
func GuestsOnly() gin.HandlerFunc {
return func(c *gin.Context) {
if !session.IsGuest(c) {
c.Error(e.Unauthorized)
c.Abort()
return
}
c.Next()
}
}
// AuthorizationRequired guards against any unauthorized session
//
// A user must either be a guest or authenticated via OAuth
func AuthorizationRequired() gin.HandlerFunc {
return func(c *gin.Context) {
if (!session.IsLoggedIn(c)) && (!session.IsGuest(c)) {
c.Error(e.Unauthorized)
c.Abort()
return
}
c.Next()
}
}