Fixed Teknik uploader to use refresh tokens, and require auth for file uploads

This commit is contained in:
Uncled1023 2020-08-22 16:17:44 -07:00
parent bffcabc909
commit 4cc9bba3c2
2 changed files with 73 additions and 4 deletions

View file

@ -42,7 +42,9 @@ public class TeknikFileUploaderService : FileUploaderService
public override bool CheckConfig(UploadersConfig config)
{
return !string.IsNullOrEmpty(config.TeknikUploadAPIUrl) && !string.IsNullOrEmpty(config.TeknikAuthUrl);
return !string.IsNullOrEmpty(config.TeknikUploadAPIUrl) &&
!string.IsNullOrEmpty(config.TeknikAuthUrl) &&
OAuth2Info.CheckOAuth(config.TeknikOAuth2Info);
}
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
@ -71,7 +73,7 @@ public enum TeknikExpirationUnit
Years
}
public class Teknik : Uploader, IOAuth2Basic
public class Teknik : Uploader, IOAuth2
{
public const string DefaultUploadAPIURL = "https://api.teknik.io/v1/Upload";
public const string DefaultPasteAPIURL = "https://api.teknik.io/v1/Paste";
@ -87,6 +89,53 @@ public Teknik(OAuth2Info oauth, string authUrl)
AuthUrl = authUrl;
}
public bool RefreshAccessToken()
{
if (OAuth2Info.CheckOAuth(AuthInfo) && !string.IsNullOrEmpty(AuthInfo.Token.refresh_token))
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("refresh_token", AuthInfo.Token.refresh_token);
args.Add("client_id", AuthInfo.Client_ID);
args.Add("client_secret", AuthInfo.Client_Secret);
args.Add("grant_type", "refresh_token");
string response = SendRequestMultiPart(AuthUrl + "/connect/token", args);
if (!string.IsNullOrEmpty(response))
{
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
if (token != null && !string.IsNullOrEmpty(token.access_token))
{
token.UpdateExpireDate();
AuthInfo.Token = token;
return true;
}
}
}
return false;
}
public bool CheckAuthorization()
{
if (OAuth2Info.CheckOAuth(AuthInfo))
{
if (AuthInfo.Token.IsExpired && !RefreshAccessToken())
{
Errors.Add("Refresh access token failed.");
return false;
}
}
else
{
Errors.Add("Teknik login is required.");
return false;
}
return true;
}
public bool GetAccessToken(string code)
{
Dictionary<string, string> args = new Dictionary<string, string>();
@ -135,7 +184,7 @@ public NameValueCollection GetAuthHeaders()
}
}
public sealed class TeknikUploader : FileUploader, IOAuth2Basic
public sealed class TeknikUploader : FileUploader, IOAuth2
{
public OAuth2Info AuthInfo { get; set; }
public string APIUrl { get; set; }
@ -187,6 +236,16 @@ public bool GetAccessToken(string code)
{
return teknik.GetAccessToken(code);
}
public bool RefreshAccessToken()
{
return teknik.RefreshAccessToken();
}
public bool CheckAuthorization()
{
return teknik.CheckAuthorization();
}
}
public class TeknikErrorResponse

View file

@ -54,7 +54,7 @@ public override URLShortener CreateShortener(UploadersConfig config, TaskReferen
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpTeknik;
}
public sealed class TeknikUrlShortener : URLShortener, IOAuth2Basic
public sealed class TeknikUrlShortener : URLShortener, IOAuth2
{
public OAuth2Info AuthInfo { get; set; }
public string APIUrl { get; set; }
@ -93,6 +93,16 @@ public override UploadResult ShortenURL(string url)
return ur;
}
public bool RefreshAccessToken()
{
return teknik.RefreshAccessToken();
}
public bool CheckAuthorization()
{
return teknik.CheckAuthorization();
}
}
public class TeknikUrlShortenerResponseWrapper