Skip to content

Commit

Permalink
remove generated client
Browse files Browse the repository at this point in the history
  • Loading branch information
woutslakhorst committed Sep 19, 2023
1 parent 9a31c59 commit 1eccd99
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 976 deletions.
25 changes: 12 additions & 13 deletions auth/api/iam/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,35 @@ import (

// HTTPClient holds the server address and other basic settings for the http client
type HTTPClient struct {
config core.ClientConfig
httpClient core.HTTPRequestDoer
}

// NewHTTPClient creates a new api client.
func NewHTTPClient(config core.ClientConfig) HTTPClient {
return HTTPClient{
config: config,
httpClient: core.MustCreateHTTPClient(config, nil),
}
}

func (hb HTTPClient) clientWithBase(baseURL string) ClientInterface {
// can only be used for public APIs
response, err := NewClientWithResponses(baseURL, WithHTTPClient(hb.httpClient))
// NewClientWithResponses can only return an error if an option returns an error.
// When adding options, make sure to check if it can return an error, if so refactor to handle the error properly.
if err != nil {
panic(err)
}
return response
}

// OAuthAuthorizationServerMetadata retrieves the OAuth authorization server metadata for the given web DID.
func (hb HTTPClient) OAuthAuthorizationServerMetadata(ctx context.Context, webDID did.DID) (*OAuthAuthorizationServerMetadata, error) {
// TODO: ignoring root web did for now. We can't use the generated client for that type of web:did :(
serverURL, err := didweb.DIDToURL(webDID)
if err != nil {
return nil, err
}

response, err := hb.clientWithBase(serverURL.String()).OAuthAuthorizationServerMetadata(ctx, webDID.String())
metadataURL, err := IssuerIdToWellKnown(serverURL.String(), authzServerWellKnown, hb.config.Strictmode)
if err != nil {
return nil, err
}

request := &http.Request{
Method: "GET",
URL: metadataURL,
}
response, err := hb.httpClient.Do(request.WithContext(ctx))
if err != nil {
return nil, err
}
Expand Down
21 changes: 18 additions & 3 deletions auth/api/iam/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package iam

import (
"context"
"fmt"
"github.com/nuts-foundation/go-did/did"
http2 "github.com/nuts-foundation/nuts-node/test/http"
"github.com/nuts-foundation/nuts-node/vdr/didweb"
Expand All @@ -36,7 +35,7 @@ import (
func TestHTTPClient_OAuthAuthorizationServerMetadata(t *testing.T) {
ctx := context.Background()

t.Run("ok using web:did", func(t *testing.T) {
t.Run("ok using root web:did", func(t *testing.T) {
result := OAuthAuthorizationServerMetadata{TokenEndpoint: "/token"}
handler := http2.Handler{StatusCode: http.StatusOK, ResponseData: result}
tlsServer, client := testServerAndClient(t, &handler)
Expand All @@ -49,7 +48,23 @@ func TestHTTPClient_OAuthAuthorizationServerMetadata(t *testing.T) {
assert.Equal(t, "/token", metadata.TokenEndpoint)
require.NotNil(t, handler.Request)
assert.Equal(t, "GET", handler.Request.Method)
assert.Equal(t, fmt.Sprintf("/.well-known/oauth-authorization-server/iam/%s", testDID), handler.Request.URL.Path)
assert.Equal(t, "/.well-known/oauth-authorization-server", handler.Request.URL.Path)
})
t.Run("ok using user web:did", func(t *testing.T) {
result := OAuthAuthorizationServerMetadata{TokenEndpoint: "/token"}
handler := http2.Handler{StatusCode: http.StatusOK, ResponseData: result}
tlsServer, client := testServerAndClient(t, &handler)
testDID := stringURLToDID(t, tlsServer.URL)
testDID = did.MustParseDID(testDID.String() + ":iam:123")

metadata, err := client.OAuthAuthorizationServerMetadata(ctx, testDID)

require.NoError(t, err)
require.NotNil(t, metadata)
assert.Equal(t, "/token", metadata.TokenEndpoint)
require.NotNil(t, handler.Request)
assert.Equal(t, "GET", handler.Request.Method)
assert.Equal(t, "/.well-known/oauth-authorization-server/iam/123", handler.Request.URL.Path)
})
t.Run("error - non 200 return value", func(t *testing.T) {
handler := http2.Handler{StatusCode: http.StatusBadRequest}
Expand Down
Loading

0 comments on commit 1eccd99

Please sign in to comment.