-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: jwtsign cli + admin login + middleware + manage panel (#3)
* feat: jwtsign cli + admin login + more orm models * feat: jwt validate on admin * chore: admin middleware * feat: add manage with real ejudge
- Loading branch information
Showing
23 changed files
with
469 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.PHONY: build run clean test | ||
.PHONY: build run clean test lint | ||
|
||
SERVICE_NAME=crosspawn | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/joho/godotenv" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
defaultUser = "babayka" | ||
defaultDuration = 1 * time.Hour | ||
) | ||
|
||
func main() { | ||
user := flag.String("user", defaultUser, "ejudge login for JWT") | ||
duration := flag.Duration("duration", defaultDuration, "duration for JWT") | ||
flag.Parse() | ||
|
||
logrus.WithFields(logrus.Fields{ | ||
"user": *user, | ||
"duration": *duration, | ||
}).Info("generating JWT") | ||
|
||
if err := godotenv.Load(); err != nil { | ||
logrus.WithError(err).Fatal("failed to load .env file") | ||
} | ||
|
||
key := os.Getenv("JWT_SECRET") | ||
if key == "" { | ||
logrus.Fatal("JWT_SECRET env var is not set") | ||
} | ||
|
||
t := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | ||
"iss": "crosspawn", | ||
"sub": *user, | ||
"exp": time.Now().Add(*duration).Unix(), | ||
}) | ||
s, err := t.SignedString([]byte(key)) | ||
if err != nil { | ||
logrus.WithError(err).Fatal("failed to sign JWT") | ||
} | ||
|
||
fmt.Println(s) //nolint:forbidigo // basic functionality | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package controller | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gin-contrib/sessions" | ||
"github.com/gin-gonic/gin" | ||
"github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
var ( | ||
ErrForeignUser = errors.New("foreign user") | ||
) | ||
|
||
type adminForm struct { | ||
JWT string `binding:"required" form:"jwt"` | ||
} | ||
|
||
// TODO: redirect to /login if admin is set. | ||
func (s *Server) AdminGET(c *gin.Context) { | ||
session := sessions.Default(c) | ||
user := session.Get("user") | ||
|
||
c.HTML(http.StatusOK, "admin.html", gin.H{ | ||
"Title": "Admin", | ||
"User": user, | ||
}) | ||
} | ||
|
||
func (s *Server) AdminPOST(c *gin.Context) { | ||
var form adminForm | ||
if err := c.ShouldBind(&form); err != nil { | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
|
||
return | ||
} | ||
|
||
session := sessions.Default(c) | ||
user := session.Get("user") | ||
|
||
if err := s.validateJWT(form.JWT, user.(string)); err != nil { //nolint:forcetypeassert // it's ok | ||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) | ||
|
||
return | ||
} | ||
|
||
session.Set("admin", true) | ||
_ = session.Save() | ||
|
||
c.Redirect(http.StatusFound, "/manage") | ||
} | ||
|
||
func (s *Server) validateJWT(t, user string) error { | ||
claims := jwt.MapClaims{} | ||
_, err := jwt.ParseWithClaims(t, claims, func(token *jwt.Token) (interface{}, error) { | ||
return []byte(s.cfg.JWTSecret), nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
if claims["sub"] != user { | ||
return fmt.Errorf("%w: token is not owned by %s", ErrForeignUser, user) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *Server) adminMiddleware(c *gin.Context) { | ||
session := sessions.Default(c) | ||
admin := session.Get("admin") | ||
|
||
if admin == nil { | ||
c.Redirect(http.StatusFound, "/admin") | ||
c.Abort() | ||
|
||
return | ||
} | ||
|
||
c.Next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package controller | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/Gornak40/crosspawn/models" | ||
"github.com/gin-contrib/sessions" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type manageForm struct { | ||
ContestID uint `binding:"required" form:"ejContestID"` | ||
} | ||
|
||
func (s *Server) ManageGET(c *gin.Context) { | ||
session := sessions.Default(c) | ||
user := session.Get("user") | ||
|
||
var contests []models.Contest | ||
if res := s.db.Find(&contests); res.Error != nil { | ||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": res.Error.Error()}) | ||
|
||
return | ||
} | ||
|
||
c.HTML(http.StatusOK, "manage.html", gin.H{ | ||
"Title": "Manage", | ||
"User": user, | ||
"Contests": contests, | ||
}) | ||
} | ||
|
||
// TODO: add check for acm format. | ||
func (s *Server) ManagePOST(c *gin.Context) { | ||
var form manageForm | ||
if err := c.ShouldBind(&form); err != nil { | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
|
||
return | ||
} | ||
|
||
contest, err := s.ej.GetContestStatus(form.ContestID) | ||
if err != nil { | ||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
|
||
return | ||
} | ||
|
||
dbContest := models.NewContest(contest) | ||
if res := s.db.Create(dbContest); res.Error != nil { | ||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": res.Error.Error()}) | ||
|
||
return | ||
} | ||
|
||
c.Redirect(http.StatusFound, "/manage") | ||
} | ||
|
||
func (s *Server) ManageFlipPOST(c *gin.Context) { | ||
var form manageForm | ||
if err := c.ShouldBind(&form); err != nil { | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
|
||
return | ||
} | ||
|
||
dbContest := models.Contest{EjudgeID: form.ContestID} | ||
if err := s.db.Where("ejudge_id = ?", form.ContestID).First(&dbContest).Error; err != nil { | ||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
|
||
return | ||
} | ||
|
||
dbContest.ReviewActive = !dbContest.ReviewActive | ||
if res := s.db.Save(&dbContest); res.Error != nil { | ||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": res.Error.Error()}) | ||
|
||
return | ||
} | ||
|
||
c.Redirect(http.StatusFound, "/manage") | ||
} |
Oops, something went wrong.