big refacto
This commit is contained in:
40
internal/ginserver/oauth_token.go
Normal file
40
internal/ginserver/oauth_token.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package ginserver
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"nos-comptes/internal/storage/model"
|
||||
"nos-comptes/internal/utils"
|
||||
"strings"
|
||||
|
||||
"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)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
oauth2Service, err := oauth2.New(&http.Client{})
|
||||
if oauth2Service == nil {
|
||||
fmt.Println(err)
|
||||
utils.JSONError(c.Writer, model.ErrInternalServer)
|
||||
c.Abort()
|
||||
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)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Set("googleUserId", token.UserId)
|
||||
}
|
||||
Reference in New Issue
Block a user