Skip to content

Commit

Permalink
Add support for app auth endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiming Yuan committed Nov 17, 2016
1 parent da65db3 commit 5bbef76
Show file tree
Hide file tree
Showing 15 changed files with 373 additions and 155 deletions.
79 changes: 77 additions & 2 deletions Dropbox.Api.Tests/DropboxApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace Dropbox.Api.Tests

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Dropbox.Api.Auth;

/// <summary>
/// The test class for Dropbox API.
/// </summary>
Expand All @@ -27,11 +29,29 @@ public class DropboxApiTests
/// </summary>
public static DropboxClient Client;

/// <summary>
/// The Dropbox team client.
/// </summary>
public static DropboxTeamClient TeamClient;

/// <summary>
/// The Dropbox app client.
/// </summary>
public static DropboxAppClient AppClient;

[ClassInitialize]
public static void Initialize(TestContext context)
{
var token = context.Properties["accessToken"].ToString();
Client = new DropboxClient(token);
var userToken = context.Properties["userAccessToken"].ToString();
Client = new DropboxClient(userToken);

var teamToken = context.Properties["teamAccessToken"].ToString();
TeamClient = new DropboxTeamClient(teamToken);

var appKey = context.Properties["appKey"].ToString();
var appSecret = context.Properties["appSecret"].ToString();

AppClient = new DropboxAppClient(appKey, appSecret);
}


Expand Down Expand Up @@ -174,6 +194,61 @@ public async Task TestRequestId()
}
}

/// Test team auth.
/// </summary>
/// <returns>The <see cref="Task"/></returns>
[TestMethod]
public async Task TestTeamAuth()
{
var result = await TeamClient.Team.GetInfoAsync();
Assert.IsNotNull(result.TeamId);
Assert.IsNotNull(result.Name);
}

/// Test team auth select user.
/// </summary>
/// <returns>The <see cref="Task"/></returns>
[TestMethod]
public async Task TestTeamAuthSelectUser()
{
var result = await TeamClient.Team.MembersListAsync();
var memberId = result.Members[0].Profile.TeamMemberId;

var userClient = TeamClient.AsMember(memberId);
var account = await userClient.Users.GetCurrentAccountAsync();
Assert.AreEqual(account.TeamMemberId, memberId);
}

/// Test app auth.
/// </summary>
/// <returns>The <see cref="Task"/></returns>
[TestMethod]
public async Task TestAppAuth()
{
try
{
var result = await AppClient.Auth.TokenFromOauth1Async("foo", "bar");
}
catch (ApiException<TokenFromOAuth1Error>)
{
}
}

/// Test no auth.
/// </summary>
/// <returns>The <see cref="Task"/></returns>
[TestMethod]
public async Task TestNoAuth()
{
var result = await Client.Files.ListFolderAsync("", recursive: true);
var cursor = result.Cursor;

var task = Client.Files.ListFolderLongpollAsync(cursor);
await Client.Files.UploadAsync("/foo.txt", body: GetStream("abc"));
var response = await task;
Assert.IsTrue(response.Changes);
}

