Files
badger/handlers/users.go
T

128 lines
3.2 KiB
Go

package handlers
import (
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/getsentry/sentry-go"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/prometheus/common/log"
"github.com/sirupsen/logrus"
"github.com/wahyd4/badger/models"
)
const (
githubHTTPTimeout = 10
)
type UsersHandler struct {
DB *gorm.DB
}
type UserAuthenticationRequest struct {
Name string `json:"name" binding:"required"`
GithubID string `json:"githubId" binding:"required"`
GithubUsername string `json:"githubUsername" binding:"required"`
GithubAvatarURL string `json:"githubAvatar" binding:"required"`
Email string `json:"email"`
Token string `json:"token" binding:"required"`
ExpiresAt string `json:"expiresAt"`
}
type UserInfo struct {
ID uint `json:"id"`
GithubID string `json:"githubId"`
}
func (usersHandler *UsersHandler) Authenticate(c *gin.Context) {
var requestBody UserAuthenticationRequest
if err := c.ShouldBindJSON(&requestBody); err != nil {
logrus.Error(err)
sentry.CaptureException(err)
c.JSON(http.StatusBadRequest, GeneralError{"invalid request body"})
return
}
if !validateUser(requestBody.Token) {
c.JSON(http.StatusBadRequest, GeneralError{fmt.Sprintf("invalid token for user %s", requestBody.Name)})
return
}
var user models.User
if err := usersHandler.DB.Preload("Token").First(&user, "name = ?", requestBody.Name).Error; err != nil {
if err == gorm.ErrRecordNotFound {
user = models.User{
Name: requestBody.Name,
ULID: NewULID(),
GithubID: requestBody.GithubID,
GithubUsername: requestBody.GithubUsername,
GithubAvatarURL: requestBody.GithubAvatarURL,
Email: requestBody.Email,
Token: models.Token{
Token: requestBody.Token,
ExpiresAt: requestBody.ExpiresAt,
},
}
} else {
logrus.Error(err)
sentry.CaptureException(err)
c.JSON(http.StatusInternalServerError, GeneralError{"internal error, please try it later"})
return
}
} else {
user.Token.Token = requestBody.Token
user.Token.ExpiresAt = requestBody.ExpiresAt
}
if err := usersHandler.DB.Save(&user).Error; err != nil {
logrus.Error(err)
sentry.CaptureException(err)
c.JSON(http.StatusInternalServerError, GeneralError{"internal error, please try it later"})
return
}
c.JSON(http.StatusOK, UserInfo{ID: user.ID, GithubID: user.GithubID})
}
func validateUser(token string) bool {
client := &http.Client{
Timeout: time.Second * githubHTTPTimeout,
}
req, err := http.NewRequest("GET", "https://api.github.com/user", nil)
if err != nil {
logrus.Error(err)
sentry.CaptureException(err)
return false
}
req.Header.Add("Accept", "application/vnd.github.v3+json")
req.Header.Add("Authorization", fmt.Sprintf("token %s", token))
resp, err := client.Do(req)
if err != nil {
logrus.Error(err)
sentry.CaptureException(err)
return false
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logrus.Error(err)
sentry.CaptureException(err)
return false
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
log.Warnf("request with error %s", body)
return false
}
logrus.Info()
return true
}