2021-09-22 13:36:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-16 15:43:37 +00:00
|
|
|
"fmt"
|
2021-09-22 13:36:45 +00:00
|
|
|
"net/http"
|
2024-08-14 13:53:09 +00:00
|
|
|
"strings"
|
2021-09-22 13:36:45 +00:00
|
|
|
)
|
|
|
|
|
2024-06-16 15:43:37 +00:00
|
|
|
func getAuthorizationFromRequest(r *http.Request) (string, error) {
|
2024-08-14 13:53:09 +00:00
|
|
|
_, token, hadPrefix := strings.Cut(r.Header.Get("Authorization"), " ")
|
|
|
|
if !hadPrefix {
|
2024-06-16 15:43:37 +00:00
|
|
|
return "", fmt.Errorf("no authorization provided")
|
2021-10-22 15:09:39 +00:00
|
|
|
}
|
2021-09-22 13:36:45 +00:00
|
|
|
|
2024-06-16 15:43:37 +00:00
|
|
|
_, user, _, _, err := editorTokenService.ValidateLoginToken(token) //nolint:dogsled // Required at other places
|
2024-08-14 13:53:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("getting authorized user: %w", err)
|
|
|
|
}
|
2024-06-16 15:43:37 +00:00
|
|
|
|
|
|
|
if user == "" {
|
|
|
|
user = "API-User"
|
|
|
|
}
|
2021-09-22 13:36:45 +00:00
|
|
|
|
2024-08-14 13:53:09 +00:00
|
|
|
return user, nil
|
2021-10-22 15:09:39 +00:00
|
|
|
}
|