Path: blob/main/components/public-api-server/pkg/auth/auth.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package auth56import (7"errors"8"fmt"9"net/http"10"strings"11)1213var (14NoAccessToken = errors.New("missing access token")15InvalidAccessToken = errors.New("invalid access token")16)1718const bearerPrefix = "Bearer "19const authorizationHeaderKey = "Authorization"2021func BearerTokenFromHeaders(h http.Header) (string, error) {22authorization := strings.TrimSpace(h.Get(authorizationHeaderKey))23if authorization == "" {24return "", fmt.Errorf("empty authorization header: %w", NoAccessToken)25}2627if !strings.HasPrefix(authorization, bearerPrefix) {28return "", fmt.Errorf("authorization header does not have a Bearer prefix: %w", NoAccessToken)29}3031return strings.TrimPrefix(authorization, bearerPrefix), nil32}333435