-
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update AuthMiddleware to handle Bearer token
This commit updates the AuthMiddleware function in the middlewares package. It modifies the logic to handle Bearer tokens instead of a generic token. The function now checks if the Authorization header starts with "Bearer " and extracts the token accordingly. It also checks if the extracted token matches the expected token. If not, it returns a 401 unauthorized response. This change improves the security and authentication process in the application.
- Loading branch information
1 parent
6add212
commit 2fa7b08
Showing
13 changed files
with
141 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package listeners | ||
|
||
import "go.uber.org/zap" | ||
|
||
// EncryptedListener represents an encrypted shell listener. | ||
type EncryptedListener struct { | ||
commonListener | ||
} | ||
|
||
// NewEncryptedListener creates a new encrypted shell listener. | ||
func NewEncryptedListener(host string, port uint16) *EncryptedListener { | ||
return &EncryptedListener{ | ||
commonListener: commonListener{ | ||
BindHost: host, | ||
BindPort: port, | ||
}, | ||
} | ||
} | ||
|
||
// Start starts the encrypted shell listener. | ||
func (l *EncryptedListener) Start(logger *zap.Logger) { | ||
logger.Info("starting encrypted listener", zap.String("host", l.BindHost), zap.Uint16("port", l.BindPort)) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,6 @@ | ||
package listeners | ||
|
||
// ListenerType represents the type of listener. | ||
type ListenerType string | ||
|
||
const ( | ||
// ListenerTypePlainShell represents a plain shell listener. | ||
ListenerTypePlainShell ListenerType = "plain_shell" | ||
// ListenerTypeEncryptedShell represents an encrypted shell listener. | ||
ListenerTypeEncryptedShell ListenerType = "encrypted_shell" | ||
// ListenerTypeRESTful represents a RESTful listener. | ||
ListenerTypeRESTful ListenerType = "restful" | ||
) | ||
|
||
// Listener is a struct that represents a listener | ||
type Listener struct { | ||
BindHost string `json:"bind_host" yaml:"bind_host" toml:"bind_host"` | ||
BindPort uint16 `json:"bind_port" yaml:"bind_port" toml:"bind_port"` | ||
Type ListenerType `json:"type" yaml:"type" toml:"type"` | ||
type commonListener struct { | ||
BindHost string `json:"bind_host" yaml:"bind_host" toml:"bind_host"` | ||
BindPort uint16 `json:"bind_port" yaml:"bind_port" toml:"bind_port"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package listeners | ||
|
||
import "go.uber.org/zap" | ||
|
||
// PlainListener represents a plain shell listener. | ||
type PlainListener struct { | ||
commonListener | ||
} | ||
|
||
// NewPlainListener creates a new plain shell listener. | ||
func NewPlainListener(host string, port uint16) *PlainListener { | ||
return &PlainListener{ | ||
commonListener: commonListener{ | ||
BindHost: host, | ||
BindPort: port, | ||
}, | ||
} | ||
} | ||
|
||
// Start starts the plain shell listener. | ||
func (l *PlainListener) Start(logger *zap.Logger) { | ||
logger.Info("starting plain listener", zap.String("host", l.BindHost), zap.Uint16("port", l.BindPort)) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,37 @@ | ||
package middlewares | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// AuthMiddleware is a middleware that checks if the request has the correct token. | ||
func AuthMiddleware(token string) gin.HandlerFunc { | ||
// AuthMiddleware is a middleware that checks if the request has the correct Bearer token. | ||
func AuthMiddleware(expectedToken string) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
if c.GetHeader("Authorization") != token { | ||
// Get the Authorization header | ||
authorizationHeader := c.GetHeader("Authorization") | ||
if authorizationHeader == "" { | ||
c.AbortWithStatusJSON(401, gin.H{"error": "missing authorization header"}) | ||
return | ||
} | ||
|
||
// Check if the header starts with "Bearer " and extract the token | ||
if !strings.HasPrefix(authorizationHeader, "Bearer ") { | ||
c.AbortWithStatusJSON(401, gin.H{"error": "invalid authorization scheme"}) | ||
return | ||
} | ||
|
||
// Extract the token | ||
token := strings.TrimPrefix(authorizationHeader, "Bearer ") | ||
|
||
// Check if the token matches the expected token | ||
if token != expectedToken { | ||
c.AbortWithStatusJSON(401, gin.H{"error": "unauthorized"}) | ||
return | ||
} | ||
|
||
// Token is valid; proceed to the next handler | ||
c.Next() | ||
} | ||
} |
Oops, something went wrong.