Skip to content

Commit

Permalink
Misskeyでのファイル添付時に必要な権限が認可されているかチェックする
Browse files Browse the repository at this point in the history
  • Loading branch information
upsilon committed Jun 11, 2024
1 parent 09862ac commit 923f244
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 3 deletions.
2 changes: 2 additions & 0 deletions OpenTween/Api/Misskey/DriveFileCreateRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class DriveFileCreateRequest

public async Task<MisskeyDriveFile> Send(IApiConnection apiConnection)
{
apiConnection.ThrowIfUnauthorizedScope("write:drive");

var request = new PostMultipartRequest
{
RequestUri = new("drive/files/create", UriKind.Relative),
Expand Down
33 changes: 33 additions & 0 deletions OpenTween/Connection/AdditionalScopeRequiredException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// OpenTween - Client of Twitter
// Copyright (c) 2024 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
// All rights reserved.
//
// This file is part of OpenTween.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.

#nullable enable

namespace OpenTween.Connection
{
public class AdditionalScopeRequiredException : WebApiException
{
public AdditionalScopeRequiredException()
: base(Properties.Resources.AdditionalScopeRequired_Message)
{
}
}
}
2 changes: 2 additions & 0 deletions OpenTween/Connection/IApiConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace OpenTween.Connection
{
public interface IApiConnection : IDisposable
{
void ThrowIfUnauthorizedScope(string scope);

Task<ApiResponse> SendAsync(IHttpRequest request);
}
}
12 changes: 11 additions & 1 deletion OpenTween/Connection/MisskeyApiConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Cache;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
using OpenTween.Api.Misskey;
using OpenTween.SocialProtocol.Misskey;

namespace OpenTween.Connection
{
Expand All @@ -40,11 +42,13 @@ public sealed class MisskeyApiConnection : IApiConnection, IDisposable

private readonly Uri apiBaseUri;
private readonly string accessToken;
private readonly MisskeyAccountState accountState;

public MisskeyApiConnection(Uri apiBaseUri, string accessToken)
public MisskeyApiConnection(Uri apiBaseUri, string accessToken, MisskeyAccountState accountState)
{
this.apiBaseUri = apiBaseUri;
this.accessToken = accessToken;
this.accountState = accountState;

this.InitializeHttpClients();
Networking.WebProxyChanged += this.Networking_WebProxyChanged;
Expand All @@ -59,6 +63,12 @@ private void InitializeHttpClients()
this.Http.Timeout = Timeout.InfiniteTimeSpan;
}

public void ThrowIfUnauthorizedScope(string scope)
{
if (!this.accountState.AuthorizedScopes.Contains(scope))
throw new AdditionalScopeRequiredException();
}

public async Task<ApiResponse> SendAsync(IHttpRequest request)
{
using var requestMessage = request.CreateMessage(this.apiBaseUri);
Expand Down
4 changes: 4 additions & 0 deletions OpenTween/Connection/TwitterApiConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ private void InitializeHttpClients()
this.Http.Timeout = Timeout.InfiniteTimeSpan;
}

public void ThrowIfUnauthorizedScope(string scope)
{
}

public async Task<ApiResponse> SendAsync(IHttpRequest request)
{
var endpointName = request.EndpointName;
Expand Down
9 changes: 9 additions & 0 deletions OpenTween/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions OpenTween/Properties/Resources.en.resx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<data name="AccountListBoxItem_Disabled"><value>(Disabled)</value></data>
<data name="AccountListBoxItem_Primary"><value>(Primary)</value></data>
<data name="AccountTypeErrorText"><value>This feature is not available for current account.</value></data>
<data name="AdditionalScopeRequired_Message"><value>You will need to re-authorize the account to use this feature.</value></data>
<data name="AddNewTab_ListView_AccessibleName"><value>PostsList</value></data>
<data name="AddNewTabText1"><value>(New Tab)</value></data>
<data name="AddNewTabText2"><value>Name</value></data>
Expand Down
1 change: 1 addition & 0 deletions OpenTween/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<data name="AccountListBoxItem_Disabled"><value>(無効)</value></data>
<data name="AccountListBoxItem_Primary"><value>(メイン)</value></data>
<data name="AccountTypeErrorText"><value>この機能は現在のアカウントでは使用できません</value></data>
<data name="AdditionalScopeRequired_Message"><value>この機能を使用するためにはアカウントの再設定が必要です</value></data>
<data name="AddNewTab_ListView_AccessibleName"><value>発言一覧</value></data>
<data name="AddNewTabText1"><value>(新規タブ)</value></data>
<data name="AddNewTabText2"><value>名前</value></data>
Expand Down
4 changes: 4 additions & 0 deletions OpenTween/SocialProtocol/InvalidAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public void Dispose()

private class InvalidAccountConnection : IApiConnection
{
public void ThrowIfUnauthorizedScope(string scope)
{
}

public Task<ApiResponse> SendAsync(IHttpRequest request)
=> throw new WebApiException("Invalid account");

Expand Down
3 changes: 2 additions & 1 deletion OpenTween/SocialProtocol/Misskey/MisskeyAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ public void Initialize(UserAccount accountSettings, SettingCommon settingCommon)
var serverUri = new Uri($"https://{accountSettings.ServerHostname}/");
this.AccountState = new(serverUri, new(accountSettings.UserId), accountSettings.Username)
{
AuthorizedScopes = accountSettings.Scopes,
HasUnrecoverableError = false,
};

var apiBaseUri = new Uri(serverUri, "/api/");

var newConnection = new MisskeyApiConnection(apiBaseUri, accountSettings.TokenSecret);
var newConnection = new MisskeyApiConnection(apiBaseUri, accountSettings.TokenSecret, this.AccountState);
(this.connection, var oldConnection) = (newConnection, this.connection);
oldConnection?.Dispose();
}
Expand Down
2 changes: 2 additions & 0 deletions OpenTween/SocialProtocol/Misskey/MisskeyAccountState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ PersonId ISocialAccountState.UserId

public string UserName { get; private set; }

public string[] AuthorizedScopes { get; set; } = Array.Empty<string>();

public int? FollowersCount { get; private set; }

public int? FriendsCount { get; private set; }
Expand Down
2 changes: 1 addition & 1 deletion OpenTween/SocialProtocol/Misskey/MisskeySetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public async Task DoAuthorize()
throw new InvalidOperationException($"{nameof(this.serverBaseUri)} is null");

var apiBaseUri = new Uri(this.serverBaseUri, "/api/");
var apiConnection = new MisskeyApiConnection(apiBaseUri, accessToken: "");
var apiConnection = new MisskeyApiConnection(apiBaseUri, accessToken: "", new());
var request = new MiauthCheckRequest
{
SessionNonce = this.sessionNonce.ToString(),
Expand Down

0 comments on commit 923f244

Please sign in to comment.