mirror of
https://github.com/absmach/magistrala.git
synced 2026-06-23 04:10:28 +00:00
f0d014eba2
Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>
38 lines
974 B
Go
38 lines
974 B
Go
// Copyright (c) Abstract Machines
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package util
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// BearerPrefix represents the token prefix for Bearer authentication scheme.
|
|
const BearerPrefix = "Bearer "
|
|
|
|
// ClientPrefix represents the key prefix for Client authentication scheme.
|
|
const ClientPrefix = "Client "
|
|
|
|
// ExtractBearerToken returns value of the bearer token. If there is no bearer token - an empty value is returned.
|
|
func ExtractBearerToken(r *http.Request) string {
|
|
token := r.Header.Get("Authorization")
|
|
|
|
if !strings.HasPrefix(token, BearerPrefix) {
|
|
return ""
|
|
}
|
|
|
|
return strings.TrimPrefix(token, BearerPrefix)
|
|
}
|
|
|
|
// ExtractClientSecret returns value of the client secret. If it's not present - an empty value is returned.
|
|
func ExtractClientSecret(r *http.Request) string {
|
|
token := r.Header.Get("Authorization")
|
|
|
|
if !strings.HasPrefix(token, ClientPrefix) {
|
|
return ""
|
|
}
|
|
|
|
return strings.TrimPrefix(token, ClientPrefix)
|
|
}
|