Skip to content

Commit 486191d

Browse files
authored
Add support for Azure AD - improvements (#34)
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
1 parent 9232146 commit 486191d

4 files changed

Lines changed: 12 additions & 27 deletions

File tree

documentdb.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"net/http"
1313
"reflect"
1414
"sync"
15-
16-
"github.com/Azure/go-autorest/autorest/adal"
1715
)
1816

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

3735
type Config struct {
3836
MasterKey *Key
39-
ServicePrincipal *adal.ServicePrincipalToken
37+
ServicePrincipal ServicePrincipalProvider
4038
Client http.Client
4139
IdentificationHydrator IdentificationHydrator
4240
IdentificationPropertyName string
@@ -51,7 +49,7 @@ func NewConfig(key *Key) *Config {
5149
}
5250

5351
// NewConfigWithServicePrincipal creates a new Config object that uses Azure AD (via a service principal) for authentication
54-
func NewConfigWithServicePrincipal(servicePrincipal *adal.ServicePrincipalToken) *Config {
52+
func NewConfigWithServicePrincipal(servicePrincipal ServicePrincipalProvider) *Config {
5553
return &Config{
5654
ServicePrincipal: servicePrincipal,
5755
IdentificationHydrator: DefaultIdentificationHydrator,
@@ -360,3 +358,12 @@ func (c *DocumentDB) ExecuteStoredProcedure(link string, params, body interface{
360358
_, err = c.client.Execute(link, params, &body, opts...)
361359
return
362360
}
361+
362+
// ServicePrincipalProvider is an interface for an object that provides an Azure service principal
363+
// It's normally used with *adal.ServicePrincipalToken objects from github.com/Azure/go-autorest/autorest/adal
364+
type ServicePrincipalProvider interface {
365+
// EnsureFresh will refresh the token if it will expire within the refresh window. This method is safe for concurrent use.
366+
EnsureFresh() error
367+
// OAuthToken returns the current access token.
368+
OAuthToken() string
369+
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module github.com/a8m/documentdb
22

33
require (
4-
github.com/Azure/go-autorest/autorest/adal v0.9.14
54
github.com/davecgh/go-spew v1.1.1 // indirect
65
github.com/json-iterator/go v1.1.5
76
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect

go.sum

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
1-
github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=
2-
github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
3-
github.com/Azure/go-autorest/autorest/adal v0.9.14 h1:G8hexQdV5D4khOXrWG2YuLCFKhWYmWD8bHYaXN5ophk=
4-
github.com/Azure/go-autorest/autorest/adal v0.9.14/go.mod h1:W/MM4U6nLxnIskrw4UwWzlHfGjwUS50aOsc/I3yuU8M=
5-
github.com/Azure/go-autorest/autorest/date v0.3.0 h1:7gUk1U5M/CQbp9WoqinNzJar+8KY+LPI6wiWrP/myHw=
6-
github.com/Azure/go-autorest/autorest/date v0.3.0/go.mod h1:BI0uouVdmngYNUzGWeSYnokU+TrmwEsOqdt8Y6sso74=
7-
github.com/Azure/go-autorest/autorest/mocks v0.4.1 h1:K0laFcLE6VLTOwNgSxaGbUcLPuGXlNkbVvq4cW4nIHk=
8-
github.com/Azure/go-autorest/autorest/mocks v0.4.1/go.mod h1:LTp+uSrOhSkaKrUy935gNZuuIPPVsHlr9DSOxSayd+k=
9-
github.com/Azure/go-autorest/logger v0.2.1 h1:IG7i4p/mDa2Ce4TRyAO8IHnVhAVF3RFU+ZtXWSmf4Tg=
10-
github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
11-
github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo=
12-
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
131
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
142
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
15-
github.com/form3tech-oss/jwt-go v3.2.2+incompatible h1:TcekIExNqud5crz4xD2pavyTgWiPvpYe4Xau31I0PRk=
16-
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
173
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
184
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
195
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
@@ -26,10 +12,3 @@ github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
2612
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2713
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
2814
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
29-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
30-
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 h1:hb9wdF1z5waM+dSIICn1l0DkLVDT3hqhhQsDNUmHPRE=
31-
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
32-
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
33-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
34-
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
35-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

request_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestResourceRequest(t *testing.T) {
2929
func TestDefaultHeaders(t *testing.T) {
3030
r, _ := http.NewRequest("GET", "link", &bytes.Buffer{})
3131
req := ResourceRequest("/dbs/b5NCAA==/", r)
32-
_ = req.DefaultHeaders(&Key{Key: "YXJpZWwNCg=="})
32+
_ = req.DefaultHeaders(&Config{MasterKey: &Key{Key: "YXJpZWwNCg=="}})
3333

3434
assert := assert.New(t)
3535
assert.NotEqual(req.Header.Get(HeaderAuth), "")

0 commit comments

Comments
 (0)