Use gin-gonic instead of httprouter
This commit is contained in:
parent
7c719d9fd5
commit
a203cdc36a
22 changed files with 1668 additions and 1392 deletions
64
auth.go
64
auth.go
|
@ -2,12 +2,10 @@ package main
|
|||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var LocalAuthFunc = checkAuthKrb5
|
||||
|
@ -18,12 +16,12 @@ type loginForm struct {
|
|||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
router.GET("/api/auth", apiAuthHandler(validateAuthToken))
|
||||
router.POST("/api/auth", apiRawHandler(func(w http.ResponseWriter, ps httprouter.Params, body []byte) HTTPResponse {
|
||||
return formatApiResponse(LocalAuthFunc(w, ps, body))
|
||||
}))
|
||||
router.POST("/api/auth/logout", apiRawHandler(logout))
|
||||
func declareAPIAuthRoutes(router *gin.RouterGroup) {
|
||||
router.GET("/auth", validateAuthToken)
|
||||
router.POST("/auth", func(c *gin.Context) {
|
||||
LocalAuthFunc(c)
|
||||
})
|
||||
router.POST("/auth/logout", logout)
|
||||
}
|
||||
|
||||
type authToken struct {
|
||||
|
@ -31,20 +29,21 @@ type authToken struct {
|
|||
CurrentPromo uint `json:"current_promo"`
|
||||
}
|
||||
|
||||
func validateAuthToken(u *User, _ httprouter.Params, _ []byte) HTTPResponse {
|
||||
if u == nil {
|
||||
return APIErrorResponse{status: http.StatusUnauthorized, err: fmt.Errorf("Not connected")}
|
||||
func validateAuthToken(c *gin.Context) {
|
||||
if u, ok := c.Get("LoggedUser"); !ok || u.(*User) == nil {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"errmsg": "Not connected"})
|
||||
return
|
||||
} else {
|
||||
return APIResponse{authToken{u, currentPromo}}
|
||||
c.JSON(http.StatusOK, authToken{u.(*User), currentPromo})
|
||||
}
|
||||
}
|
||||
|
||||
func logout(w http.ResponseWriter, ps httprouter.Params, body []byte) HTTPResponse {
|
||||
eraseCookie(w)
|
||||
return APIResponse{true}
|
||||
func logout(c *gin.Context) {
|
||||
eraseCookie(c)
|
||||
c.JSON(http.StatusOK, true)
|
||||
}
|
||||
|
||||
func completeAuth(w http.ResponseWriter, username string, email string, firstname string, lastname string, groups string, session *Session) (usr User, err error) {
|
||||
func completeAuth(c *gin.Context, username string, email string, firstname string, lastname string, groups string, session *Session) (usr *User, err error) {
|
||||
if !userExists(username) {
|
||||
if usr, err = NewUser(username, email, firstname, lastname, groups); err != nil {
|
||||
return
|
||||
|
@ -64,9 +63,7 @@ func completeAuth(w http.ResponseWriter, username string, email string, firstnam
|
|||
}
|
||||
|
||||
if session == nil {
|
||||
var s Session
|
||||
s, err = usr.NewSession()
|
||||
session = &s
|
||||
session, err = usr.NewSession()
|
||||
} else {
|
||||
_, err = session.SetUser(usr)
|
||||
}
|
||||
|
@ -75,7 +72,7 @@ func completeAuth(w http.ResponseWriter, username string, email string, firstnam
|
|||
return
|
||||
}
|
||||
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
http.SetCookie(c.Writer, &http.Cookie{
|
||||
Name: "auth",
|
||||
Value: base64.StdEncoding.EncodeToString(session.Id),
|
||||
Path: baseURL + "/",
|
||||
|
@ -88,11 +85,28 @@ func completeAuth(w http.ResponseWriter, username string, email string, firstnam
|
|||
return
|
||||
}
|
||||
|
||||
func dummyAuth(w http.ResponseWriter, _ httprouter.Params, body []byte) (interface{}, error) {
|
||||
func eraseCookie(c *gin.Context) {
|
||||
http.SetCookie(c.Writer, &http.Cookie{
|
||||
Name: "auth",
|
||||
Value: "",
|
||||
Path: baseURL + "/",
|
||||
Expires: time.Unix(0, 0),
|
||||
HttpOnly: true,
|
||||
SameSite: http.SameSiteStrictMode,
|
||||
})
|
||||
}
|
||||
|
||||
func dummyAuth(c *gin.Context) {
|
||||
var lf map[string]string
|
||||
if err := json.Unmarshal(body, &lf); err != nil {
|
||||
return nil, err
|
||||
if err := c.ShouldBindJSON(&lf); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
return completeAuth(w, lf["username"], lf["email"], lf["firstname"], lf["lastname"], "", nil)
|
||||
if usr, err := completeAuth(c, lf["username"], lf["email"], lf["firstname"], lf["lastname"], "", nil); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
} else {
|
||||
c.JSON(http.StatusOK, authToken{usr, currentPromo})
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue