Skip to content

Commit

Permalink
feat: support multi-geo for Konnect
Browse files Browse the repository at this point in the history
  • Loading branch information
GGabriele committed Aug 19, 2022
1 parent 49d2f98 commit 1f5a979
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
### Added

- Add support to multi-geo for Konnect.
[#732](https://github.com/Kong/deck/pull/732)
- Support PAT (Personal Access Tokens) for Konnect authentication.
[#710](https://github.com/Kong/deck/pull/710)

Expand Down
40 changes: 33 additions & 7 deletions konnect/login_service.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package konnect

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
)

type user struct {
Expand Down Expand Up @@ -54,17 +56,41 @@ func (s *AuthService) Login(ctx context.Context, email,
return authResponse, nil
}

// getGlobalAuthEndpoint returns the global auth endpoint
// given a base Konnect URL.
func getGlobalAuthEndpoint(baseURL string) string {
parts := strings.Split(baseURL, "api.konghq")
return baseEndpointUS + parts[len(parts)-1] + authEndpointV2
}

func createAuthRequest(baseURL, email, password string) (*http.Request, error) {
var (
buf []byte
err error

body = map[string]string{
"username": email,
"password": password,
}
)

buf, err = json.Marshal(body)
if err != nil {
return nil, err
}

endpoint := getGlobalAuthEndpoint(baseURL)
return http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(buf))
}

func (s *AuthService) sessionAuth(ctx context.Context, email,
password string,
) (AuthResponse, error) {
body := map[string]string{
"username": email,
"password": password,
}
req, err := s.client.NewRequest(http.MethodPost, authEndpointV2, nil, body)
req, err := createAuthRequest(s.client.baseURL, email, password)
if err != nil {
return AuthResponse{}, fmt.Errorf("build http request: %v", err)
return AuthResponse{}, err
}

var authResponse AuthResponse
resp, err := s.client.Do(ctx, req, &authResponse)
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions konnect/login_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package konnect

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetGlobalAuthEndpoint(t *testing.T) {
tests := []struct {
baseURL string
expected string
}{
{
baseURL: "https://us.api.konghq.com",
expected: "https://global.api.konghq.com/kauth/api/v1/authenticate",
},
{
baseURL: "https://global.api.konghq.com",
expected: "https://global.api.konghq.com/kauth/api/v1/authenticate",
},
{
baseURL: "https://eu.api.konghq.com",
expected: "https://global.api.konghq.com/kauth/api/v1/authenticate",
},
{
baseURL: "https://api.konghq.com",
expected: "https://global.api.konghq.com/kauth/api/v1/authenticate",
},
{
baseURL: "https://eu.api.konghq.test",
expected: "https://global.api.konghq.test/kauth/api/v1/authenticate",
},
}
for _, tt := range tests {
assert.Equal(t, tt.expected, getGlobalAuthEndpoint(tt.baseURL))
}
}
1 change: 1 addition & 0 deletions konnect/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

const (
baseEndpointUS = "https://global.api.konghq"
authEndpoint = "/api/auth"
authEndpointV2 = "/kauth/api/v1/authenticate"
)
Expand Down

0 comments on commit 1f5a979

Please sign in to comment.