-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes Invalid JWT token using a personal account (#2388)
* fix: Decode valid JWT tokens. * chore: Add unit tests. * chore: Fix typo.
- Loading branch information
Showing
5 changed files
with
105 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/Authentication/Authentication.Test/Helpers/JwtHelpersTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Microsoft.Graph.PowerShell.Authentication; | ||
using Microsoft.Graph.PowerShell.Authentication.Core.Utilities; | ||
using Microsoft.Identity.Client; | ||
using Moq; | ||
using System; | ||
using Xunit; | ||
|
||
namespace Microsoft.Graph.Authentication.Test.Helpers | ||
{ | ||
public class JwtHelpersTests | ||
{ | ||
private readonly IAccount _mockIAccount; | ||
public JwtHelpersTests() | ||
{ | ||
_mockIAccount = GetIAccountMock(); | ||
} | ||
|
||
[Fact] | ||
public void DecodeJWTStringShouldReturnAuthContextWithClaims() | ||
{ | ||
IAuthContext authContext = new AuthContext | ||
{ | ||
AuthType = AuthenticationType.Delegated | ||
}; | ||
|
||
JwtHelpers.DecodeJWT(MockConstants.DummyAccessToken, _mockIAccount, ref authContext); | ||
|
||
Assert.Equal("mockAppId", authContext.ClientId); | ||
Assert.Equal("mockTid", authContext.TenantId); | ||
Assert.Equal("[email protected]", authContext.Account); | ||
Assert.Equal(2, authContext.Scopes.Length); | ||
} | ||
|
||
[Fact] | ||
public void DecodeJWTStringShouldReturnNullAuthContextWhenTokenIsNotJWT() | ||
{ | ||
IAuthContext authContext = new AuthContext | ||
{ | ||
AuthType = AuthenticationType.Delegated | ||
}; | ||
|
||
JwtHelpers.DecodeJWT("EwCQA_NOT_JWT", _mockIAccount, ref authContext); | ||
|
||
Assert.Null(authContext.Scopes); | ||
Assert.Equal("mockUsername", authContext.Account); | ||
} | ||
|
||
[Fact] | ||
public void DecodeJWTStringShouldThrowExceptionWhenTokenIsExpired() | ||
{ | ||
IAuthContext authContext = new AuthContext | ||
{ | ||
AuthType = AuthenticationType.UserProvidedAccessToken | ||
}; | ||
|
||
_ = Assert.Throws<Exception>(() => JwtHelpers.DecodeJWT(MockConstants.DummyAccessToken, _mockIAccount, ref authContext)); | ||
} | ||
|
||
private IAccount GetIAccountMock() | ||
{ | ||
var accountId = new AccountId("mockId", "mockObjectId", "mockTenantId"); | ||
Mock<IAccount> accountMock = new Mock<IAccount>(); | ||
_ = accountMock.SetupGet(account => account.HomeAccountId).Returns(accountId); | ||
_ = accountMock.SetupGet(account => account.Username).Returns("mockUsername"); | ||
return accountMock.Object; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// ------------------------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. | ||
// ------------------------------------------------------------------------------ | ||
|
||
namespace Microsoft.Graph.Authentication.Test | ||
{ | ||
internal class MockConstants | ||
{ | ||
// This is a dummy access token that is used for testing purposes only. Expired at 2023-10-25T22:23:50.265Z. | ||
internal const string DummyAccessToken = "eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiVGVzdCIsInNjcCI6Im9wZW5pZCBSZXBvcnRzLlJlYWQiLCJ1cG4iOiJ1cG5AY29udG9zby5jb20iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IlRlc3QiLCJhcHBpZCI6Im1vY2tBcHBJZCIsImFwcF9kaXNwbGF5bmFtZSI6Im1vY2tOYW1lIiwiZXhwIjoxNjk4MjcyNjMwLCJpYXQiOjE2OTgyNzI2MzAsInRpZCI6Im1vY2tUaWQifQ.sA7eX8PRxhUTRnXHYZyFB095jszZX75NeIjUae8oGic"; | ||
} | ||
} |