added support for passing different bearer tokens
This commit is contained in:
parent
e11b297ae7
commit
a865a3ba2d
@ -10,7 +10,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/config"
|
||||
@ -23,8 +22,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
|
||||
logg = slogging.Get().With("component", "sarafu-api.devapi")
|
||||
logg = slogging.Get().With("component", "sarafu-api.devapi")
|
||||
)
|
||||
|
||||
type APIError struct {
|
||||
@ -32,6 +30,10 @@ type APIError struct {
|
||||
Description string
|
||||
}
|
||||
|
||||
type ctxKey string
|
||||
|
||||
const ctxKeyAuthToken ctxKey = "authToken"
|
||||
|
||||
func (e *APIError) Error() string {
|
||||
if e.Code != "" {
|
||||
return fmt.Sprintf("[%s] %s", e.Code, e.Description)
|
||||
@ -787,7 +789,13 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
|
||||
var okResponse api.OKResponse
|
||||
var errResponse api.ErrResponse
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+config.BearerToken)
|
||||
// Check if a custom Authorization token was provided
|
||||
if token, ok := ctx.Value(ctxKeyAuthToken).(string); ok && token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
} else {
|
||||
req.Header.Set("Authorization", "Bearer "+config.BearerToken)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Log request
|
||||
|
||||
Loading…
Reference in New Issue
Block a user