-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05c7aa2
commit 1ea6de9
Showing
13 changed files
with
274 additions
and
47 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
55 changes: 55 additions & 0 deletions
55
src/Okta.Sdk.UnitTests/DefaultBearerTokenProviderShould.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,55 @@ | ||
// <copyright file="DefaultBearerTokenProviderShould.cs" company="Okta, Inc"> | ||
// Copyright (c) 2020 - present Okta, Inc. All rights reserved. | ||
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information. | ||
// </copyright> | ||
|
||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.Logging; | ||
using NSubstitute; | ||
using Okta.Sdk.Configuration; | ||
using Okta.Sdk.Internal; | ||
using Okta.Sdk.UnitTests.Internal; | ||
using Xunit; | ||
|
||
namespace Okta.Sdk.UnitTests | ||
{ | ||
public class DefaultBearerTokenProviderShould | ||
{ | ||
[Fact] | ||
public async Task ReturnAccessTokenWhenRequestSuccess() | ||
{ | ||
var tokenProvider = new DefaultBearerTokenProvider("DefaultExternallyGeneratedToken", default); | ||
var token = await tokenProvider.GetAccessTokenAsync(); | ||
token.Should().Be("DefaultExternallyGeneratedToken"); | ||
} | ||
|
||
[Fact] | ||
public async Task ReturnNewAccessTokenWhenOldIsNotAuthorized() | ||
{ | ||
var configuration = new OktaClientConfiguration | ||
{ | ||
OktaDomain = "https://myOktaDomain.oktapreview.com", | ||
AuthorizationMode = AuthorizationMode.BearerToken, | ||
ClientId = "foo", | ||
BearerToken = "token", | ||
Scopes = new List<string> { "foo" }, | ||
}; | ||
|
||
var requestMessageHandler = new MockHttpMessageHandler(string.Empty, HttpStatusCode.Unauthorized); | ||
var httpClientRequest = new HttpClient(requestMessageHandler); | ||
var testableCustomTokenProvider = new TestableCustomTokenProvider(); | ||
var mockLogger = Substitute.For<ILogger>(); | ||
var externalTokenProvider = new DefaultBearerTokenProvider("oldAccessToken", testableCustomTokenProvider); | ||
var requestExecutor = new DefaultRequestExecutor(configuration, httpClientRequest, mockLogger, oAuthTokenProvider: externalTokenProvider); | ||
|
||
testableCustomTokenProvider.TokenRefreshed.Should().BeFalse(); | ||
await requestExecutor.GetAsync("https://myOktaDomain.oktapreview.com/v1/users", default, default).ConfigureAwait(false); | ||
testableCustomTokenProvider.TokenRefreshed.Should().BeTrue(); | ||
} | ||
} | ||
} |
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,22 @@ | ||
// <copyright file="TestableCustomTokenProvider.cs" company="Okta, Inc"> | ||
// Copyright (c) 2020 - present Okta, Inc. All rights reserved. | ||
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Okta.Sdk.Internal; | ||
|
||
namespace Okta.Sdk.UnitTests | ||
{ | ||
internal class TestableCustomTokenProvider : IOAuthTokenProvider | ||
{ | ||
public bool TokenRefreshed { get; set; } | ||
|
||
public Task<string> GetAccessTokenAsync(bool forceRenew = false) | ||
{ | ||
TokenRefreshed = true; | ||
return Task.FromResult("NewAccessToken"); | ||
} | ||
} | ||
} |
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
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,53 @@ | ||
// <copyright file="DefaultBearerTokenProvider.cs" company="Okta, Inc"> | ||
// Copyright (c) 2014 - present Okta, Inc. All rights reserved. | ||
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Okta.Sdk.Internal | ||
{ | ||
/// <summary> | ||
/// External Token Provider. | ||
/// </summary> | ||
public class DefaultBearerTokenProvider : IOAuthTokenProvider | ||
{ | ||
private readonly IOAuthTokenProvider _thirdPartyTokenProvider; | ||
private string _bearerToken; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DefaultBearerTokenProvider"/> class. | ||
/// </summary> | ||
/// <param name="bearerToken">The token.</param> | ||
/// <param name="tokenProvider">Optional custom token provider.</param> | ||
public DefaultBearerTokenProvider(string bearerToken, IOAuthTokenProvider tokenProvider) | ||
{ | ||
if (string.IsNullOrEmpty(bearerToken) && tokenProvider == default) | ||
{ | ||
throw new ArgumentException("The token is not set.", nameof(bearerToken)); | ||
} | ||
|
||
_bearerToken = bearerToken; | ||
_thirdPartyTokenProvider = tokenProvider; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<string> GetAccessTokenAsync(bool forceRenew = false) | ||
{ | ||
if (forceRenew || string.IsNullOrEmpty(_bearerToken)) | ||
{ | ||
if (_thirdPartyTokenProvider != null) | ||
{ | ||
_bearerToken = await _thirdPartyTokenProvider.GetAccessTokenAsync(forceRenew).ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
_bearerToken = string.Empty; | ||
} | ||
} | ||
|
||
return _bearerToken; | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.