1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-02 22:17:10 +08:00
stefankovacevic123 5ca8495f35
MF-1469 - Indicate proper authentication scheme in Authorization header (#1523)
* MF-1469 - Indicate proper authentication scheme in Authorization header

Signed-off-by: Stefan Kovacevic <jen2tri@gmail.com>

* Fixing the remarks on the last push

Signed-off-by: Stefan Kovacevic <jen2tri@gmail.com>

* Remove Bearer prefix in all services and fix tests

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix remarks

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

Co-authored-by: Manuel Imperiale <manuel.imperiale@gmail.com>
2022-02-18 14:56:01 +01:00

26 lines
649 B
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package httputil
import (
"net/http"
"strings"
"github.com/mainflux/mainflux/pkg/errors"
)
// BearerPrefix represents the token prefix for Bearer authentication scheme.
const BearerPrefix = "Bearer "
// ExtractAuthToken reads the value of request Authorization and removes the Bearer substring or returns error if it does not exist
func ExtractAuthToken(r *http.Request) (string, error) {
token := r.Header.Get("Authorization")
if !strings.HasPrefix(token, BearerPrefix) {
return token, errors.ErrAuthentication
}
return strings.TrimPrefix(token, BearerPrefix), nil
}