Files
budget-backend/middleware/oauth_token.go
kratisto f81c902ca6
Some checks failed
golangci-lint / lint (push) Successful in 1m33s
Test / test (push) Failing after 2m16s
chore: migrate to gitea
2026-01-27 01:56:53 +01:00

38 lines
1003 B
Go

package middleware
import (
"fmt"
"strings"
"gitea.frenchtouch.duckdns.org/kratisto/budget-backend/internal/storage/model"
"gitea.frenchtouch.duckdns.org/kratisto/budget-backend/internal/utils"
"github.com/gin-gonic/gin"
"google.golang.org/api/oauth2/v1"
)
func ValidateOAuthToken(c *gin.Context) {
authorizationHeader := c.GetHeader("Authorization")
authorizationHeaderSplitted := strings.Split(authorizationHeader, " ")
if len(authorizationHeaderSplitted) != 2 {
utils.JSONError(c.Writer, model.ErrBadRequestFormat)
return
}
oauth2Service, err := oauth2.NewService(c)
if oauth2Service == nil {
fmt.Println(err)
utils.JSONError(c.Writer, model.ErrInternalServer)
return
}
tokenInfoCall := oauth2Service.Tokeninfo()
tokenInfoCall.IdToken(authorizationHeaderSplitted[1])
token, err := tokenInfoCall.Do()
if err != nil {
utils.GetLogger().WithError(err).Error(err)
utils.JSONError(c.Writer, model.ErrBadRequestFormat)
return
}
c.Set("googleUserId", token.UserId)
}