mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
Heartbeat Insertions.
Restructuring.
This commit is contained in:
38
middlewares/authenticate.go
Normal file
38
middlewares/authenticate.go
Normal file
@ -0,0 +1,38 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/n1try/wakapi/models"
|
||||
"github.com/n1try/wakapi/services"
|
||||
)
|
||||
|
||||
type AuthenticateMiddleware struct {
|
||||
UserSrvc *services.UserService
|
||||
}
|
||||
|
||||
func (m *AuthenticateMiddleware) Handle(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||
authHeader := strings.Split(r.Header.Get("Authorization"), " ")
|
||||
if len(authHeader) != 2 {
|
||||
w.WriteHeader(401)
|
||||
return
|
||||
}
|
||||
|
||||
key, err := base64.StdEncoding.DecodeString(authHeader[1])
|
||||
if err != nil {
|
||||
w.WriteHeader(401)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := m.UserSrvc.GetUserByKey(strings.TrimSpace(string(key)))
|
||||
if err != nil {
|
||||
w.WriteHeader(401)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), models.UserKey, &user)
|
||||
next(w, r.WithContext(ctx))
|
||||
}
|
Reference in New Issue
Block a user