Skip to content

Commit

Permalink
Support specified the oauth2 private key with prefix 'file://' and 'd…
Browse files Browse the repository at this point in the history
…ata://' (#343)

---

Master Issue: #<xyz>

*Motivation*

Make the oauth2 read the private key can handle with  'file://' schema and 'data://' schema.
  • Loading branch information
zymap authored Aug 17, 2020
1 parent 570d5ce commit a8204e2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
18 changes: 17 additions & 1 deletion oauth2/client_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ package oauth2
import (
"encoding/json"
"io/ioutil"
"strings"
)

const (
FILE = "file://"
DATA = "data://"
)

type KeyFileProvider struct {
Expand All @@ -43,7 +49,17 @@ func NewClientCredentialsProviderFromKeyFile(keyFile string) *KeyFileProvider {
var _ ClientCredentialsProvider = &KeyFileProvider{}

func (k *KeyFileProvider) GetClientCredentials() (*KeyFile, error) {
keyFile, err := ioutil.ReadFile(k.KeyFile)
var keyFile []byte
var err error
switch {
case strings.HasPrefix(k.KeyFile, FILE):
filename := strings.TrimPrefix(k.KeyFile, FILE)
keyFile, err = ioutil.ReadFile(filename)
case strings.HasPrefix(k.KeyFile, "data://"):
keyFile = []byte(strings.TrimPrefix(k.KeyFile, DATA))
default:
keyFile, err = ioutil.ReadFile(k.KeyFile)
}
if err != nil {
return nil, err
}
Expand Down
63 changes: 44 additions & 19 deletions pulsar/internal/auth/oauth2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,52 @@ func TestNewAuthenticationOAuth2WithParams(t *testing.T) {
t.Fatal(errors.Wrap(err, "create mocked key file failed"))
}

params := map[string]string{
ConfigParamType: ConfigParamTypeClientCredentials,
ConfigParamIssuerURL: server.URL,
ConfigParamClientID: "client-id",
ConfigParamAudience: "audience",
ConfigParamKeyFile: kf,
testData := []map[string]string{
{
ConfigParamType: ConfigParamTypeClientCredentials,
ConfigParamIssuerURL: server.URL,
ConfigParamClientID: "client-id",
ConfigParamAudience: "audience",
ConfigParamKeyFile: kf,
},
{
ConfigParamType: ConfigParamTypeClientCredentials,
ConfigParamIssuerURL: server.URL,
ConfigParamClientID: "client-id",
ConfigParamAudience: "audience",
ConfigParamKeyFile: fmt.Sprintf("file://%s", kf),
},
{
ConfigParamType: ConfigParamTypeClientCredentials,
ConfigParamIssuerURL: server.URL,
ConfigParamClientID: "client-id",
ConfigParamAudience: "audience",
ConfigParamKeyFile: "data://" + fmt.Sprintf(`{
"type":"resource",
"client_id":"client-id",
"client_secret":"client-secret",
"client_email":"[email protected]",
"issuer_url":"%s"
}`, server.URL),
},
}

auth, err := NewAuthenticationOAuth2WithParams(params)
if err != nil {
t.Fatal(err)
}
err = auth.Init()
if err != nil {
t.Fatal(err)
}
for i := range testData {
params := testData[i]
auth, err := NewAuthenticationOAuth2WithParams(params)
if err != nil {
t.Fatal(err)
}
err = auth.Init()
if err != nil {
t.Fatal(err)
}

token, err := auth.GetData()
if err != nil {
t.Fatal(err)
}
token, err := auth.GetData()
if err != nil {
t.Fatal(err)
}

assert.Equal(t, "token-content", string(token))
assert.Equal(t, "token-content", string(token))
}
}

0 comments on commit a8204e2

Please sign in to comment.