Skip to content

Commit 451cb24

Browse files
authored
Merge pull request #523 from AzureAD/4gust/region-auto-enable
Added Region auto enable
2 parents bf74752 + efa66ec commit 451cb24

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

apps/confidential/confidential.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"encoding/pem"
1919
"errors"
2020
"fmt"
21+
"os"
22+
"strings"
2123

2224
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/cache"
2325
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/base"
@@ -315,16 +317,21 @@ func New(authority, clientID string, cred Credential, options ...Option) (Client
315317
if err != nil {
316318
return Client{}, err
317319
}
318-
320+
autoEnabledRegion := os.Getenv("MSAL_FORCE_REGION")
319321
opts := clientOptions{
320322
authority: authority,
321323
// if the caller specified a token provider, it will handle all details of authentication, using Client only as a token cache
322324
disableInstanceDiscovery: cred.tokenProvider != nil,
323325
httpClient: shared.DefaultClient,
326+
azureRegion: autoEnabledRegion,
324327
}
325328
for _, o := range options {
326329
o(&opts)
327330
}
331+
if strings.EqualFold(opts.azureRegion, "DisableMsalForceRegion") {
332+
opts.azureRegion = ""
333+
}
334+
328335
baseOpts := []base.Option{
329336
base.WithCacheAccessor(opts.accessor),
330337
base.WithClientCapabilities(opts.capabilities),

apps/confidential/confidential_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,79 @@ func TestAcquireTokenByCredential(t *testing.T) {
164164
}
165165
}
166166

167+
func TestRegionAutoEnable_EmptyRegion_EnvRegion(t *testing.T) {
168+
cred, err := NewCredFromSecret(fakeSecret)
169+
if err != nil {
170+
t.Fatal(err)
171+
}
172+
173+
envRegion := "envRegion"
174+
err = os.Setenv("MSAL_FORCE_REGION", envRegion)
175+
if err != nil {
176+
t.Fatal(err)
177+
}
178+
defer os.Unsetenv("MSAL_FORCE_REGION")
179+
180+
lmo := "login.microsoftonline.com"
181+
tenant := "tenant"
182+
mockClient := mock.Client{}
183+
client, err := New(fmt.Sprintf(authorityFmt, lmo, tenant), fakeClientID, cred, WithHTTPClient(&mockClient))
184+
if err != nil {
185+
t.Fatal(err)
186+
}
187+
188+
if client.base.AuthParams.AuthorityInfo.Region != envRegion {
189+
t.Fatalf("wanted %q, got %q", envRegion, client.base.AuthParams.AuthorityInfo.Region)
190+
}
191+
}
192+
193+
func TestRegionAutoEnable_SpecifiedRegion_EnvRegion(t *testing.T) {
194+
cred, err := NewCredFromSecret(fakeSecret)
195+
if err != nil {
196+
t.Fatal(err)
197+
}
198+
199+
envRegion := "envRegion"
200+
err = os.Setenv("MSAL_FORCE_REGION", envRegion)
201+
if err != nil {
202+
t.Fatal(err)
203+
}
204+
defer os.Unsetenv("MSAL_FORCE_REGION")
205+
206+
lmo := "login.microsoftonline.com"
207+
tenant := "tenant"
208+
mockClient := mock.Client{}
209+
testRegion := "region"
210+
client, err := New(fmt.Sprintf(authorityFmt, lmo, tenant), fakeClientID, cred, WithHTTPClient(&mockClient), WithAzureRegion(testRegion))
211+
if err != nil {
212+
t.Fatal(err)
213+
}
214+
215+
if client.base.AuthParams.AuthorityInfo.Region != testRegion {
216+
t.Fatalf("wanted %q, got %q", testRegion, client.base.AuthParams.AuthorityInfo.Region)
217+
}
218+
}
219+
220+
func TestRegionAutoEnable_DisableMsalForceRegion(t *testing.T) {
221+
cred, err := NewCredFromSecret(fakeSecret)
222+
if err != nil {
223+
t.Fatal(err)
224+
}
225+
226+
lmo := "login.microsoftonline.com"
227+
tenant := "tenant"
228+
mockClient := mock.Client{}
229+
testRegion := "DisableMsalForceRegion"
230+
client, err := New(fmt.Sprintf(authorityFmt, lmo, tenant), fakeClientID, cred, WithHTTPClient(&mockClient), WithAzureRegion(testRegion))
231+
if err != nil {
232+
t.Fatal(err)
233+
}
234+
235+
if client.base.AuthParams.AuthorityInfo.Region != "" {
236+
t.Fatalf("wanted empty, got %q", client.base.AuthParams.AuthorityInfo.Region)
237+
}
238+
}
239+
167240
func TestAcquireTokenOnBehalfOf(t *testing.T) {
168241
// this test is an offline version of TestOnBehalfOf in integration_test.go
169242
cred, err := NewCredFromSecret(fakeSecret)

0 commit comments

Comments
 (0)