@@ -10,7 +10,6 @@ import (
10
10
"crypto/x509"
11
11
"encoding/base64"
12
12
"encoding/json"
13
- "errors"
14
13
"fmt"
15
14
"io"
16
15
"net/http"
@@ -25,6 +24,7 @@ import (
25
24
"github.com/kylelemons/godebug/pretty"
26
25
27
26
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/cache"
27
+ "github.com/AzureAD/microsoft-authentication-library-for-go/apps/errors"
28
28
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/exported"
29
29
internalTime "github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/json/types/time"
30
30
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/mock"
@@ -35,6 +35,7 @@ import (
35
35
36
36
// errorClient is an HTTP client for tests that should fail when confidential.Client sends a request
37
37
type errorClient struct {}
38
+ type contextKey struct {}
38
39
39
40
func (* errorClient ) Do (req * http.Request ) (* http.Response , error ) {
40
41
return nil , fmt .Errorf ("expected no requests but received one for %s" , req .URL .String ())
@@ -138,7 +139,7 @@ func TestAcquireTokenByCredential(t *testing.T) {
138
139
}
139
140
client , err := fakeClient (accesstokens.TokenResponse {
140
141
AccessToken : token ,
141
- ExpiresOn : internalTime. DurationTime { T : time .Now ().Add (1 * time .Hour )} ,
142
+ ExpiresOn : time .Now ().Add (1 * time .Hour ),
142
143
ExtExpiresOn : internalTime.DurationTime {T : time .Now ().Add (1 * time .Hour )},
143
144
GrantedScopes : accesstokens.Scopes {Slice : tokenScope },
144
145
TokenType : "Bearer" ,
@@ -305,7 +306,7 @@ func TestAcquireTokenOnBehalfOf(t *testing.T) {
305
306
306
307
func TestAcquireTokenByAssertionCallback (t * testing.T ) {
307
308
calls := 0
308
- key := struct {} {}
309
+ key := contextKey {}
309
310
ctx := context .WithValue (context .Background (), key , true )
310
311
getAssertion := func (c context.Context , o AssertionRequestOptions ) (string , error ) {
311
312
if v := c .Value (key ); v == nil || ! v .(bool ) {
@@ -358,7 +359,7 @@ func TestAcquireTokenByAuthCode(t *testing.T) {
358
359
tr := accesstokens.TokenResponse {
359
360
AccessToken : token ,
360
361
RefreshToken : refresh ,
361
- ExpiresOn : internalTime. DurationTime { T : time .Now ().Add (1 * time .Hour )} ,
362
+ ExpiresOn : time .Now ().Add (1 * time .Hour ),
362
363
ExtExpiresOn : internalTime.DurationTime {T : time .Now ().Add (1 * time .Hour )},
363
364
GrantedScopes : accesstokens.Scopes {Slice : tokenScope },
364
365
IDToken : accesstokens.IDToken {
@@ -427,6 +428,40 @@ func TestAcquireTokenByAuthCode(t *testing.T) {
427
428
}
428
429
}
429
430
431
+ func TestInvalidJsonErrFromResponse (t * testing.T ) {
432
+ cred , err := NewCredFromSecret (fakeSecret )
433
+ if err != nil {
434
+ t .Fatal (err )
435
+ }
436
+ tenant := "A"
437
+ lmo := "login.microsoftonline.com"
438
+ mockClient := mock.Client {}
439
+ mockClient .AppendResponse (mock .WithBody (mock .GetInstanceDiscoveryBody (lmo , tenant )))
440
+ client , err := New (fmt .Sprintf (authorityFmt , lmo , tenant ), fakeClientID , cred , WithHTTPClient (& mockClient ))
441
+ if err != nil {
442
+ t .Fatal (err )
443
+ }
444
+ ctx := context .Background ()
445
+ // cache an access token for each tenant. To simplify determining their provenance below, the value of each token is the ID of the tenant that provided it.
446
+ if _ , err = client .AcquireTokenSilent (ctx , tokenScope , WithTenantID (tenant )); err == nil {
447
+ t .Fatal ("silent auth should fail because the cache is empty" )
448
+ }
449
+ mockClient .AppendResponse (mock .WithBody (mock .GetTenantDiscoveryBody (lmo , tenant )))
450
+ body := fmt .Sprintf (
451
+ `{"access_token": "%s","expires_in": %d,"expires_on": %d,"token_type": "Bearer"` ,
452
+ tenant , 3600 , time .Now ().Add (time .Duration (3600 )* time .Second ).Unix (),
453
+ )
454
+ mockClient .AppendResponse (mock .WithBody ([]byte (body )))
455
+ _ , err = client .AcquireTokenByCredential (ctx , tokenScope , WithTenantID (tenant ))
456
+ if err == nil {
457
+ t .Fatal ("should have failed with InvalidJsonErr Response" )
458
+ }
459
+ var ie errors.InvalidJsonErr
460
+ if ! errors .As (err , & ie ) {
461
+ t .Fatal ("should have revieved a InvalidJsonErr, but got" , err )
462
+ }
463
+ }
464
+
430
465
func TestAcquireTokenSilentTenants (t * testing.T ) {
431
466
cred , err := NewCredFromSecret (fakeSecret )
432
467
if err != nil {
@@ -478,7 +513,7 @@ func TestADFSTokenCaching(t *testing.T) {
478
513
AccessToken : "at1" ,
479
514
RefreshToken : "rt" ,
480
515
TokenType : "bearer" ,
481
- ExpiresOn : internalTime. DurationTime { T : time .Now ().Add (time .Hour )} ,
516
+ ExpiresOn : time .Now ().Add (time .Hour ),
482
517
ExtExpiresOn : internalTime.DurationTime {T : time .Now ().Add (time .Hour )},
483
518
GrantedScopes : accesstokens.Scopes {Slice : tokenScope },
484
519
IDToken : accesstokens.IDToken {
@@ -608,7 +643,7 @@ func TestNewCredFromCert(t *testing.T) {
608
643
t .Run (fmt .Sprintf ("%s/%v" , filepath .Base (file .path ), sendX5c ), func (t * testing.T ) {
609
644
client , err := fakeClient (accesstokens.TokenResponse {
610
645
AccessToken : token ,
611
- ExpiresOn : internalTime. DurationTime { T : time .Now ().Add (time .Hour )} ,
646
+ ExpiresOn : time .Now ().Add (time .Hour ),
612
647
GrantedScopes : accesstokens.Scopes {Slice : tokenScope },
613
648
}, cred , fakeAuthority , opts ... )
614
649
if err != nil {
@@ -724,7 +759,7 @@ func TestNewCredFromTokenProvider(t *testing.T) {
724
759
expectedToken := "expected token"
725
760
called := false
726
761
expiresIn := 4200
727
- key := struct {} {}
762
+ key := contextKey {}
728
763
ctx := context .WithValue (context .Background (), key , true )
729
764
cred := NewCredFromTokenProvider (func (c context.Context , tp exported.TokenProviderParameters ) (exported.TokenProviderResult , error ) {
730
765
if called {
@@ -982,7 +1017,7 @@ func TestWithClaims(t *testing.T) {
982
1017
case "password" :
983
1018
ar , err = client .AcquireTokenByUsernamePassword (ctx , tokenScope , "username" , "password" , WithClaims (test .claims ))
984
1019
default :
985
- t .Fatalf ("test bug: no test for " + method )
1020
+ t .Fatalf ("test bug: no test for %s" , method )
986
1021
}
987
1022
if err != nil {
988
1023
t .Fatal (err )
@@ -1092,7 +1127,7 @@ func TestWithTenantID(t *testing.T) {
1092
1127
case "obo" :
1093
1128
ar , err = client .AcquireTokenOnBehalfOf (ctx , "assertion" , tokenScope , WithTenantID (test .tenant ))
1094
1129
default :
1095
- t .Fatalf ("test bug: no test for " + method )
1130
+ t .Fatalf ("test bug: no test for %s" , method )
1096
1131
}
1097
1132
if err != nil {
1098
1133
if test .expectError {
@@ -1402,7 +1437,7 @@ func TestWithAuthenticationScheme(t *testing.T) {
1402
1437
}
1403
1438
client , err := fakeClient (accesstokens.TokenResponse {
1404
1439
AccessToken : token ,
1405
- ExpiresOn : internalTime. DurationTime { T : time .Now ().Add (1 * time .Hour )} ,
1440
+ ExpiresOn : time .Now ().Add (1 * time .Hour ),
1406
1441
ExtExpiresOn : internalTime.DurationTime {T : time .Now ().Add (1 * time .Hour )},
1407
1442
GrantedScopes : accesstokens.Scopes {Slice : tokenScope },
1408
1443
TokenType : "TokenType" ,
@@ -1442,7 +1477,7 @@ func TestAcquireTokenByCredentialFromDSTS(t *testing.T) {
1442
1477
}
1443
1478
client , err := fakeClient (accesstokens.TokenResponse {
1444
1479
AccessToken : token ,
1445
- ExpiresOn : internalTime. DurationTime { T : time .Now ().Add (1 * time .Hour )} ,
1480
+ ExpiresOn : time .Now ().Add (1 * time .Hour ),
1446
1481
ExtExpiresOn : internalTime.DurationTime {T : time .Now ().Add (1 * time .Hour )},
1447
1482
GrantedScopes : accesstokens.Scopes {Slice : tokenScope },
1448
1483
TokenType : "Bearer" ,
0 commit comments