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 (#289)
Browse files Browse the repository at this point in the history
Signed-off-by: Firas Qutishat <firas.qutishat@securekey.com>

Signed-off-by: Firas Qutishat <firas.qutishat@securekey.com>
  • Loading branch information
fqutishat authored Oct 24, 2022
1 parent af6620d commit 9b275fb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
25 changes: 20 additions & 5 deletions component/vdr/sidetree/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ SPDX-License-Identifier: Apache-2.0
*/

// Package sidetree implements sidetree client
//
package sidetree

import (
Expand Down Expand Up @@ -39,10 +38,15 @@ const (

var logger = log.New("aries-framework-ext/vdr/sidetree/client") //nolint: gochecknoglobals

type authTokenProvider interface {
AuthToken() (string, error)
}

// Client sidetree client.
type Client struct {
client *http.Client
authToken string
client *http.Client
authToken string
authTokenProvider authTokenProvider
}

// New return did bloc client.
Expand Down Expand Up @@ -462,8 +466,19 @@ func (c *Client) sendRequest(req []byte, endpointURL string) ([]byte, error) {

httpReq.Header.Set("Content-Type", "application/json")

if c.authToken != "" {
httpReq.Header.Add("Authorization", c.authToken)
authToken := c.authToken

if c.authTokenProvider != nil {
v, errToken := c.authTokenProvider.AuthToken()
if errToken != nil {
return nil, errToken
}

authToken = "Bearer " + v
}

if authToken != "" {
httpReq.Header.Add("Authorization", authToken)
}

resp, err := c.client.Do(httpReq)
Expand Down
8 changes: 7 additions & 1 deletion component/vdr/sidetree/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestClient_DeactivateDID(t *testing.T) {
}))
defer serv.Close()

v := sidetree.New(sidetree.WithAuthToken("tk1"))
v := sidetree.New(sidetree.WithAuthTokenProvider(&tokenProvider{}))

pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
require.NoError(t, err)
Expand Down Expand Up @@ -863,3 +863,9 @@ func (s *signerMock) Headers() jws.Headers {
func (s *signerMock) PublicKeyJWK() *jws.JWK {
return s.publicKey
}

type tokenProvider struct{}

func (t *tokenProvider) AuthToken() (string, error) {
return "newTK", nil
}
7 changes: 7 additions & 0 deletions component/vdr/sidetree/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ func WithAuthToken(authToken string) Option {
opts.authToken = "Bearer " + authToken
}
}

// WithAuthTokenProvider add auth token provider.
func WithAuthTokenProvider(p authTokenProvider) Option {
return func(opts *Client) {
opts.authTokenProvider = p
}
}

0 comments on commit 9b275fb

Please sign in to comment.