Skip to content

Commit

Permalink
Replace Scopes with GranularScopes #107
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Lacasa committed Apr 22, 2023
1 parent 4548c6b commit dd684b7
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 17 deletions.
62 changes: 47 additions & 15 deletions Mastonet/AuthenticationClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Mastonet.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
Expand Down Expand Up @@ -36,11 +37,43 @@ public AuthenticationClient(AppRegistration app, HttpClient client) : base(clien
/// <param name="scope">The rights needed by your application</param>
/// <param name="website">URL to the homepage of your app</param>
/// <returns></returns>
public async Task<AppRegistration> CreateApp(string appName, Scope scope, string? website = null, string? redirectUri = null)
[Obsolete("Use GranularScopes instead of deprecated Scope")]
public Task<AppRegistration> CreateApp(string appName, Scope scope, string? website = null, string? redirectUri = null)
{
var scopes = new List<GranularScope>();
if (scope.HasFlag(Scope.Read))
{
scopes.Add(GranularScope.Read);
}

if (scope.HasFlag(Scope.Write))
{
scopes.Add(GranularScope.Write);
}

if (scope.HasFlag(Scope.Follow))
{
scopes.AddRange(new GranularScope[] {
GranularScope.Read__Blocks, GranularScope.Write__Blocks,
GranularScope.Read__Follows, GranularScope.Write__Follows,
GranularScope.Read__Mutes, GranularScope.Write__Mutes
});
}

return CreateApp(appName, website, redirectUri, scopes);
}

public Task<AppRegistration> CreateApp(string appName, string? website = null, string? redirectUri = null, params GranularScope[] scope)
{
return CreateApp(appName, website, redirectUri, scope.AsEnumerable());
}

public async Task<AppRegistration> CreateApp(string appName, string? website = null, string? redirectUri = null, IEnumerable<GranularScope>? scope = null)
{
var scopeString = GetScopeParam(scope);
var data = new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("client_name", appName),
new KeyValuePair<string, string>("scopes", GetScopeParam(scope)),
new KeyValuePair<string, string>("scopes", scopeString),
new KeyValuePair<string, string>("redirect_uris", redirectUri?? "urn:ietf:wg:oauth:2.0:oob")
};

Expand All @@ -52,7 +85,7 @@ public async Task<AppRegistration> CreateApp(string appName, Scope scope, string
var appRegistration = await Post<AppRegistration>("/api/v1/apps", data);

appRegistration.Instance = Instance;
appRegistration.Scope = scope;
appRegistration.Scope = scopeString;
this.AppRegistration = appRegistration;

return appRegistration;
Expand All @@ -62,7 +95,7 @@ public async Task<AppRegistration> CreateApp(string appName, Scope scope, string

#region Auth

public Task<Auth> ConnectWithPassword(string email, string password)
public Task<Auth> ConnectWithPassword(string email, string password)
{
if (AppRegistration == null)
{
Expand All @@ -76,13 +109,13 @@ public Task<Auth> ConnectWithPassword(string email, string password)
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", email),
new KeyValuePair<string, string>("password", password),
new KeyValuePair<string, string>("scope", GetScopeParam(AppRegistration.Scope)),
new KeyValuePair<string, string>("scope", AppRegistration.Scope),
};

return Post<Auth>("/oauth/token", data);
}

public Task<Auth> ConnectWithCode(string code, string? redirect_uri = null)
public Task<Auth> ConnectWithCode(string code, string? redirect_uri = null)
{
if (AppRegistration == null)
{
Expand Down Expand Up @@ -117,7 +150,7 @@ public string OAuthUrl(string? redirectUri = null)
redirectUri = "urn:ietf:wg:oauth:2.0:oob";
}

return $"https://{this.Instance}/oauth/authorize?response_type=code&client_id={AppRegistration.ClientId}&scope={GetScopeParam(AppRegistration.Scope).Replace(" ", "%20")}&redirect_uri={redirectUri ?? "urn:ietf:wg:oauth:2.0:oob"}";
return $"https://{this.Instance}/oauth/authorize?response_type=code&client_id={AppRegistration.ClientId}&scope={AppRegistration.Scope.Replace(" ", "%20")}&redirect_uri={redirectUri ?? "urn:ietf:wg:oauth:2.0:oob"}";
}

/// <summary>
Expand All @@ -142,20 +175,19 @@ public Task Revoke(string token)
return Post<Auth>("/oauth/revoke", data);
}

private static string GetScopeParam(Scope scope)
private static string GetScopeParam(IEnumerable<GranularScope>? scopes)
{
var scopeParam = "";
if ((scope & Scope.Read) == Scope.Read) scopeParam += " read";
if ((scope & Scope.Write) == Scope.Write) scopeParam += " write";
if ((scope & Scope.Follow) == Scope.Follow) scopeParam += " follow";
if (scopes == null)
{
return "";
}

return scopeParam.Trim();
return String.Join(" ", scopes.Select(s => s.ToString().ToLowerInvariant().Replace("__", ":")));
}

#endregion

protected override void OnResponseReceived(HttpResponseMessage response)
{
}

}
2 changes: 1 addition & 1 deletion Mastonet/Entities/AppRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public class AppRegistration
public string Instance { get; set; } = string.Empty;

[JsonProperty("scope")]
public Scope Scope { get; set; }
public string Scope { get; set; } = string.Empty;
}
55 changes: 54 additions & 1 deletion Mastonet/Enums/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,62 @@
namespace Mastonet;

