Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update oauthflowcollection to properly validate required urls #14

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,4 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml
*.xml

.idea/
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
6 changes: 5 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
[0.3.5](https://github.com/gregsdennis/Graeae/pull/14)

- Fixed an [issue](https://github.com/gregsdennis/Graeae/issues/13) where OAuth objects incorrectly required various URLs.

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

- Added .Net Standard 2.0 support.

[0.3.2](https://github.com/gregsdennis/Graeae/pull/11)

- Fixed a bug that Draft4Support.Enable() crashed when called multiple times.
- Fixed a bug that `Draft4Support.Enable()` crashed when called multiple times.

[0.3.1](https://github.com/gregsdennis/Graeae/pull/10)

Expand Down
Loading