/// <summary>
/// Converts string to a memory stream.
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion Dropbox.Api.Tests/dropbox.runsettings
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<TestRunParameters>
<Parameter name="accessToken" value="" />
<Parameter name="userAccessToken" value="" />
<Parameter name="teamAccessToken" value="" />
<Parameter name="appKey" value="" />
<Parameter name="appSecret" value="" />
</TestRunParameters>
</RunSettings>
2 changes: 1 addition & 1 deletion Dropbox.Api/Auth/AuthAppRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal AuthAppRoutes(enc.ITransport transport)
/// cref="TokenFromOAuth1Error"/>.</exception>
public t.Task<TokenFromOAuth1Result> TokenFromOauth1Async(TokenFromOAuth1Arg tokenFromOAuth1Arg)
{
return this.Transport.SendRpcRequestAsync<TokenFromOAuth1Arg, TokenFromOAuth1Result, TokenFromOAuth1Error>(tokenFromOAuth1Arg, "api", "/auth/token/from_oauth1", Dropbox.Api.Auth.TokenFromOAuth1Arg.Encoder, Dropbox.Api.Auth.TokenFromOAuth1Result.Decoder, Dropbox.Api.Auth.TokenFromOAuth1Error.Decoder);
return this.Transport.SendRpcRequestAsync<TokenFromOAuth1Arg, TokenFromOAuth1Result, TokenFromOAuth1Error>(tokenFromOAuth1Arg, "api", "/auth/token/from_oauth1", "app", Dropbox.Api.Auth.TokenFromOAuth1Arg.Encoder, Dropbox.Api.Auth.TokenFromOAuth1Result.Decoder, Dropbox.Api.Auth.TokenFromOAuth1Error.Decoder);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Dropbox.Api/Auth/AuthUserRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal AuthUserRoutes(enc.ITransport transport)
/// <returns>The task that represents the asynchronous send operation.</returns>
public t.Task TokenRevokeAsync()
{
return this.Transport.SendRpcRequestAsync<enc.Empty, enc.Empty, enc.Empty>(enc.Empty.Instance, "api", "/auth/token/revoke", enc.EmptyEncoder.Instance, enc.EmptyDecoder.Instance, enc.EmptyDecoder.Instance);
return this.Transport.SendRpcRequestAsync<enc.Empty, enc.Empty, enc.Empty>(enc.Empty.Instance, "api", "/auth/token/revoke", "user", enc.EmptyEncoder.Instance, enc.EmptyDecoder.Instance, enc.EmptyDecoder.Instance);
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Dropbox.Api/Dropbox.Api.Portable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="Auth\TokenFromOAuth1Error.cs" />
<Compile Include="Auth\TokenFromOAuth1Result.cs" />
<Compile Include="AuthException.cs" />
<Compile Include="DropboxAppClient.cs" />
<Compile Include="DropboxClient.cs" />
<Compile Include="DropboxTeamClient.cs" />
<Compile Include="Files\AddPropertiesError.cs" />
Expand Down
1 change: 1 addition & 0 deletions Dropbox.Api/Dropbox.Api.Portable40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<Compile Include="Auth\TokenFromOAuth1Error.cs" />
<Compile Include="Auth\TokenFromOAuth1Result.cs" />
<Compile Include="AuthException.cs" />
<Compile Include="DropboxAppClient.cs" />
<Compile Include="DropboxClient.cs" />
<Compile Include="DropboxTeamClient.cs" />
<Compile Include="Files\AddPropertiesError.cs" />
Expand Down
1 change: 1 addition & 0 deletions Dropbox.Api/Dropbox.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<Compile Include="Auth\TokenFromOAuth1Error.cs" />
<Compile Include="Auth\TokenFromOAuth1Result.cs" />
<Compile Include="AuthException.cs" />
<Compile Include="DropboxAppClient.cs" />
<Compile Include="DropboxClient.cs" />
<Compile Include="DropboxTeamClient.cs" />
<Compile Include="Files\AddPropertiesError.cs" />
Expand Down
28 changes: 28 additions & 0 deletions Dropbox.Api/DropboxAppClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// <auto-generated>
// Auto-generated by StoneAPI, do not modify.
// </auto-generated>

namespace Dropbox.Api
{
using sys = System;

using Dropbox.Api.Stone;
using Dropbox.Api.Auth.Routes;

public sealed partial class DropboxAppClient
{
/// <summary>
/// <para>Gets the Auth routes.</para>
/// </summary>
public AuthAppRoutes Auth { get; private set; }

/// <summary>
/// <para>Initializes the routes.</para>
/// </summary>
/// <returns>The transport.</returns>
private void InitializeRoutes(ITransport transport)
{
this.Auth = new AuthAppRoutes(transport);
}
}
}
56 changes: 56 additions & 0 deletions Dropbox.Api/DropboxClient.common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,62 @@ public void Dispose()
}
}

/// <summary>
/// The client which contains endpoints which perform app-auth actions.
/// </summary>
public sealed partial class DropboxAppClient
{
/// <summary>
/// Initializes a new instance of the <see cref="T:Dropbox.Api.DropboxAppClient"/> class.
/// </summary>
/// <param name="appKey">The Dropbox app key (e.g. consumer key in OAuth).</param>
/// <param name="appSecret">The Dropbox app secret (e.g. consumer secret in OAuth).</param>
public DropboxAppClient(string appKey, string appSecret)
: this(appKey, appSecret, new DropboxClientConfig())
{
}


/// <summary>
/// Initializes a new instance of the <see cref="T:Dropbox.Api.DropboxClient"/> class.
/// </summary>
/// <param name="appKey">The Dropbox app key (e.g. consumer key in OAuth).</param>
/// <param name="appSecret">The Dropbox app secret (e.g. consumer secret in OAuth).</param>
/// <param name="config">The <see cref="DropboxClientConfig"/>.</param>
public DropboxAppClient(string appKey, string appSecret, DropboxClientConfig config)
{
if (appKey == null)
{
throw new ArgumentNullException("appKey");
}

if (appSecret == null)
{
throw new ArgumentNullException("appSecret");
}

var options = new DropboxRequestHandlerOptions(
GetBasicAuthHeader(appKey, appSecret),
config.MaxRetriesOnError,
config.UserAgent,
httpClient: config.HttpClient);

this.InitializeRoutes(new DropboxRequestHandler(options));
}

/// <summary>
/// Gets the basic auth header from app key and app secret.
/// </summary>
/// <param name="appKey">The app key.</param>
/// <param name="appSecret">The app secret.</param>
/// <returns>The basic auth header.</returns>
private static string GetBasicAuthHeader(string appKey, string appSecret)
{
var rawValue = string.Format("{0}:{1}", appKey, appSecret);
return Convert.ToBase64String(Encoding.UTF8.GetBytes(rawValue));
}
}

/// <summary>
/// The client which contains endpoints which perform team-level actions.
/// </summary>
Expand Down
Loading

0 comments on commit 5bbef76

Please sign in to comment.