chore: init repository

This commit is contained in:
2022-10-06 15:37:28 +02:00
commit 833851f695
38 changed files with 2302 additions and 0 deletions

38
middleware/oauth_token.go Normal file
View File

@@ -0,0 +1,38 @@
package middleware
import (
"fmt"
"john/internal/storage/model"
"john/internal/utils"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"google.golang.org/api/oauth2/v2"
)
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.New(&http.Client{})
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)
}