Skip to content

Commit

Permalink
fix prefixing bearer token twice
Browse files Browse the repository at this point in the history
  • Loading branch information
franzmueller committed Jan 22, 2025
1 parent 44c2c32 commit 2518eb8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
14 changes: 12 additions & 2 deletions lib/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ package client
import (
"encoding/json"
"fmt"
"github.com/SENERGY-Platform/import-deploy/lib/model"
"github.com/SENERGY-Platform/service-commons/pkg/jwt"
"io"
"net/http"
"strings"

"github.com/SENERGY-Platform/import-deploy/lib/model"
"github.com/SENERGY-Platform/service-commons/pkg/jwt"
)

type Interface interface {
Expand Down Expand Up @@ -59,3 +61,11 @@ func do[T any](req *http.Request) (result T, err error, code int) {
}
return
}

func prefixTokenIfNeeded(jwt jwt.Token) string {
s := jwt.Jwt()
if !strings.HasPrefix(strings.ToLower(s), "bearer ") {
s = "Bearer " + s
}
return s
}
12 changes: 6 additions & 6 deletions lib/client/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *Client) ListInstances(jwt jwt.Token, limit int64, offset int64, sort st
if err != nil {
return results, err, http.StatusBadRequest
}
req.Header.Set("Authorization", "Bearer "+jwt.Jwt())
req.Header.Set("Authorization", prefixTokenIfNeeded(jwt))
return do[[]model.Instance](req)
}

Expand All @@ -52,7 +52,7 @@ func (c *Client) ReadInstance(id string, jwt jwt.Token, forUser string) (result
if err != nil {
return result, err, http.StatusBadRequest
}
req.Header.Set("Authorization", "Bearer "+jwt.Jwt())
req.Header.Set("Authorization", prefixTokenIfNeeded(jwt))
return do[model.Instance](req)
}

Expand All @@ -65,7 +65,7 @@ func (c *Client) CreateInstance(instance model.Instance, jwt jwt.Token) (result
if err != nil {
return result, err, http.StatusInternalServerError
}
req.Header.Set("Authorization", "Bearer "+jwt.Jwt())
req.Header.Set("Authorization", prefixTokenIfNeeded(jwt))
return do[model.Instance](req)
}

Expand All @@ -78,7 +78,7 @@ func (c *Client) SetInstance(importType model.Instance, jwt jwt.Token) (err erro
if err != nil {
return err, http.StatusInternalServerError
}
req.Header.Set("Authorization", "Bearer "+jwt.Jwt())
req.Header.Set("Authorization", prefixTokenIfNeeded(jwt))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err, http.StatusInternalServerError
Expand All @@ -91,7 +91,7 @@ func (c *Client) DeleteInstance(id string, jwt jwt.Token, forUser string) (err e
if err != nil {
return err, http.StatusBadRequest
}
req.Header.Set("Authorization", "Bearer "+jwt.Jwt())
req.Header.Set("Authorization", prefixTokenIfNeeded(jwt))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err, http.StatusInternalServerError
Expand All @@ -107,6 +107,6 @@ func (c *Client) CountInstances(jwt jwt.Token, search string, includeGenerated b
if err != nil {
return 0, err, http.StatusBadRequest
}
req.Header.Set("Authorization", "Bearer "+jwt.Jwt())
req.Header.Set("Authorization", prefixTokenIfNeeded(jwt))
return do[int64](req)
}

0 comments on commit 2518eb8

Please sign in to comment.