Skip to content

Commit

Permalink
update oauthflowcollection to properly validate required urls
Browse files Browse the repository at this point in the history
  • Loading branch information
gregsdennis committed Nov 7, 2024
1 parent 9e2e34b commit 749edbb
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 21 deletions.
13 changes: 13 additions & 0 deletions .idea/.idea.Graeae/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Graeae.AspNet.Analyzer/Graeae.AspNet.Analyzer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<PackageReleaseNotes>Release notes can be found at https://github.com/gregsdennis/Graeae</PackageReleaseNotes>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<DocumentationFile>Graeae.AspNet.xml</DocumentationFile>
<Version>0.1.0-preview2</Version>
<Version>0.1.0-preview3</Version>
<FileVersion>0.1.0</FileVersion>
<AssemblyVersion>0.1.0.0</AssemblyVersion>
<IncludeSymbols>false</IncludeSymbols>
Expand Down
4 changes: 2 additions & 2 deletions Graeae.AspNet/Graeae.AspNet.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>Graeae.AspNet</PackageId>
Expand All @@ -17,7 +17,7 @@
<PackageReleaseNotes>Release notes can be found at https://github.com/gregsdennis/Graeae</PackageReleaseNotes>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<DocumentationFile>Graeae.AspNet.xml</DocumentationFile>
<Version>0.1.0-preview1</Version>
<Version>0.1.0-preview2</Version>
<FileVersion>0.1.0</FileVersion>
<AssemblyVersion>0.1.0.0</AssemblyVersion>
<IncludeSymbols>true</IncludeSymbols>
Expand Down
6 changes: 3 additions & 3 deletions Graeae.Models/Graeae.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/../RELEASE_NOTES.md"))</PackageReleaseNotes>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<DocumentationFile>Graeae.Models.xml</DocumentationFile>
<Version>0.3.4</Version>
<FileVersion>0.3.4</FileVersion>
<AssemblyVersion>0.3.4.0</AssemblyVersion>
<Version>0.3.5</Version>
<FileVersion>0.3.5</FileVersion>
<AssemblyVersion>0.3.5.0</AssemblyVersion>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand Down
24 changes: 9 additions & 15 deletions Graeae.Models/OAuthFlow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public class OAuthFlow : IRefTargetContainer
/// <summary>
/// Gets the authorization URL.
/// </summary>
public Uri AuthorizationUrl { get; }
public Uri? AuthorizationUrl { get; set; }
/// <summary>
/// Gets the token URL.
/// </summary>
public Uri TokenUrl { get; }
public Uri? TokenUrl { get; set; }
/// <summary>
/// Gets or sets the refresh token URL.
/// </summary>
Expand All @@ -42,13 +42,9 @@ public class OAuthFlow : IRefTargetContainer
/// <summary>
/// Creates a new <see cref="OAuthFlow"/>
/// </summary>
/// <param name="authorizationUrl">The authorization URL</param>
/// <param name="tokenUrl">The token URL</param>
/// <param name="scopes">The scopes</param>
public OAuthFlow(Uri authorizationUrl, Uri tokenUrl, Dictionary<string, string> scopes)
public OAuthFlow(Dictionary<string, string> scopes)
{
AuthorizationUrl = authorizationUrl;
TokenUrl = tokenUrl;
Scopes = scopes;
}

Expand All @@ -58,10 +54,10 @@ internal static OAuthFlow FromNode(JsonNode? node)
throw new JsonException("Expected an object");

var flow = new OAuthFlow(
obj.ExpectUri("authorizationUrl", "oauth flow"),
obj.ExpectUri("tokenUrl", "oauth flow"),
obj.ExpectMap("scopes", "oauth flow", x => x is JsonValue v && v.TryGetValue(out string? s) ? s : throw new JsonException("scopes must be strings")))
{
AuthorizationUrl = obj.MaybeUri("authorizationUrl", "oauth flow"),
TokenUrl = obj.MaybeUri("tokenUrl", "oauth flow"),
RefreshUrl = obj.MaybeUri("refreshUrl", "oauth flow"),
ExtensionData = ExtensionData.FromNode(obj)
};
Expand All @@ -75,11 +71,10 @@ internal static OAuthFlow FromNode(JsonNode? node)
{
if (flow == null) return null;

var obj = new JsonObject
{
["authorizationUrl"] = flow.AuthorizationUrl.ToString(),
["tokenUrl"] = flow.TokenUrl.ToString()
};
var obj = new JsonObject();
obj.MaybeAdd("authorizationUrl", flow.AuthorizationUrl?.ToString());
obj.MaybeAdd("tokenUrl", flow.TokenUrl?.ToString());
obj.MaybeAdd("refreshUrl", flow.RefreshUrl?.ToString());

var scopes = new JsonObject();
foreach (var kvp in flow.Scopes)
Expand All @@ -88,7 +83,6 @@ internal static OAuthFlow FromNode(JsonNode? node)
}
obj.Add("scopes", scopes);

obj.MaybeAdd("refreshUrl", flow.RefreshUrl?.ToString());
obj.AddExtensions(flow.ExtensionData);

return obj;
Expand Down
12 changes: 12 additions & 0 deletions Graeae.Models/OAuthFlowCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ internal static OAuthFlowCollection FromNode(JsonNode? node)
AuthorizationCode = obj.Maybe("authorizationCode", OAuthFlow.FromNode),
ExtensionData = ExtensionData.FromNode(obj)
};

if (flows.Implicit is not null && flows.Implicit.AuthorizationUrl is null)
throw new JsonException($"`authorizationUrl` is required for implicit oauth flow object");
if (flows.Password is not null && flows.Password.TokenUrl is null)
throw new JsonException($"`tokenUrl` is required for password oauth flow object");
if (flows.ClientCredentials is not null && flows.ClientCredentials.TokenUrl is null)
throw new JsonException($"`tokenUrl` is required for clientCredentials oauth flow object");
if (flows.AuthorizationCode is not null)
{
if (flows.AuthorizationCode.AuthorizationUrl is null) throw new JsonException($"`authorizationUrl` is required for authorizationCode oauth flow object");
if (flows.AuthorizationCode.TokenUrl is null) throw new JsonException($"`tokenUrl` is required for authorizationCode oauth flow object");
}

obj.ValidateNoExtraKeys(KnownKeys, flows.ExtensionData?.Keys);

Expand Down
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


[0.3.4](https://github.com/gregsdennis/Graeae/commit/f3f6bb0c4ec29879f8fb24573900f51f61e0bfae) (v0.3.3 redacted)

- Added .Net Standard 2.0 support.
Expand Down

0 comments on commit 749edbb

Please sign in to comment.