Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add retrieveAuthorizationHeader method to authentication classes (box/box-codegen#442) Closes: SDK-3600 #22

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .codegen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "engineHash": "905c6a0", "specHash": "b2f7568", "version": "0.1.0" }
7 changes: 3 additions & 4 deletions Box.Sdk.Gen.Tests.Integration/Test/Auth/AuthManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,12 @@ public async System.Threading.Tasks.Task TestDeveloperTokenAuth() {
public async System.Threading.Tasks.Task TestOauthAuthRevoke() {
OAuthConfig config = new OAuthConfig(clientId: Utils.GetEnvVar(name: "CLIENT_ID"), clientSecret: Utils.GetEnvVar(name: "CLIENT_SECRET"));
BoxOAuth auth = new BoxOAuth(config: config);
BoxClient client = new BoxClient(auth: auth);
AccessToken token = await GetAccessTokenAsync().ConfigureAwait(false);
await auth.TokenStorage.StoreAsync(token: token).ConfigureAwait(false);
AccessToken? tokenBeforeRevoke = await auth.TokenStorage.GetAsync().ConfigureAwait(false);
await client.Users.GetUserMeAsync().ConfigureAwait(false);
await auth.RevokeTokenAsync().ConfigureAwait(false);
AccessToken? tokenAfterRevoke = await auth.TokenStorage.GetAsync().ConfigureAwait(false);
Assert.IsTrue(tokenBeforeRevoke != null);
Assert.IsTrue(tokenAfterRevoke == null);
await Assert.That.IsExceptionAsync(async() => await client.Users.GetUserMeAsync().ConfigureAwait(false));
}

[TestMethod]
Expand Down
5 changes: 5 additions & 0 deletions Box.Sdk.Gen/Box/CcgAuth/BoxCcgAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public async System.Threading.Tasks.Task<AccessToken> RetrieveTokenAsync(Network
return oldToken;
}

public async System.Threading.Tasks.Task<string> RetrieveAuthorizationHeaderAsync(NetworkSession? networkSession = null) {
AccessToken token = await this.RetrieveTokenAsync(networkSession: networkSession).ConfigureAwait(false);
return string.Concat("Bearer ", token.AccessTokenField);
}

/// <summary>
/// Create a new BoxCCGAuth instance that uses the provided user ID as the subject ID.
/// May be one of this application's created App User. Depending on the configured User Access Level, may also be any other App User or Managed User in the enterprise.
Expand Down
5 changes: 5 additions & 0 deletions Box.Sdk.Gen/Box/DeveloperTokenAuth/BoxDeveloperTokenAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@ public async System.Threading.Tasks.Task<AccessToken> RefreshTokenAsync(NetworkS
throw new BoxSdkError(message: "Developer token has expired. Please provide a new one.");
}

public async System.Threading.Tasks.Task<string> RetrieveAuthorizationHeaderAsync(NetworkSession? networkSession = null) {
AccessToken token = await this.RetrieveTokenAsync(networkSession: networkSession).ConfigureAwait(false);
return string.Concat("Bearer ", token.AccessTokenField);
}

}
}
5 changes: 5 additions & 0 deletions Box.Sdk.Gen/Box/JwtAuth/BoxJwtAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public async System.Threading.Tasks.Task<AccessToken> RetrieveTokenAsync(Network
return oldToken;
}

public async System.Threading.Tasks.Task<string> RetrieveAuthorizationHeaderAsync(NetworkSession? networkSession = null) {
AccessToken token = await this.RetrieveTokenAsync(networkSession: networkSession).ConfigureAwait(false);
return string.Concat("Bearer ", token.AccessTokenField);
}

/// <summary>
/// Create a new BoxJWTAuth instance that uses the provided user ID as the subject of the JWT assertion.
/// May be one of this application's created App User. Depending on the configured User Access Level, may also be any other App User or Managed User in the enterprise.
Expand Down
6 changes: 5 additions & 1 deletion Box.Sdk.Gen/Box/Oauth/BoxOAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public async System.Threading.Tasks.Task<AccessToken> RefreshTokenAsync(NetworkS
return token;
}

public async System.Threading.Tasks.Task<string> RetrieveAuthorizationHeaderAsync(NetworkSession? networkSession = null) {
AccessToken token = await this.RetrieveTokenAsync(networkSession: networkSession).ConfigureAwait(false);
return string.Concat("Bearer ", token.AccessTokenField);
}

/// <summary>
/// Revoke an active Access Token, effectively logging a user out that has been previously authenticated.
/// </summary>
Expand All @@ -94,7 +99,6 @@ public async System.Threading.Tasks.Task RevokeTokenAsync(NetworkSession? networ
}
AuthorizationManager authManager = networkSession != null ? new AuthorizationManager(networkSession: networkSession) : new AuthorizationManager();
await authManager.RevokeAccessTokenAsync(requestBody: new PostOAuth2Revoke() { ClientId = this.Config.ClientId, ClientSecret = this.Config.ClientSecret, Token = token.AccessTokenField }).ConfigureAwait(false);
await this.TokenStorage.ClearAsync().ConfigureAwait(false);
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion Box.Sdk.Gen/Networking/Auth/IAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ namespace Box.Sdk.Gen {
public interface IAuthentication {
public System.Threading.Tasks.Task<AccessToken> RetrieveTokenAsync(NetworkSession? networkSession = null);

public System.Threading.Tasks.Task<AccessToken> RefreshTokenAsync(NetworkSession networkSession);
public System.Threading.Tasks.Task<AccessToken> RefreshTokenAsync(NetworkSession? networkSession = null);

public System.Threading.Tasks.Task<string> RetrieveAuthorizationHeaderAsync(NetworkSession? networkSession = null);

}
}
4 changes: 2 additions & 2 deletions Box.Sdk.Gen/Networking/Fetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ private static async Task<HttpRequestMessage> BuildHttpRequest(string resource,

if (options.Auth != null)
{
var token = await options.Auth.RetrieveTokenAsync().ConfigureAwait(false);
httpRequest.Headers.Add("Authorization", $"Bearer {token.AccessTokenField}");
var authHeaderValue = await options.Auth.RetrieveAuthorizationHeaderAsync(options.NetworkSession).ConfigureAwait(false);
httpRequest.Headers.Add("Authorization", authHeaderValue);
}

return httpRequest;
Expand Down
Loading