Skip to content

Commit

Permalink
Add support for Azure AD - improvements (#34)
Browse files Browse the repository at this point in the history
Use an interface for the SP provider object to avoid adding "github.com/Azure/go-autorest/autorest/adal" as a dependency to this module
Also, fixed test that was leftover from before.
Improves upon #33
  • Loading branch information
ItalyPaleAle committed Jun 25, 2021
1 parent 9232146 commit 486191d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 27 deletions.
15 changes: 11 additions & 4 deletions documentdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"net/http"
"reflect"
"sync"

"github.com/Azure/go-autorest/autorest/adal"
)

var buffers = &sync.Pool{
Expand All @@ -36,7 +34,7 @@ func DefaultIdentificationHydrator(config *Config, doc interface{}) {

type Config struct {
MasterKey *Key
ServicePrincipal *adal.ServicePrincipalToken
ServicePrincipal ServicePrincipalProvider
Client http.Client
IdentificationHydrator IdentificationHydrator
IdentificationPropertyName string
Expand All @@ -51,7 +49,7 @@ func NewConfig(key *Key) *Config {
}

// NewConfigWithServicePrincipal creates a new Config object that uses Azure AD (via a service principal) for authentication
func NewConfigWithServicePrincipal(servicePrincipal *adal.ServicePrincipalToken) *Config {
func NewConfigWithServicePrincipal(servicePrincipal ServicePrincipalProvider) *Config {
return &Config{
ServicePrincipal: servicePrincipal,
IdentificationHydrator: DefaultIdentificationHydrator,
Expand Down Expand Up @@ -360,3 +358,12 @@ func (c *DocumentDB) ExecuteStoredProcedure(link string, params, body interface{
_, err = c.client.Execute(link, params, &body, opts...)
return
}

// ServicePrincipalProvider is an interface for an object that provides an Azure service principal
// It's normally used with *adal.ServicePrincipalToken objects from github.com/Azure/go-autorest/autorest/adal
type ServicePrincipalProvider interface {
// EnsureFresh will refresh the token if it will expire within the refresh window. This method is safe for concurrent use.
EnsureFresh() error
// OAuthToken returns the current access token.
OAuthToken() string
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module github.com/a8m/documentdb

require (
github.com/Azure/go-autorest/autorest/adal v0.9.14
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/json-iterator/go v1.1.5
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand Down
21 changes: 0 additions & 21 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=
github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
github.com/Azure/go-autorest/autorest/adal v0.9.14 h1:G8hexQdV5D4khOXrWG2YuLCFKhWYmWD8bHYaXN5ophk=
github.com/Azure/go-autorest/autorest/adal v0.9.14/go.mod h1:W/MM4U6nLxnIskrw4UwWzlHfGjwUS50aOsc/I3yuU8M=
github.com/Azure/go-autorest/autorest/date v0.3.0 h1:7gUk1U5M/CQbp9WoqinNzJar+8KY+LPI6wiWrP/myHw=
github.com/Azure/go-autorest/autorest/date v0.3.0/go.mod h1:BI0uouVdmngYNUzGWeSYnokU+TrmwEsOqdt8Y6sso74=
github.com/Azure/go-autorest/autorest/mocks v0.4.1 h1:K0laFcLE6VLTOwNgSxaGbUcLPuGXlNkbVvq4cW4nIHk=
github.com/Azure/go-autorest/autorest/mocks v0.4.1/go.mod h1:LTp+uSrOhSkaKrUy935gNZuuIPPVsHlr9DSOxSayd+k=
github.com/Azure/go-autorest/logger v0.2.1 h1:IG7i4p/mDa2Ce4TRyAO8IHnVhAVF3RFU+ZtXWSmf4Tg=
github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo=
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible h1:TcekIExNqud5crz4xD2pavyTgWiPvpYe4Xau31I0PRk=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
Expand All @@ -26,10 +12,3 @@ github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 h1:hb9wdF1z5waM+dSIICn1l0DkLVDT3hqhhQsDNUmHPRE=
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
2 changes: 1 addition & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestResourceRequest(t *testing.T) {
func TestDefaultHeaders(t *testing.T) {
r, _ := http.NewRequest("GET", "link", &bytes.Buffer{})
req := ResourceRequest("/dbs/b5NCAA==/", r)
_ = req.DefaultHeaders(&Key{Key: "YXJpZWwNCg=="})
_ = req.DefaultHeaders(&Config{MasterKey: &Key{Key: "YXJpZWwNCg=="}})

assert := assert.New(t)
assert.NotEqual(req.Header.Get(HeaderAuth), "")
Expand Down

0 comments on commit 486191d

Please sign in to comment.