-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathginyourface.go
54 lines (42 loc) · 1.23 KB
/
ginyourface.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
package ginyourface
import (
"encoding/json"
"net/http"
"os"
"github.com/gin-gonic/gin"
facecontrolResponse "github.com/gobricks/facecontrol/classes/response"
)
// Facecontrol middleware checks for token is session cookie on every request
func Facecontrol() gin.HandlerFunc {
return func(c *gin.Context) {
cookies := c.Request.Cookies()
sessionCookieName := os.Getenv("FC_SESSION_COOKIE")
facecontrolHost := os.Getenv("FC_HOST")
loginPage := os.Getenv("FC_LOGIN_PAGE")
if sessionCookieName == "" {
panic("Facecontrol session cookie variable is not set")
}
var cookieToken string
for _, cookie := range cookies {
if cookie.Name == sessionCookieName {
cookieToken = cookie.Value
break
}
}
if cookieToken == "" {
c.Redirect(http.StatusTemporaryRedirect, loginPage)
}
response, err := http.Get(facecontrolHost + "/token/" + cookieToken)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
c.AbortWithStatus(http.StatusForbidden)
}
var responseJSON facecontrolResponse.UserResponse
json.NewDecoder(response.Body).Decode(&responseJSON)
c.Set("userPayload", responseJSON.User)
c.Next()
}
}