From 788f4668e2be9eb7b01ec3261796dd96591c583b Mon Sep 17 00:00:00 2001 From: Firas Qutishat Date: Tue, 25 Oct 2022 16:04:09 +0300 Subject: [PATCH] chore: Add auth token provider (#3415) Signed-off-by: Firas Qutishat Signed-off-by: Firas Qutishat --- pkg/vdr/httpbinding/resolver.go | 15 +++++++++++++-- pkg/vdr/httpbinding/resolver_test.go | 10 ++++++++-- pkg/vdr/httpbinding/vdr.go | 20 ++++++++++++++++---- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/pkg/vdr/httpbinding/resolver.go b/pkg/vdr/httpbinding/resolver.go index fe5bf603b..4bd668212 100644 --- a/pkg/vdr/httpbinding/resolver.go +++ b/pkg/vdr/httpbinding/resolver.go @@ -35,8 +35,19 @@ func (v *VDR) resolveDID(uri string) ([]byte, error) { req.Header.Add("Accept", didLDJson) - if v.resolveAuthToken != "" { - req.Header.Add("Authorization", v.resolveAuthToken) + authToken := v.resolveAuthToken + + if v.authTokenProvider != nil { + v, errToken := v.authTokenProvider.AuthToken() + if errToken != nil { + return nil, errToken + } + + authToken = "Bearer " + v + } + + if authToken != "" { + req.Header.Add("Authorization", authToken) } resp, err := v.client.Do(req) diff --git a/pkg/vdr/httpbinding/resolver_test.go b/pkg/vdr/httpbinding/resolver_test.go index 510f722a0..0e1936820 100644 --- a/pkg/vdr/httpbinding/resolver_test.go +++ b/pkg/vdr/httpbinding/resolver_test.go @@ -103,7 +103,7 @@ func TestRead_DIDDoc(t *testing.T) { defer func() { testServer.Close() }() - resolver, err := New(testServer.URL, WithResolveAuthToken("tk1")) + resolver, err := New(testServer.URL, WithResolveAuthTokenProvider(&tokenProvider{})) require.NoError(t, err) gotDocument, err := resolver.Read("did:example:334455") require.NoError(t, err) @@ -289,7 +289,7 @@ func TestRead_HTTPGetFailed(t *testing.T) { } func TestDIDResolver_Accept(t *testing.T) { - resolver, err := New("localhost:8080") + resolver, err := New("localhost:8080", WithResolveAuthToken("tk1")) require.NoError(t, err) require.True(t, resolver.accept("example")) @@ -299,3 +299,9 @@ func TestDIDResolver_Accept(t *testing.T) { require.NoError(t, err) require.False(t, resolver.accept("example")) } + +type tokenProvider struct{} + +func (t *tokenProvider) AuthToken() (string, error) { + return "newTK", nil +} diff --git a/pkg/vdr/httpbinding/vdr.go b/pkg/vdr/httpbinding/vdr.go index 7c3c46f0f..3bcb25554 100644 --- a/pkg/vdr/httpbinding/vdr.go +++ b/pkg/vdr/httpbinding/vdr.go @@ -20,12 +20,17 @@ import ( var logger = log.New("aries-framework/vdr/httpbinding") +type authTokenProvider interface { + AuthToken() (string, error) +} + // VDR via HTTP(s) endpoint. type VDR struct { - endpointURL string - client *http.Client - accept Accept - resolveAuthToken string + endpointURL string + client *http.Client + accept Accept + resolveAuthToken string + authTokenProvider authTokenProvider } // Accept is method to accept did method. @@ -106,6 +111,13 @@ func WithResolveAuthToken(authToken string) Option { } } +// WithResolveAuthTokenProvider add auth token provider. +func WithResolveAuthTokenProvider(p authTokenProvider) Option { + return func(opts *VDR) { + opts.authTokenProvider = p + } +} + func closeResponseBody(respBody io.Closer) { e := respBody.Close() if e != nil {