Skip to content

Commit

Permalink
feat(token): add token model
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu <[email protected]>
  • Loading branch information
mathieu-brl committed Dec 8, 2024
1 parent ae918f4 commit c45705d
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 4 deletions.
61 changes: 61 additions & 0 deletions server/build/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# TODO RESTORE
services:
rabbitmq:
image: rabbitmq:3-management
ports:
- "8082:15672"
- "5000:5673"
networks:
- app_network
volumes:
- ./rabbit-mq/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
healthcheck:
test: [ "CMD", "rabbitmqctl", "status" ]
interval: 5s
timeout: 15s
retries: 5

mariadb:
image: mariadb:10.4
ports:
- "3305:3306"
environment:
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_DATABASE: ${DB_NAME}
volumes:
- mariadb_data:/var/lib/mysql
networks:
- app_network
healthcheck:
test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ]
interval: 10s
timeout: 5s
retries: 5

# go-app:
# build:
# context: ../.
# dockerfile: Dockerfile
# ports:
# - "8080:8080"
# environment:
# DB_HOST: mariadb
# DB_PORT: 3306
# DB_NAME: ${DB_NAME}
# DB_USER: root
# DB_PASSWORD: ${DB_PASSWORD}
# SECRET_KEY: ${SECRET_KEY}
# depends_on:
# mariadb:
# condition: service_healthy
# rabbitmq:
# condition: service_healthy
# networks:
# - app_network

volumes:
mariadb_data:

networks:
app_network:
driver: bridge
32 changes: 32 additions & 0 deletions server/internal/controllers/oauth.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package controllers

import (
"AREA/internal/pkg"
"fmt"
"net/http"

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

Expand All @@ -9,3 +13,31 @@ func Google(c *gin.Context) {
"message": "Google",
})
}

type Token struct {
Token string
}

func OAuth(c *gin.Context) {
var token Token
err := c.ShouldBindJSON(&token)
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
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 {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H {
"message": "Service not found",
})
return
}
fmt.Println(userId, serviceId)
}
3 changes: 2 additions & 1 deletion server/internal/models/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import "gorm.io/gorm"
type Service struct {
gorm.Model
Name string `json:"name"`
Events []Event `gorm:"constraint:OnDelete:CASCADE;" json:"events"`
Events []Event `gorm:"constraint:OnDelete:CASCADE;" json:"events"`
Tokens []Token `gorm:"constraint:OnDelete:CASCADE;" json:"tokens"`
}
10 changes: 10 additions & 0 deletions server/internal/models/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

import "gorm.io/gorm"

type Token struct {
gorm.Model
Value string
ServiceID uint
UserID uint
}
2 changes: 2 additions & 0 deletions server/internal/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ type User struct {
Password string `gorm:"not null" json:"password" binding:"required"`
Salt string `gorm:"not null" json:"salt"`
Token string `gorm:"not null" json:"token"`
Workflows []Workflow `gorm:"constraint:OnDelete:CASCADE;"`
Tokens []Token `gorm:"constraint:OnDelete:CASCADE;"`
}
1 change: 1 addition & 0 deletions server/internal/pkg/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func migrateDB() error {
&models.Service{},
&models.Event{},
&models.Parameters{},
&models.Token{},
)
if err != nil {
log.Fatalf("Failed to migrate DB: %v", err)
Expand Down
8 changes: 8 additions & 0 deletions server/internal/pkg/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ func GetUserFromToken(c *gin.Context) (uint, error) {
}
return user.ID, nil
}

func GetServiceFromName(serviceName string) (uint, error) {
service := models.Service{}
if err := DB.Where("name = ?", serviceName).First(&service); err != nil {
return 0, errors.New("Service not found")
}
return service.ID, nil
}
7 changes: 4 additions & 3 deletions server/internal/routers/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import (
)

func setUpOauthGroup(router *gin.Engine) {
oauth := router.Group("/oauth")
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)*/
}
auth.POST("/twitch", controllers.Twitch)
}*/
}

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

0 comments on commit c45705d

Please sign in to comment.