Commit 28790d85 authored by Julien Schröter's avatar Julien Schröter Committed by Frederik Wegner

Add endpoint to update firebase-token for the current session

parent 3f2512ad
Pipeline #1964 failed with stages
in 2 minutes and 41 seconds
package refreshjwt
import (
"net/http"
"github.com/getsentry/raven-go"
"github.com/gin-gonic/gin"
"gitlab.akamu.de/akamu/game-server-go/endpoint"
)
func SetupRefreshJWTRoutes(group *gin.RouterGroup, repository RefreshJWTQuery) {
group.OPTIONS("", func(ctx *gin.Context) {
ctx.Header("Allow", "PATCH")
ctx.Status(200)
})
group.PATCH("", updateFirebaseToken(repository))
}
func updateFirebaseToken(repository RefreshJWTQuery) gin.HandlerFunc {
return func(ctx *gin.Context) {
// Get current sessionID
payload, err := endpoint.GetSessionPayload(ctx)
if err != nil {
return
}
// Get new token from request payload
var req struct {
Token string `json:"firebase-token"`
}
if err := ctx.BindJSON(&req); err != nil {
return
}
// Update token in database
err = repository.UpdateFirebaseToken(payload.SessionID, req.Token)
if err != nil {
raven.CaptureError(err, nil)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
ctx.Status(http.StatusOK)
}
}
/*
func getTitles(repository TitleQuery) gin.HandlerFunc {
return func(ctx *gin.Context) {
userID, err := endpoint.GetUserID(ctx)
if err != nil {
return
}
// Fetch titles which are available to the current user from database
titles, err := repository.Select(userID)
if err != nil {
eventID := raven.CaptureError(err, nil)
ctx.String(http.StatusInternalServerError, "Failed fetching data from DB. "+eventID)
return
}
// set http response
ctx.JSON(http.StatusOK, titles)
}
}
func getAllTitles(repository TitleQuery) gin.HandlerFunc {
return func(ctx *gin.Context) {
// fetch titles which are available to the current user from database
titles, err := repository.SelectAll()
if err != nil {
eventID := raven.CaptureError(err, nil)
ctx.String(http.StatusInternalServerError, "Failed fetching data from DB. "+eventID)
return
}
// set http response
ctx.JSON(http.StatusOK, titles)
}
}
*/
......@@ -10,9 +10,25 @@ import (
"gitlab.akamu.de/akamu/game-server-go/schemas"
)
var ErrFetchingSessionPayload = errors.New("failed to fetch session payload")
var errFetchingUserID = errors.New("failed fetching user id")
var errFetchingUsername = errors.New("failed fetching username")
// GetSessionPayload returns the payload of the current session.
// Returns error if unable to receive the session payload.
func GetSessionPayload(ctx *gin.Context) (*schemas.TokenPayload, error) {
identity, ok := ctx.Get("identity")
if !ok {
ctx.String(http.StatusInternalServerError, "failed to fetch session payload")
raven.CaptureError(ErrFetchingSessionPayload, nil)
return nil, ErrFetchingSessionPayload
}
payload := identity.(schemas.TokenPayload)
return &payload, nil
}
// GetUserID returns the userID from the currently requesting user. Returns error and HTTP 500
// response if unable to receive the userID.
func GetUserID(ctx *gin.Context) (uint32, error) {
......
......@@ -254,6 +254,7 @@ func SetupRoutes(courseRepository course.CourseQuery, flashcardRepository flashc
avatar.SetupAvatarRoutes(authGroup.Group("/avatar"), avatarRepository)
duel.SetupDuelRoutes(authGroup.Group("/duel"), duelRepository, titleRepository, refreshjwtRepository)
resource.SetupResourceRoutes(authGroup.Group("/resource"), resourceRepository, resourceServerConfig)
refreshjwt.SetupRefreshJWTRoutes(authGroup.Group("/session"), refreshjwtRepository)
return router
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment