refactor(identity): change receiver of OauthUtils.ExpandScopes()

This commit is contained in:
sam 2024-05-22 17:19:45 +02:00
parent 00a54f4f8b
commit 8bd118ea67
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
4 changed files with 13 additions and 14 deletions

View file

@ -25,7 +25,7 @@ public class PasswordAuthController(ILogger logger, IdentityContext db, IClock c
var appToken = var appToken =
HttpContext.GetToken() ?? HttpContext.GetToken() ??
throw new UnreachableException(); // GetApplicationOrThrow already gets the token and throws if it's null throw new UnreachableException(); // GetApplicationOrThrow already gets the token and throws if it's null
var appScopes = appToken.ExpandScopes(); var appScopes = appToken.Scopes.ExpandScopes();
if (req.Scopes.Except(appScopes).Any()) if (req.Scopes.Except(appScopes).Any())
throw new ApiError.Forbidden("Cannot request token scopes that are not allowed for this token", throw new ApiError.Forbidden("Cannot request token scopes that are not allowed for this token",
@ -54,7 +54,7 @@ public class PasswordAuthController(ILogger logger, IdentityContext db, IClock c
{ {
var app = HttpContext.GetApplicationOrThrow(); var app = HttpContext.GetApplicationOrThrow();
var appToken = HttpContext.GetToken() ?? throw new UnreachableException(); var appToken = HttpContext.GetToken() ?? throw new UnreachableException();
var appScopes = appToken.ExpandScopes(); var appScopes = appToken.Scopes.ExpandScopes();
if (req.Scopes.Except(appScopes).Any()) if (req.Scopes.Except(appScopes).Any())
throw new ApiError.Forbidden("Cannot request token scopes that are not allowed for this token", throw new ApiError.Forbidden("Cannot request token scopes that are not allowed for this token",

View file

@ -15,7 +15,7 @@ public class TokenController(ILogger logger, IdentityContext db, IClock clock) :
public async Task<IActionResult> PostToken([FromBody] PostTokenRequest req) public async Task<IActionResult> PostToken([FromBody] PostTokenRequest req)
{ {
var app = await db.GetApplicationAsync(req.ClientId, req.ClientSecret); var app = await db.GetApplicationAsync(req.ClientId, req.ClientSecret);
var appScopes = app.ExpandScopes(); var appScopes = app.Scopes.ExpandScopes();
var scopes = req.Scope.Split(' '); var scopes = req.Scope.Split(' ');
if (scopes.Except(appScopes).Any()) if (scopes.Except(appScopes).Any())
@ -25,9 +25,9 @@ public class TokenController(ILogger logger, IdentityContext db, IClock clock) :
switch (req.GrantType) switch (req.GrantType)
{ {
case "client_credentials": case OauthUtils.ClientCredentials:
return await HandleClientCredentialsAsync(app, scopes); return await HandleClientCredentialsAsync(app, scopes);
case "authorization_code": case OauthUtils.AuthorizationCode:
// TODO // TODO
break; break;
default: default:

View file

@ -24,8 +24,8 @@ public class ClientAuthorizationMiddleware(
var token = ctx.GetToken(); var token = ctx.GetToken();
if (token == null || token.Expires < clock.GetCurrentInstant()) if (token == null || token.Expires < clock.GetCurrentInstant())
throw new ApiError.Unauthorized("This endpoint requires an authenticated user."); throw new ApiError.Unauthorized("This endpoint requires an authenticated user.");
if (attribute.Scopes.Length > 0 && attribute.Scopes.Except(token.ExpandScopes()).Any()) if (attribute.Scopes.Length > 0 && attribute.Scopes.Except(token.Scopes.ExpandScopes()).Any())
throw new ApiError.Forbidden("This endpoint requires ungranted scopes.", attribute.Scopes.Except(token.ExpandScopes())); throw new ApiError.Forbidden("This endpoint requires ungranted scopes.", attribute.Scopes.Except(token.Scopes.ExpandScopes()));
await next(ctx); await next(ctx);
} }

View file

@ -6,7 +6,10 @@ namespace Foxchat.Identity.Utils;
public static class OauthUtils public static class OauthUtils
{ {
public static readonly string[] Scopes = ["identify", "chat_client"]; public const string ClientCredentials = "client_credentials";
public const string AuthorizationCode = "authorization_code";
public static readonly string[] Scopes = ["identify", "email", "guilds", "chat_client"];
private static readonly string[] ForbiddenSchemes = ["javascript", "file", "data", "mailto", "tel"]; private static readonly string[] ForbiddenSchemes = ["javascript", "file", "data", "mailto", "tel"];
private const string OobUri = "urn:ietf:wg:oauth:2.0:oob"; private const string OobUri = "urn:ietf:wg:oauth:2.0:oob";
@ -25,11 +28,7 @@ public static class OauthUtils
} }
} }
public static string[] ExpandScopes(this Token token) => token.Scopes.Contains("chat_client") public static string[] ExpandScopes(this string[] scopes) => scopes.Contains("chat_client")
? Scopes ? Scopes
: token.Scopes; : scopes;
public static string[] ExpandScopes(this Application app) => app.Scopes.Contains("chat_client")
? Scopes
: app.Scopes;
} }