1
|
|
|
package auth |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"fmt" |
5
|
|
|
"github.com/gofiber/fiber/v2" |
6
|
|
|
"github.com/golang-jwt/jwt" |
7
|
|
|
"github.com/memnix/memnixrest/pkg/database" |
8
|
|
|
"github.com/memnix/memnixrest/pkg/logger" |
9
|
|
|
"github.com/memnix/memnixrest/pkg/models" |
10
|
|
|
"os" |
11
|
|
|
"strings" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
var SecretKey string // SecretKey env variable |
15
|
|
|
var _ bool // AuthDebugMode env variable |
16
|
|
|
|
17
|
|
|
func Init() { |
18
|
|
|
SecretKey = os.Getenv("SECRET") // SecretKey env variable |
19
|
|
|
_ = os.Getenv("AUTH_DEBUG") == "true" // AuthDebugMode env variable |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
// extractToken function to extract token from header |
23
|
|
|
func extractToken(c *fiber.Ctx) string { |
24
|
|
|
token := c.Get("Authorization") // Get token from header |
25
|
|
|
// Normally Authorization HTTP header. |
26
|
|
|
onlyToken := strings.Split(token, " ") // Split token |
27
|
|
|
if len(onlyToken) == 2 { |
28
|
|
|
return onlyToken[1] // Return only token |
29
|
|
|
} |
30
|
|
|
return "" // Return empty string |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// jwtKeyFunc function to get the key for the token |
34
|
|
|
func jwtKeyFunc(_ *jwt.Token) (interface{}, error) { |
35
|
|
|
return []byte(SecretKey), nil // Return secret key |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// IsConnected function to check if user is connected |
39
|
|
|
func IsConnected(c *fiber.Ctx) (int, models.ResponseAuth) { |
40
|
|
|
db := database.DBConn // DB Conn |
41
|
|
|
tokenString := extractToken(c) // Extract token |
42
|
|
|
var user models.User // User object |
43
|
|
|
|
44
|
|
|
// Parse token |
45
|
|
|
token, err := jwt.Parse(tokenString, jwtKeyFunc) |
46
|
|
|
if err != nil { |
47
|
|
|
// Return error |
48
|
|
|
return fiber.StatusForbidden, models.ResponseAuth{ |
49
|
|
|
Success: false, |
50
|
|
|
Message: "Failed to get the user. Try to logout/login. Otherwise, contact the support", |
51
|
|
|
User: user, |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
// Check if token is valid |
55
|
|
|
claims := token.Claims.(jwt.MapClaims) |
56
|
|
|
|
57
|
|
|
// Get user from token |
58
|
|
|
if res := db.Where("id = ?", claims["iss"]).First(&user); res.Error != nil { |
59
|
|
|
// Generate log |
60
|
|
|
log := logger.CreateLog(fmt.Sprintf("Error on check auth: %s", res.Error), logger.LogLoginError).SetType(logger.LogTypeError).AttachIDs(user.ID, 0, 0) |
61
|
|
|
_ = log.SendLog() // Send log |
62
|
|
|
c.Status(fiber.StatusInternalServerError) // InternalServerError Status |
63
|
|
|
// return error |
64
|
|
|
return fiber.StatusInternalServerError, models.ResponseAuth{ |
65
|
|
|
Success: false, |
66
|
|
|
Message: "Failed to get the user. Try to logout/login. Otherwise, contact the support", |
67
|
|
|
User: user, |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// User is connected |
72
|
|
|
return fiber.StatusOK, models.ResponseAuth{ |
73
|
|
|
Success: true, |
74
|
|
|
Message: "User is connected", |
75
|
|
|
User: user, |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|