Skip to content
This repository has been archived by the owner on Nov 29, 2020. It is now read-only.

Commit

Permalink
Fix logic
Browse files Browse the repository at this point in the history
  • Loading branch information
HMBSbige committed Jan 4, 2020
1 parent 025737e commit 4732575
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 18 deletions.
2 changes: 1 addition & 1 deletion AutoSplitVideo.WPF/Model/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public bool FixTimestamp

public string Token
{
get => string.IsNullOrEmpty(_token) || _token.Length != 32 ? string.Empty : _token;
get => BilibiliApi.Utils.IsToken(_token) ? _token : string.Empty;
set => SetField(ref _token, value);
}

Expand Down
14 changes: 11 additions & 3 deletions AutoSplitVideo.WPF/View/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,20 @@
<xctk:WatermarkTextBox Margin="3" Watermark="密码"
Text="{Binding Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />

<Button x:Name="LoginButton" Margin="3,3,3,6" Padding="2" Content="获取 Access Token" HorizontalAlignment="Left" Click="LoginButton_OnClick"/>
<Button x:Name="LoginButton" Margin="3,3,3,6" Padding="2" Content="获取 Token" HorizontalAlignment="Left" Click="LoginButton_OnClick"/>

<xctk:WatermarkTextBox Margin="3,0" Watermark="SESSDATA/Access Token"
<xctk:WatermarkTextBox Margin="3" Watermark="SESSDATA/Access Token"
Text="{Binding Token,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

<Button x:Name="ApplyToApiButton" Margin="3,3,3,6" Padding="2" Content="应用" HorizontalAlignment="Left" Click="ApplyToApiButton_OnClick"/>
<WrapPanel>
<Button x:Name="ApplyToApiButton"
Margin="3,3,3,6" Padding="2"
Content="登录" Click="ApplyToApiButton_OnClick" />
<Button x:Name="RevokeButton"
Margin="3,3,3,6" Padding="2"
Content="注销" Click="RevokeButton_OnClick"/>
</WrapPanel>

<TextBlock Margin="3,0,0,0" Text="{Binding Status,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</GroupBox>
Expand Down
19 changes: 11 additions & 8 deletions AutoSplitVideo.WPF/View/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,7 @@ private void LoginButton_OnClick(object sender, RoutedEventArgs e)
LoginButton.IsEnabled = false;
MainWindowViewModel.Login().ContinueWith(task =>
{
Dispatcher?.InvokeAsync(() =>
{
LoginButton.IsEnabled = true;
});
Dispatcher?.InvokeAsync(() => { LoginButton.IsEnabled = true; });
});
}

Expand All @@ -550,10 +547,16 @@ private void ApplyToApiButton_OnClick(object sender, RoutedEventArgs e)
ApplyToApiButton.IsEnabled = false;
MainWindowViewModel.ApplyToApi().ContinueWith(task =>
{
Dispatcher?.InvokeAsync(() =>
{
ApplyToApiButton.IsEnabled = true;
});
Dispatcher?.InvokeAsync(() => { ApplyToApiButton.IsEnabled = true; });
});
}

private void RevokeButton_OnClick(object sender, RoutedEventArgs e)
{
RevokeButton.IsEnabled = false;
MainWindowViewModel.Revoke().ContinueWith(task =>
{
Dispatcher?.InvokeAsync(() => { RevokeButton.IsEnabled = true; });
});
}

Expand Down
23 changes: 22 additions & 1 deletion AutoSplitVideo.WPF/ViewModel/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ public async Task Login()
{
Token = token.AccessToken;
UpdateStatus($@"获取 Access Token 成功,Token 有效期至 {token.Expires.AddHours(8)}");
Account = Password = string.Empty;
}
}
catch (Exception ex)
Expand All @@ -410,7 +411,7 @@ public async Task ApplyToApi()
CurrentConfig.Token = Token;
UpdateStatus(@"Cookie 应用成功");
}
else
else if (BilibiliApi.Utils.IsToken(Token))
{
try
{
Expand All @@ -431,13 +432,33 @@ public async Task ApplyToApi()
UpdateStatus($@"登录失败,{ex.Message}");
}
}
else
{
UpdateStatus(@"登录失败,Access Token 格式错误");
}
}
else
{
UpdateStatus($@"登录失败,SESSDATA/Access Token 的长度为 {Token.Length} ≠ 32");
}
}

public async Task Revoke()
{
if (BilibiliApi.Utils.IsToken(Token))
{
//TODO
await BilibiliApi.BililiveApi.RevokeToken(Token);
UpdateStatus(@"注销请求发送完成");
Token = string.Empty;
BilibiliApi.BililiveApi.Reload(null);
}
else
{
UpdateStatus(@"Access Token 格式错误");
}
}

#endregion
}
}
17 changes: 13 additions & 4 deletions BilibiliApi/BililiveAPI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BilibiliApi.Model;
using BilibiliApi.Model;
using System;
using System.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -147,11 +147,20 @@ public static async Task<BilibiliToken> LoginAsync(string userName, string passw
public static async Task<BilibiliToken> GetTokenInfo(string accessToken)
{
var token = new BilibiliToken(await Passport.Passport.GetTokenInfo(accessToken));
if (token.Code == 0)
return token.Code == 0 ? token : null;
}

public static async Task RevokeToken(string accessToken)
{
//TODO
try
{
return token;
await Passport.Passport.Revoke(accessToken);
}
catch
{
// ignored
}
return null;
}
}
}
14 changes: 13 additions & 1 deletion BilibiliApi/Passport/Passport.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BilibiliApi.Passport
Expand Down Expand Up @@ -32,6 +32,18 @@ public static async Task<string> Login(string hash, string publicKey, string use
return await PostAsync(BaseUri, requestUri, body);
}

public static async Task<string> Revoke(string accessToken)
{
const string requestUri = @"api/oauth2/revoke";
var pair = new Dictionary<string, string>
{
{@"platform", @"android"},
{@"access_token", accessToken}
};
using var body = await GetBody(pair, true);
return await PostAsync(BaseUri, requestUri, body);
}

public static async Task<string> GetTokenInfo(string accessToken)
{
var pair = new Dictionary<string, string>
Expand Down
6 changes: 6 additions & 0 deletions BilibiliApi/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace BilibiliApi
{
Expand Down Expand Up @@ -84,5 +85,10 @@ public static DateTime GetTime(string timeStamp)
var toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
}

public static bool IsToken(string token)
{
return token.Length == 32 && Regex.IsMatch(token, @"^[a-f0-9]+$");
}
}
}

0 comments on commit 4732575

Please sign in to comment.