Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu <[email protected]>
  • Loading branch information
mathieu-brl committed Dec 9, 2024
1 parent 34bb8fc commit 227774e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 26 deletions.
55 changes: 47 additions & 8 deletions server/internal/controllers/oauth.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package controllers

import (
"AREA/internal/controllers/oauth"
"AREA/internal/models"
"AREA/internal/pkg"
"AREA/internal/controllers/oauth"
"AREA/internal/utils"
"errors"
"fmt"
"net/http"

Expand All @@ -20,26 +22,64 @@ type Token struct {
Token string `json:"token" binding:"required"`
}

type OAuthCallback func(c *gin.Context, token *models.Token)
type OAuthCallback func(c *gin.Context, token *models.Token) (*models.User, error)

var OAuthCallbacks = map[string]OAuthCallback {
//"google": googleCallback,
"microsoft": oauth.MicrosoftCallback,
}

func getServiceID(c *gin.Context) (uint, error) {
serviceId, err := pkg.GetServiceFromName(c.Param("service"))
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H {
"message": "Service not found",
})
return 0, errors.New("Invalid request")
}
return serviceId, nil
}

func OAuth(c *gin.Context) {
var token Token
err := c.ShouldBindJSON(&token)
if err := c.ShouldBindJSON(&token); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H {
"message": "Invalid request",
})
}


serviceId, err := getServiceID(c)
if err != nil {
return
}

var dbToken models.Token
dbToken.Value = token.Token
dbToken.ServiceID = serviceId
user, err := OAuthCallbacks[c.Param("service")](c, &dbToken)
dbToken.UserID = user.ID
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}

tokenString := utils.NewToken(c, user.Email)
c.JSON(http.StatusOK, gin.H{"username": user.Username, "email": user.Email, "jwt": tokenString})
}

/*func OAuthBind(c *gin.Context) {
var token Token
if err := c.ShouldBindJSON(&token); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H {
"message": "Invalid request",
})
return
}
userId, err := pkg.GetUserFromToken(c)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H {
"message": "User not found",
})
return
}
serviceId, err := pkg.GetServiceFromName(c.Param("service"))
if err != nil {
Expand All @@ -48,10 +88,9 @@ func OAuth(c *gin.Context) {
})
return
}
var dbToken models.Token
dbToken.Value = token.Token
dbToken.UserID = userId
dbToken.ServiceID = serviceId
OAuthCallbacks[c.Param("service")](c, &dbToken)
pkg.DB.Create(&dbToken)
}
}*/
37 changes: 30 additions & 7 deletions server/internal/controllers/oauth/microsoft.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,69 @@ import (
"AREA/internal/models"
"AREA/internal/pkg"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"

"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

type MicrosoftResponse struct {
Mail string `json:"mail"`
DisplayName string `json:"displayName"`
}

func getBindedAccount(token *models.Token) (models.User, error) {
func createAccount(response MicrosoftResponse, token *models.Token) (*models.User) {
var user models.User
//user.Token = token.Value
user.Email = token.Email
user.Username = response.DisplayName
pkg.DB.Create(&user)
token.UserID = user.ID
pkg.DB.Create(&token)
return &user
}

func getBindedAccount(response MicrosoftResponse, token *models.Token) (*models.User, error) {
var user models.User

err := pkg.DB.Where("email = ? AND service_id = 3", token.Email).First(token).Error

pkg.DB
return user, nil
if errors.Is(err, gorm.ErrRecordNotFound) {
return createAccount(response, token), nil
} else {
pkg.DB.Where("id = ?", token.UserID).First(&user)
pkg.DB.Where("email = ?", token.Email).First(token).Update("value", token.Value)
return &user, nil
}
}

func MicrosoftCallback(c *gin.Context, token *models.Token) {
func MicrosoftCallback(c *gin.Context, token *models.Token) (*models.User, error) {
httpRequestUrl := "https://graph.microsoft.com/v1.0/me"
req, err := http.NewRequest("GET", httpRequestUrl, nil)
if err != nil {
fmt.Println(err)
return
err := errors.New("Error creating request")
return nil, err

}
req.Header.Set("Authorization", "Bearer " + token.Value)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
return nil, errors.New("Error executing request")
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
return nil, errors.New("Error reading request")
}
var response MicrosoftResponse
json.Unmarshal([]byte(b), &response)
token.Email = response.Mail
return getBindedAccount(response, token)
}
2 changes: 1 addition & 1 deletion server/internal/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

type User struct {
gorm.Model
Email string `gorm:"unique" json:"email" binding:"required"`
Email string `json:"email" binding:"required"`
Username string `gorm:"unique" json:"username" binding:"required"`
Password string `gorm:"not null" json:"password" binding:"required"`
Salt string `gorm:"not null" json:"salt"`
Expand Down
11 changes: 1 addition & 10 deletions server/internal/routers/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@ import (

func setUpOauthGroup(router *gin.Engine) {
router.POST("/oauth/:service", controllers.OAuth)
/*oauth := router.Group("/oauth")
{
oauth.POST("/google", controllers.Google)
/*oauth.POST("/spotify", controllers.Spotify)
oauth.POST("/github", controllers.Github)
oauth.POST("/linkedin", controllers.Linkedin)
oauth.POST("/discord", controllers.Discord)
auth.POST("/twitch", controllers.Twitch)
}*/
//router.POST("/oauth/bind/:service", controllers.OAuthBind)
}

func setUpAuthGroup(router *gin.Engine) {
Expand Down

0 comments on commit 227774e

Please sign in to comment.