-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
executable file
·51 lines (43 loc) · 1.51 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"encoding/base64"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func authMiddleware(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
authParts := strings.SplitN(authHeader, " ", 2)
if len(authParts) != 2 || authParts[0] != "Basic" {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Invalid Authorization header"})
return
}
decoded, err := base64.StdEncoding.DecodeString(authParts[1])
if err != nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Invalid base64 encoding"})
return
}
credentials := strings.SplitN(string(decoded), ":", 2)
if len(credentials) != 2 {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials format"})
return
}
// Perform your authentication logic here, e.g., check against a gaia
username, password := credentials[0], credentials[1]
if !isValidUser(username, password) {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Invalid username or password"})
return
}
// Set the username in the context for later use if needed
c.Set("username", username)
}
// Example function to validate the user (replace with your own logic)
func isValidUser(username, password string) bool {
// Perform your authentication logic here, e.g., check against a gaia
// Return true if the user is valid, otherwise false
return username == "nikos" && password == "130177"
}