Skip to content

Commit

Permalink
Add API key authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
barreiro authored and lampajr committed Nov 7, 2024
1 parent 5e588e1 commit 63b4ab0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/horreum/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
type HorreumCredentials struct {
Username *string
Password *string
ApiKey *string
}

func NewDefaultHorreumCredentials() HorreumCredentials {
return HorreumCredentials{
Username: nil,
Password: nil,
ApiKey: nil,
}
}

Expand All @@ -25,6 +27,8 @@ const (
BEARER = iota
// BASIC encodes username and password in the HTTP request
BASIC
// API_KEY authenticate with Horreum tokens
API_KEY
)

type ClientConfiguration struct {
Expand Down
7 changes: 7 additions & 0 deletions pkg/horreum/horreum.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ func NewHorreumClient(baseUrl string, credentials *HorreumCredentials, clientCon
log.Default().Println("Using Basic HTTP authentication")
client.AuthProvider = provider
}
} else if credentials.ApiKey != nil && (clientConfig == nil || clientConfig.AuthMethod == API_KEY) {
provider, err := authentication.NewApiKeyAuthenticationProvider(*credentials.ApiKey, "X-Horreum-API-Key", authentication.HEADER_KEYLOCATION)
if err != nil {
return nil, fmt.Errorf("error setting up auth provider: %w", err)
}
log.Default().Println("Using API key authentication")
client.AuthProvider = provider
} else if credentials.Password != nil {
return nil, fmt.Errorf("provided password without username")
}
Expand Down
34 changes: 34 additions & 0 deletions pkg/horreum/horreum_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package horreum

import (
"context"
"github.com/microsoft/kiota-abstractions-go/authentication"
"net/url"
"testing"

Expand Down Expand Up @@ -136,6 +137,39 @@ func TestAddAndDeleteTest(t *testing.T) {
a.EqualValues(0, *list.GetCount())
}

func TestAPIKeyAuthSetup(t *testing.T) {
client, _ := NewHorreumClient("http://localhost:8080", &HorreumCredentials{
Username: &username,
Password: &password,
}, &ClientConfiguration{
AuthMethod: BEARER,
})

keyRequest := api.NewUserApikeyPostRequestBody()
keyRequest.SetName(of("Go client test key"))
keyRequest.SetTypeEscaped(of(models.USER_KEYTYPE))

key, err := client.RawClient.Api().User().Apikey().Post(context.Background(), keyRequest, nil)
assert.Nil(t, err)
assert.NotEmpty(t, key)

apiClient, err := NewHorreumClient("http://localhost:8080", &HorreumCredentials{
ApiKey: key,
}, &ClientConfiguration{AuthMethod: API_KEY})

assert.IsType(t, &authentication.ApiKeyAuthenticationProvider{}, apiClient.AuthProvider)

// make sure the API Key header is added correctly to a dummy request
req := abstractions.NewRequestInformationWithMethodAndUrlTemplateAndPathParameters(abstractions.GET, "https://localhost:8080", make(map[string]string))
apiClient.AuthProvider.AuthenticateRequest(context.Background(), req, nil)
assert.True(t, req.Headers.ContainsKey("X-Horreum-API-Key"))
assert.Equal(t, *key, req.Headers.Get("X-Horreum-API-Key")[0])

// kiota go client requires https which prevents using the API Key for integration test (with a dev Horreum instance)
_, apiErr := apiClient.RawClient.Api().User().Apikey().Get(context.Background(), nil)
assert.NotNil(t, apiErr)
}

// of returns a pointer to the provided literal/const input
func of[E any](e E) *E {
return &e
Expand Down

0 comments on commit 63b4ab0

Please sign in to comment.