Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
chore: Add auth token provider (#3415)
Browse files Browse the repository at this point in the history
Signed-off-by: Firas Qutishat <[email protected]>

Signed-off-by: Firas Qutishat <[email protected]>
  • Loading branch information
fqutishat authored Oct 25, 2022
1 parent 919b90a commit 788f466
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
15 changes: 13 additions & 2 deletions pkg/vdr/httpbinding/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions pkg/vdr/httpbinding/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"))

Expand All @@ -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
}
20 changes: 16 additions & 4 deletions pkg/vdr/httpbinding/vdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 788f466

Please sign in to comment.