Files
opencloud/services/graph/pkg/middleware/token.go
Florian Schade ad06a192d8 enhancement: add graph beta listPermissions endpoint (#7753)
* enhancement: add graph beta listPermissions endpoint

besides the new api endpoint it includes several utilities to simplify the graph api development.

* resolve drive and item id from the request path
* generic pointer and value utilities
* space root detection

* update GetDriveAndItemIDParam signature to return a error

* move errorcode package

* enhancement: add generic error code handling

* fix: rebase
2023-11-28 17:06:04 +01:00

46 lines
1.1 KiB
Go

package middleware
import (
"crypto/sha256"
"crypto/subtle"
"net/http"
"github.com/owncloud/ocis/v2/services/graph/pkg/errorcode"
)
var (
// ErrInvalidToken is returned when the request token is invalid.
ErrInvalidToken = "invalid or missing token"
)
// Token provides a middleware to check access secured by a static token.
func Token(token string) func(http.Handler) http.Handler {
h := sha256.New()
requiredTokenHash := h.Sum(([]byte("Bearer " + token)))
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if token == "" {
next.ServeHTTP(w, r)
return
}
header := r.Header.Get("Authorization")
if header == "" {
errorcode.InvalidAuthenticationToken.Render(w, r, http.StatusUnauthorized, ErrInvalidToken)
return
}
h = sha256.New()
providedTokenHash := h.Sum([]byte(header))
if subtle.ConstantTimeCompare(requiredTokenHash, providedTokenHash) == 0 {
errorcode.InvalidAuthenticationToken.Render(w, r, http.StatusUnauthorized, ErrInvalidToken)
return
}
next.ServeHTTP(w, r)
})
}
}