-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.go
More file actions
52 lines (43 loc) · 997 Bytes
/
middleware.go
File metadata and controls
52 lines (43 loc) · 997 Bytes
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
package wireguardhttps
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/gorilla/sessions"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
)
func ProviderWhitelistMiddleware(c *gin.Context) {
if !isAllowedProvider(c) {
c.AbortWithStatus(http.StatusBadRequest)
return
}
c.Next()
}
func isAllowedProvider(c *gin.Context) bool {
if p, err := gothic.GetProviderName(c.Request); err == nil {
for _, provider := range goth.GetProviders() {
if p == provider.Name() {
return true
}
}
}
return false
}
func AuthenticationRequiredMiddleware(store sessions.Store, sessionName string) func(*gin.Context) {
return func(c *gin.Context) {
session, err := store.Get(c.Request, sessionName)
if err != nil {
log.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
user, ok := session.Values["user"]
if !ok {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
c.Set("user", user)
c.Next()
}
}