diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-08-26 08:53:24 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-08-26 08:53:24 +0300 |
| commit | f1b7589b2936335957a6a1da1eea3d66233ad0ce (patch) | |
| tree | 1975af217c190f5dbdb23b96015cef45206302d4 /backend/api/auth.go | |
| parent | docs: profile improvement swagger (#51) (diff) | |
| download | lphub-f1b7589b2936335957a6a1da1eea3d66233ad0ce.tar.gz lphub-f1b7589b2936335957a6a1da1eea3d66233ad0ce.tar.bz2 lphub-f1b7589b2936335957a6a1da1eea3d66233ad0ce.zip | |
refactor: reorganizing packages
Former-commit-id: 99410223654c2a5ffc15fdab6ec3e921b5410cba
Diffstat (limited to 'backend/api/auth.go')
| -rw-r--r-- | backend/api/auth.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/backend/api/auth.go b/backend/api/auth.go new file mode 100644 index 0000000..91ef80c --- /dev/null +++ b/backend/api/auth.go | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | package api | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "os" | ||
| 6 | "time" | ||
| 7 | |||
| 8 | "github.com/gin-gonic/gin" | ||
| 9 | "github.com/golang-jwt/jwt/v4" | ||
| 10 | "github.com/pektezol/leastportalshub/backend/database" | ||
| 11 | "github.com/pektezol/leastportalshub/backend/models" | ||
| 12 | ) | ||
| 13 | |||
| 14 | func CheckAuth(c *gin.Context) { | ||
| 15 | tokenString := c.GetHeader("Authorization") | ||
| 16 | // Validate token | ||
| 17 | token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { | ||
| 18 | if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { | ||
| 19 | return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) | ||
| 20 | } | ||
| 21 | return []byte(os.Getenv("SECRET_KEY")), nil | ||
| 22 | }) | ||
| 23 | if token == nil { | ||
| 24 | c.Next() | ||
| 25 | return | ||
| 26 | } | ||
| 27 | if err != nil { | ||
| 28 | c.Next() | ||
| 29 | return | ||
| 30 | } | ||
| 31 | if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { | ||
| 32 | // Check exp | ||
| 33 | if float64(time.Now().Unix()) > claims["exp"].(float64) { | ||
| 34 | c.Next() | ||
| 35 | return | ||
| 36 | } | ||
| 37 | // Get user from DB | ||
| 38 | var user models.User | ||
| 39 | database.DB.QueryRow(`SELECT u.steam_id, u.user_name, u.avatar_link, u.country_code, u.created_at, u.updated_at FROM users u WHERE steam_id = $1`, claims["sub"]).Scan( | ||
| 40 | &user.SteamID, &user.UserName, &user.AvatarLink, | ||
| 41 | &user.CountryCode, &user.CreatedAt, &user.UpdatedAt) | ||
| 42 | if user.SteamID == "" { | ||
| 43 | c.Next() | ||
| 44 | return | ||
| 45 | } | ||
| 46 | // Get user titles from DB | ||
| 47 | var moderator bool | ||
| 48 | user.Titles = []models.Title{} | ||
| 49 | rows, _ := database.DB.Query(`SELECT t.title_name, t.title_color FROM titles t INNER JOIN user_titles ut ON t.id=ut.title_id WHERE ut.user_id = $1`, user.SteamID) | ||
| 50 | for rows.Next() { | ||
| 51 | var title models.Title | ||
| 52 | rows.Scan(&title.Name, &title.Color) | ||
| 53 | if title.Name == "Moderator" { | ||
| 54 | moderator = true | ||
| 55 | } | ||
| 56 | user.Titles = append(user.Titles, title) | ||
| 57 | } | ||
| 58 | c.Set("user", user) | ||
| 59 | c.Set("mod", moderator) | ||
| 60 | c.Next() | ||
| 61 | } else { | ||
| 62 | c.Next() | ||
| 63 | return | ||
| 64 | } | ||
| 65 | } | ||