NTFY username and password usage instead of auth.
This commit is contained in:
parent
42feea3ad5
commit
d52ec8d36f
@ -12,28 +12,41 @@ public class Ntfy : NotificationConnector
|
|||||||
public string topic { get; init; }
|
public string topic { get; init; }
|
||||||
private readonly HttpClient _client = new();
|
private readonly HttpClient _client = new();
|
||||||
|
|
||||||
public Ntfy(GlobalBase clone, string endpoint, string auth) : base(clone, NotificationConnectorType.Ntfy)
|
[JsonConstructor]
|
||||||
|
public Ntfy(GlobalBase clone, string endpoint, string topic, string auth) : base(clone, NotificationConnectorType.Ntfy)
|
||||||
{
|
{
|
||||||
if (!baseUrlRex.IsMatch(endpoint))
|
this.endpoint = endpoint;
|
||||||
throw new ArgumentException("endpoint does not match pattern");
|
this.topic = topic;
|
||||||
Regex rootUriRex = new(@"(https?:\/\/[a-zA-Z0-9-\.]+\.[a-zA-Z0-9]+)(?:\/([a-zA-Z0-9-\.]+))?.*");
|
|
||||||
Match match = rootUriRex.Match(endpoint);
|
|
||||||
if(!match.Success)
|
|
||||||
Log($"Error getting URI from provided endpoint-URI: {endpoint}");
|
|
||||||
this.endpoint = match.Groups[1].Value;
|
|
||||||
if (match.Groups[2].Success)
|
|
||||||
topic = match.Groups[2].Value;
|
|
||||||
else
|
|
||||||
topic = "tranga";
|
|
||||||
this.auth = auth;
|
this.auth = auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonConstructor]
|
public Ntfy(GlobalBase clone, string endpoint, string username, string password, string? topic = null) :
|
||||||
public Ntfy(GlobalBase clone, string endpoint, string auth, string topic) : base(clone, NotificationConnectorType.Ntfy)
|
this(clone, EndpointAndTopicFromUrl(endpoint)[0], topic??EndpointAndTopicFromUrl(endpoint)[1], AuthFromUsernamePassword(username, password))
|
||||||
{
|
{
|
||||||
this.endpoint = endpoint;
|
|
||||||
this.topic = topic.Length > 0 ? topic : "tranga";
|
}
|
||||||
this.auth = auth;
|
|
||||||
|
private static string AuthFromUsernamePassword(string username, string password)
|
||||||
|
{
|
||||||
|
string authHeader = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));
|
||||||
|
string authParam = Convert.ToBase64String(Encoding.UTF8.GetBytes(authHeader)).Replace("=","");
|
||||||
|
return authParam;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string[] EndpointAndTopicFromUrl(string url)
|
||||||
|
{
|
||||||
|
string[] ret = new string[2];
|
||||||
|
if (!baseUrlRex.IsMatch(url))
|
||||||
|
throw new ArgumentException("url does not match pattern");
|
||||||
|
Regex rootUriRex = new(@"(https?:\/\/[a-zA-Z0-9-\.]+\.[a-zA-Z0-9]+)(?:\/([a-zA-Z0-9-\.]+))?.*");
|
||||||
|
Match match = rootUriRex.Match(url);
|
||||||
|
if(!match.Success)
|
||||||
|
throw new ArgumentException($"Error getting URI from provided endpoint-URI: {url}");
|
||||||
|
|
||||||
|
ret[0] = match.Groups[1].Value;
|
||||||
|
ret[1] = match.Groups[2].Success && match.Groups[2].Value.Length > 0 ? match.Groups[2].Value : "tranga";
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
|
Loading…
Reference in New Issue
Block a user