[Flags]
[Obsolete("Use Granular Scopes")]
public enum Scope
{
Read = 1,
Write = 2,
Follow = 4,
Follow = 4
}

public enum GranularScope
{
Read,
Read__Accounts,
Read__Blocks,
Read__Bookmarks,
Read__Favourites,
Read__Filters,
Read__Follows,
Read__Lists,
Read__Mutes,
Read__Notifications,
Read__Search,
Read__Statuses,

Write,
Write__Accounts,
Write__Blocks,
Write__Bookmarks,
Write__Conversations,
Write__Favourites,
Write__Filters,
Write__Follows,
Write__Lists,
Write__Media,
Write__Mutes,
Write__Notifications,
Write__Reports,
Write__Statuses,

Push,

Admin__Read,
Admin__Read__Accounts,
Admin__Read__Reports,
Admin__Read__Domain_Allows,
Admin__Read__Domain_Blocks,
Admin__Read__Ip_Blocks,
Admin__Read__Email_Domain_Blocks,
Admin__Read__Canonical_Email_Blocks,

Admin__Write,
Admin__Write__Accounts,
Admin__Write__Reports,
Admin__Write__Domain_Allows,
Admin__Write__Domain_Blocks,
Admin__Write__Ip_Blocks,
Admin__Write__Email_Domain_Blocks,
Admin__Write__Canonical_Email_Blocks,

}
4 changes: 4 additions & 0 deletions Mastonet/IAuthenticationClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ public interface IAuthenticationClient
/// <param name="scope">The rights needed by your application</param>
/// <param name="website">URL to the homepage of your app</param>
/// <returns></returns>
[Obsolete("Use GranularScopes instead of deprecated Scope")]
Task<AppRegistration> CreateApp(string appName, Scope scope, string? website = null, string? redirectUri = null);

Task<AppRegistration> CreateApp(string appName, string? website = null, string? redirectUri = null, params GranularScope[] scope);
Task<AppRegistration> CreateApp(string appName, string? website = null, string? redirectUri = null, IEnumerable<GranularScope>? scope = null);

Task<Auth> ConnectWithPassword(string email, string password);

Task<Auth> ConnectWithCode(string code, string? redirect_uri = null);
Expand Down

0 comments on commit dd684b7

Please sign in to comment.