Skip to content

Commit 55124bc

Browse files
committed
Dynamically fetch devlooped JWK from github
This avoids hardcoding the key, ensuring it will match whatever we have on the backend at build time. Minor improvements to ILRepack invocation (ignore warning on duplicate resource).
1 parent 8d29f01 commit 55124bc

8 files changed

Lines changed: 54 additions & 25 deletions

File tree

src/SponsorLink/Library/Library.csproj

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<PackageId>SponsorableLib</PackageId>
77
<Description>Sample library incorporating SponsorLink checks</Description>
88
<PackOnBuild>true</PackOnBuild>
9+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
910
</PropertyGroup>
1011

1112
<ItemGroup>
@@ -16,16 +17,4 @@
1617
<ProjectReference Include="..\Analyzer\Analyzer.csproj" ReferenceOutputAssembly="false" OutputType="Analyzer" />
1718
</ItemGroup>
1819

19-
<ItemGroup>
20-
<EmbeddedResource Update="Resources.resx">
21-
<!-- Default to Just Works resources generation. See https://www.cazzulino.com/resources.html -->
22-
<Generator>MSBuild:Compile</Generator>
23-
<StronglyTypedFileName>$(IntermediateOutputPath)\$([MSBuild]::ValueOrDefault('%(RelativeDir)', '').Replace('\', '.').Replace('/', '.'))%(Filename).g$(DefaultLanguageSourceExtension)</StronglyTypedFileName>
24-
<StronglyTypedLanguage>$(Language)</StronglyTypedLanguage>
25-
<StronglyTypedNamespace Condition="'%(RelativeDir)' == ''">$(RootNamespace)</StronglyTypedNamespace>
26-
<StronglyTypedNamespace Condition="'%(RelativeDir)' != ''">$(RootNamespace).$([MSBuild]::ValueOrDefault('%(RelativeDir)', '').Replace('\', '.').Replace('/', '.').TrimEnd('.'))</StronglyTypedNamespace>
27-
<StronglyTypedClassName>%(Filename)</StronglyTypedClassName>
28-
</EmbeddedResource>
29-
</ItemGroup>
30-
3120
</Project>

src/SponsorLink/Library/readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Sponsorable Library
2+
3+
Example of a library that is available for sponsorship and leverages
4+
[SponsorLink](https://github.com/devlooped/SponsorLink) to remind users
5+
in an IDE (VS/Rider).

src/SponsorLink/SponsorLink.targets

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
<!-- If we are going to merge files, we need to copy local -->
1111
<CopyLocalLockFileAssemblies Condition="'$(MergeAnalyzerAssemblies)' == 'true'">true</CopyLocalLockFileAssemblies>
1212

13-
<!-- Read public key we validate manifests against -->
14-
<DevloopedJwk>$([System.IO.File]::ReadAllText('$(MSBuildThisFileDirectory)SponsorLink/devlooped.pub.jwk'))</DevloopedJwk>
15-
1613
<!-- Default funding product the Product, which already part of ThisAssembly -->
1714
<FundingProduct Condition="'$(FundingProduct)' == ''">$(Product)</FundingProduct>
1815
<!-- Default prefix is the joined upper-case letters in the product name (i.e. for ThisAssembly, TA) -->
@@ -22,7 +19,6 @@
2219
</PropertyGroup>
2320

2421
<ItemGroup>
25-
<AssemblyMetadata Include="Funding.GitHub.devlooped" Value="$(DevloopedJwk)" />
2622
<Constant Include="Funding.Product" Value="$(FundingProduct)" />
2723
<Constant Include="Funding.Prefix" Value="$(FundingPrefix)" />
2824
<Constant Include="Funding.Grace" Value="$(FundingGrace)" />
@@ -123,7 +119,7 @@
123119
<!--<ILRepackArgs>$(ILRepackArgs) "/lib:$(NetstandardDirectory)"</ILRepackArgs> -->
124120
<!-- This is needed for ilrepack to find netstandard.dll, which is referenced by the System.Text.Json assembly -->
125121
</PropertyGroup>
126-
<Exec Command="&quot;$(ILRepack)&quot; $(ILRepackArgs)" WorkingDirectory="$(MSBuildProjectDirectory)\$(OutputPath)" StandardErrorImportance="high" StandardOutputImportance="low" ConsoleToMSBuild="true" ContinueOnError="true">
122+
<Exec Command='"$(ILRepack)" $(ILRepackArgs)' WorkingDirectory="$(MSBuildProjectDirectory)\$(OutputPath)" StandardErrorImportance="high" IgnoreStandardErrorWarningFormat="true" StandardOutputImportance="low" ConsoleToMSBuild="true" ContinueOnError="true">
127123
<Output TaskParameter="ConsoleOutput" PropertyName="ILRepackOutput" />
128124
<Output TaskParameter="ExitCode" PropertyName="ExitCode" />
129125
</Exec>
@@ -138,4 +134,23 @@
138134
<Delete Files="@(MergedAssembliesToRemove -&gt; '$(MSBuildProjectDirectory)\$(OutputPath)%(Filename)%(Extension)')" Condition="Exists('$(MSBuildProjectDirectory)\$(OutputPath)%(Filename)%(Extension)')" />
139135
</Target>
140136

137+
<Target Name="DownloadDevloopedJwk" BeforeTargets="GetAssemblyAttributes" Inputs="$(MSBuildProjectFullPath)" Outputs="$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk">
138+
<Exec Command='pwsh -nop -f "$(MSBuildThisFileDirectory)jwk.ps1"' ConsoleToMSBuild="true" EchoOff="true">
139+
<Output TaskParameter="ConsoleOutput" PropertyName="RawJwk"/>
140+
<Output TaskParameter="ExitCode" PropertyName="MSBuildLastExitCode" />
141+
</Exec>
142+
<Error Text="$(RawJwk)" Condition="'$(MSBuildLastExitCode)' != '0'" />
143+
<WriteLinesToFile File="$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk" Lines="$(RawJwk)" Overwrite="true" />
144+
</Target>
145+
146+
<Target Name="ReadDevloopedJwk" DependsOnTargets="DownloadDevloopedJwk" BeforeTargets="GetAssemblyAttributes">
147+
<PropertyGroup>
148+
<!-- Read public key we validate manifests against -->
149+
<DevloopedJwk>$([System.IO.File]::ReadAllText('$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk'))</DevloopedJwk>
150+
</PropertyGroup>
151+
<ItemGroup>
152+
<AssemblyMetadata Include="Funding.GitHub.devlooped" Value="$(DevloopedJwk)" />
153+
</ItemGroup>
154+
</Target>
155+
141156
</Project>

src/SponsorLink/SponsorLink/SponsorLink.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static bool TryRead([NotNullWhen(true)] out ClaimsPrincipal? principal, I
8282

8383
foreach (var value in values)
8484
{
85-
if (string.IsNullOrWhiteSpace(value.jwk) || string.IsNullOrEmpty(value.jwk))
85+
if (string.IsNullOrWhiteSpace(value.jwt) || string.IsNullOrEmpty(value.jwk))
8686
continue;
8787

8888
if (Validate(value.jwt, value.jwk, out var token, out var claims, false) == ManifestStatus.Valid && claims != null)

src/SponsorLink/SponsorLink/SponsorLink.csproj

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
</PropertyGroup>
99

1010
<PropertyGroup Label="SponsorLink">
11-
<DevloopedJwk>$([System.IO.File]::ReadAllText('$(MSBuildThisFileDirectory)devlooped.pub.jwk'))</DevloopedJwk>
1211
<!-- Default funding product the Product, which already part of ThisAssembly -->
1312
<FundingProduct Condition="'$(FundingProduct)' == ''">$(Product)</FundingProduct>
1413
<!-- Default prefix is the joined upper-case letters in the product name (i.e. for ThisAssembly, TA) -->
@@ -37,10 +36,32 @@
3736
</ItemGroup>
3837

3938
<ItemGroup>
40-
<AssemblyMetadata Include="Funding.GitHub.devlooped" Value="$(DevloopedJwk)" />
4139
<Constant Include="Funding.Product" Value="$(FundingProduct)" />
4240
<Constant Include="Funding.Prefix" Value="$(FundingPrefix)" />
4341
<Constant Include="Funding.Grace" Value="$(FundingGrace)" />
4442
</ItemGroup>
4543

44+
<ItemGroup>
45+
<None Include="..\SponsorLink.targets" Link="SponsorLink.targets" />
46+
</ItemGroup>
47+
48+
<Target Name="DownloadDevloopedJwk" BeforeTargets="GetAssemblyAttributes" Inputs="$(MSBuildProjectFullPath)" Outputs="$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk">
49+
<Exec Command='pwsh -nop -f "$(MSBuildThisFileDirectory)jwk.ps1"' ConsoleToMSBuild="true" EchoOff="true">
50+
<Output TaskParameter="ConsoleOutput" PropertyName="DevloopedJwk"/>
51+
<Output TaskParameter="ExitCode" PropertyName="MSBuildLastExitCode" />
52+
</Exec>
53+
<Error Text="$(DevloopedJwk)" Condition="'$(MSBuildLastExitCode)' != '0'" />
54+
<WriteLinesToFile File="$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk" Lines="$(DevloopedJwk)" Overwrite="true" />
55+
</Target>
56+
57+
<Target Name="ReadDevloopedJwk" DependsOnTargets="DownloadDevloopedJwk" BeforeTargets="GetAssemblyAttributes">
58+
<PropertyGroup>
59+
<!-- Read public key we validate manifests against -->
60+
<DevloopedJwk>$([System.IO.File]::ReadAllText('$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk'))</DevloopedJwk>
61+
</PropertyGroup>
62+
<ItemGroup>
63+
<AssemblyMetadata Include="Funding.GitHub.devlooped" Value="$(DevloopedJwk)" />
64+
</ItemGroup>
65+
</Target>
66+
4667
</Project>

src/SponsorLink/SponsorLink/buildTransitive/Devlooped.Sponsors.targets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@
8686
Note that since running this requires autosync=true, we can safely assume the user
8787
has already run `sponsorlink [...] -autosync` at least once to turn it on. Otherwise,
8888
this target won't run at all.
89+
Note that since we don't specify -f (force), we only sync if the local manifest is expired,
90+
so as not to slow the build unnecessarily. Analyzer checking for the manifest will still
91+
check the validity of the manifest using the embedded key.
8992
-->
9093
<Exec Command="sponsor sync --local --unattended" StandardErrorImportance="high" StandardOutputImportance="low" ConsoleToMSBuild="true" ContinueOnError="true">
9194
<Output TaskParameter="ExitCode" PropertyName="SponsorsExitCode" />

src/SponsorLink/SponsorLink/devlooped.pub.jwk

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl https://raw.githubusercontent.com/devlooped/.github/main/sponsorlink.jwt --silent | jq -R 'split(".") | .[1] | @base64d | fromjson' | jq '.sub_jwk'

0 commit comments

Comments
 (0)