Skip to content

Commit

Permalink
trace2: add regions to OAuth
Browse files Browse the repository at this point in the history
Add region tracing to key methods in OAuth2Client. This was deemed a good
starting point for regions due to the server interaction required to
obtain auth codes, device codes, and tokens. Additional regions can and
should be added to additional sections of the code that are deemed
performance-critical in the future.
  • Loading branch information
ldennington committed Mar 27, 2023
1 parent d12a7c5 commit 2fa5442
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/shared/Core/Authentication/OAuth/OAuth2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public IOAuth2CodeGenerator CodeGenerator
public async Task<OAuth2AuthorizationCodeResult> GetAuthorizationCodeAsync(IEnumerable<string> scopes,
IOAuth2WebBrowser browser, IDictionary<string, string> extraQueryParams, CancellationToken ct)
{
var label = "get auth code";
using IDisposable region = _trace2.CreateRegion(OAuth2Constants.Trace2Category, label);

string state = CodeGenerator.CreateNonce();
string codeVerifier = CodeGenerator.CreatePkceCodeVerifier();
string codeChallenge = CodeGenerator.CreatePkceCodeChallenge(OAuth2PkceChallengeMethod.Sha256, codeVerifier);
Expand Down Expand Up @@ -183,6 +186,9 @@ public async Task<OAuth2AuthorizationCodeResult> GetAuthorizationCodeAsync(IEnum

public async Task<OAuth2DeviceCodeResult> GetDeviceCodeAsync(IEnumerable<string> scopes, CancellationToken ct)
{
var label = "get device code";
using IDisposable region = _trace2.CreateRegion(OAuth2Constants.Trace2Category, label);

if (_endpoints.DeviceAuthorizationEndpoint is null)
{
throw new Trace2InvalidOperationException(_trace2,
Expand Down Expand Up @@ -218,6 +224,9 @@ public async Task<OAuth2DeviceCodeResult> GetDeviceCodeAsync(IEnumerable<string>

public async Task<OAuth2TokenResult> GetTokenByAuthorizationCodeAsync(OAuth2AuthorizationCodeResult authorizationCodeResult, CancellationToken ct)
{
var label = "get token by auth code";
using IDisposable region = _trace2.CreateRegion(OAuth2Constants.Trace2Category, label);

var formData = new Dictionary<string, string>
{
[OAuth2Constants.TokenEndpoint.GrantTypeParameter] = OAuth2Constants.TokenEndpoint.AuthorizationCodeGrantType,
Expand Down Expand Up @@ -254,6 +263,9 @@ public async Task<OAuth2TokenResult> GetTokenByAuthorizationCodeAsync(OAuth2Auth

public async Task<OAuth2TokenResult> GetTokenByRefreshTokenAsync(string refreshToken, CancellationToken ct)
{
var label = "get token by refresh token";
using IDisposable region = _trace2.CreateRegion(OAuth2Constants.Trace2Category, label);

var formData = new Dictionary<string, string>
{
[OAuth2Constants.TokenEndpoint.GrantTypeParameter] = OAuth2Constants.TokenEndpoint.RefreshTokenGrantType,
Expand Down Expand Up @@ -284,6 +296,9 @@ public async Task<OAuth2TokenResult> GetTokenByRefreshTokenAsync(string refreshT

public async Task<OAuth2TokenResult> GetTokenByDeviceCodeAsync(OAuth2DeviceCodeResult deviceCodeResult, CancellationToken ct)
{
var label = "get token by device code";
using IDisposable region = _trace2.CreateRegion(OAuth2Constants.Trace2Category, label);

var formData = new Dictionary<string, string>
{
[OAuth2Constants.DeviceAuthorization.GrantTypeParameter] = OAuth2Constants.DeviceAuthorization.DeviceCodeGrantType,
Expand Down
1 change: 1 addition & 0 deletions src/shared/Core/Authentication/OAuth/OAuth2Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public static class OAuth2Constants
public const string ClientSecretParameter = "client_secret";
public const string RedirectUriParameter = "redirect_uri";
public const string ScopeParameter = "scope";
public const string Trace2Category = "oauth2";

public static class AuthorizationEndpoint
{
Expand Down

0 comments on commit 2fa5442

Please sign in to comment.