Skip to content

Commit 4c0c458

Browse files
committed
fixup! 設定画面にMastodonアカウントの認証機能を実装
1 parent 4dcc887 commit 4c0c458

3 files changed

Lines changed: 48 additions & 28 deletions

File tree

OpenTween/Api/MastodonApi.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,31 @@ public Task<MastodonInstance> Instance()
8181
return this.Connection.GetAsync<MastodonInstance>(endpoint, null);
8282
}
8383

84-
public async Task<MastodonAccessToken> OAuthToken(string clientId, string clientSecret, string grantType,
85-
string username, string password, string scope = null)
84+
public Uri OAuthAuthorize(string clientId, string responseType, Uri redirectUri, string scope)
85+
{
86+
var endpoint = new Uri("/oauth/authorize", UriKind.Relative);
87+
var param = new Dictionary<string, string>
88+
{
89+
["client_id"] = clientId,
90+
["response_type"] = responseType,
91+
["redirect_uri"] = redirectUri.AbsoluteUri,
92+
["scope"] = scope,
93+
};
94+
95+
return new Uri(new Uri(this.InstanceUri, endpoint), "?" + MyCommon.BuildQueryString(param));
96+
}
97+
98+
public async Task<MastodonAccessToken> OAuthToken(string clientId, string clientSecret, Uri redirectUri,
99+
string grantType, string code, string scope = null)
86100
{
87101
var endpoint = new Uri("/oauth/token", UriKind.Relative);
88102
var param = new Dictionary<string, string>
89103
{
90104
["client_id"] = clientId,
91105
["client_secret"] = clientSecret,
106+
["redirect_uri"] = redirectUri.AbsoluteUri,
92107
["grant_type"] = grantType,
93-
["username"] = username,
94-
["password"] = password,
108+
["code"] = code,
95109
};
96110

97111
if (scope != null)

OpenTween/Mastodon.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,24 @@ public static async Task<MastodonRegisteredApp> RegisterClientAsync(Uri instance
7272
}
7373
}
7474

75+
public static Uri GetAuthorizeUri(Uri instanceUri, string clientId)
76+
{
77+
var redirectUri = new Uri("urn:ietf:wg:oauth:2.0:oob");
78+
var scope = "read write follow";
79+
using (var api = new MastodonApi(instanceUri))
80+
{
81+
return api.OAuthAuthorize(clientId, "code", redirectUri, scope);
82+
}
83+
}
84+
7585
public static async Task<string> GetAccessTokenAsync(Uri instanceUri, string clientId, string clientSecret,
76-
string username, string password)
86+
string authorizationCode)
7787
{
7888
using (var api = new MastodonApi(instanceUri))
7989
{
90+
var redirectUri = new Uri("urn:ietf:wg:oauth:2.0:oob");
8091
var scope = "read write follow";
81-
var token = await api.OAuthToken(clientId, clientSecret, "password", username, password, scope)
92+
var token = await api.OAuthToken(clientId, clientSecret, redirectUri, "authorization_code", authorizationCode, scope)
8293
.ConfigureAwait(false);
8394

8495
return token.AccessToken;

OpenTween/Setting/Panel/BasedPanel.cs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -126,30 +126,25 @@ private async void buttonMastodonAuth_Click(object sender, EventArgs e)
126126

127127
var application = await Mastodon.RegisterClientAsync(instanceUri);
128128

129-
using (var loginDialog = new LoginDialog())
130-
{
131-
string accessToken = null;
129+
var authorizeUri = Mastodon.GetAuthorizeUri(instanceUri, application.ClientId);
132130

133-
loginDialog.LoginCallback = async () =>
134-
{
135-
try
136-
{
137-
accessToken = await Mastodon.GetAccessTokenAsync(instanceUri,
138-
application.ClientId, application.ClientSecret, loginDialog.LoginName, loginDialog.Password);
139-
140-
return true;
141-
}
142-
catch (WebApiException ex)
143-
{
144-
var message = $"認証に失敗しました: {ex.Message}";
145-
MessageBox.Show(loginDialog, message, ApplicationSettings.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
146-
return false;
147-
}
148-
};
149-
loginDialog.ShowDialog(this);
150-
151-
this.mastodonCredential = await Mastodon.VerifyCredentialAsync(instanceUri, accessToken);
131+
var code = AuthDialog.DoAuth(this, authorizeUri);
132+
if (string.IsNullOrEmpty(code))
133+
return;
134+
135+
string accessToken;
136+
try
137+
{
138+
accessToken = await Mastodon.GetAccessTokenAsync(instanceUri, application.ClientId, application.ClientSecret, code);
152139
}
140+
catch (WebApiException ex)
141+
{
142+
var message = Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine + ex.Message;
143+
MessageBox.Show(this, message, "Authenticate", MessageBoxButtons.OK, MessageBoxIcon.Error);
144+
return;
145+
}
146+
147+
this.mastodonCredential = await Mastodon.VerifyCredentialAsync(instanceUri, accessToken);
153148

154149
this.RefreshMastodonCredential();
155150
}

0 commit comments

Comments
 (0)