-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from ChangemakerStudios/feature/Modernizing
Merging this monkey... wish me luck.
- Loading branch information
Showing
140 changed files
with
4,885 additions
and
3,937 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...Docker.Registry.DotNet/Application/Authentication/AnonymousOAuthAuthenticationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2017-2022 Rich Quackenbush, Jaben Cargman | ||
// and Docker.Registry.DotNet Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Docker.Registry.DotNet.Application.OAuth; | ||
|
||
namespace Docker.Registry.DotNet.Application.Authentication; | ||
|
||
[PublicAPI] | ||
public class AnonymousOAuthAuthenticationProvider : AuthenticationProvider | ||
{ | ||
private readonly OAuthClient _client = new(); | ||
|
||
private static string Schema { get; } = "Bearer"; | ||
|
||
public override Task Authenticate(HttpRequestMessage request, IRegistryUriBuilder uriBuilder) | ||
{ | ||
using var activity = Assembly.Source.StartActivity("AnonymousOAuthAuthenticationProvider.Authenticate(request)"); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public override async Task Authenticate( | ||
HttpRequestMessage request, | ||
HttpResponseMessage response, | ||
IRegistryUriBuilder uriBuilder) | ||
{ | ||
using var activity = Assembly.Source.StartActivity("AnonymousOAuthAuthenticationProvider.Authenticate(request, response)"); | ||
|
||
var header = this.TryGetSchemaHeader(response, Schema); | ||
|
||
//Get the bearer bits | ||
var bearerBits = AuthenticateParser.ParseTyped(header.Parameter); | ||
|
||
//Get the token | ||
var token = await this._client.GetToken( | ||
bearerBits.Realm, | ||
bearerBits.Service, | ||
bearerBits.Scope); | ||
|
||
//Set the header | ||
request.Headers.Authorization = new AuthenticationHeaderValue(Schema, token.Token); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
src/Docker.Registry.DotNet/Application/Authentication/AuthenticateParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2017-2022 Rich Quackenbush, Jaben Cargman | ||
// and Docker.Registry.DotNet Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace Docker.Registry.DotNet.Application.Authentication; | ||
|
||
internal static class AuthenticateParser | ||
{ | ||
public static IDictionary<string, string> Parse(string value) | ||
{ | ||
//https://stackoverflow.com/questions/45516717/extracting-and-parsing-the-www-authenticate-header-from-httpresponsemessage-in/45516809#45516809 | ||
return SplitWWWAuthenticateHeader(value).ToDictionary(GetKey, GetValue); | ||
} | ||
|
||
private static IEnumerable<string> SplitWWWAuthenticateHeader(string value) | ||
{ | ||
var builder = new StringBuilder(); | ||
var inQuotes = false; | ||
for (var i = 0; i < value.Length; i++) | ||
{ | ||
var charI = value[i]; | ||
switch (charI) | ||
{ | ||
case '\"': | ||
if (inQuotes) | ||
{ | ||
yield return builder.ToString(); | ||
builder.Clear(); | ||
inQuotes = false; | ||
} | ||
else | ||
{ | ||
inQuotes = true; | ||
} | ||
|
||
break; | ||
|
||
case ',': | ||
if (inQuotes) | ||
{ | ||
builder.Append(charI); | ||
} | ||
else | ||
{ | ||
if (builder.Length > 0) | ||
{ | ||
yield return builder.ToString(); | ||
builder.Clear(); | ||
} | ||
} | ||
|
||
break; | ||
|
||
default: | ||
builder.Append(charI); | ||
break; | ||
} | ||
} | ||
|
||
if (builder.Length > 0) yield return builder.ToString(); | ||
} | ||
|
||
public static ParsedAuthentication ParseTyped(string value) | ||
{ | ||
var parsed = Parse(value); | ||
|
||
return new ParsedAuthentication( | ||
parsed.GetValueOrDefault("realm"), | ||
parsed.GetValueOrDefault("service"), | ||
parsed.GetValueOrDefault("scope")); | ||
} | ||
|
||
private static string GetKey(string pair) | ||
{ | ||
var equalPos = pair.IndexOf("=", StringComparison.Ordinal); | ||
|
||
if (equalPos < 1) | ||
throw new FormatException("No '=' found."); | ||
|
||
return pair.Substring(0, equalPos); | ||
} | ||
|
||
private static string GetValue(string pair) | ||
{ | ||
var equalPos = pair.IndexOf("=", StringComparison.Ordinal); | ||
|
||
if (equalPos < 1) | ||
throw new FormatException("No '=' found."); | ||
|
||
var value = pair.Substring(equalPos + 1).Trim(); | ||
|
||
//Trim quotes | ||
if (value.StartsWith("\"") && value.EndsWith("\"")) | ||
value = value.Substring(1, value.Length - 2); | ||
|
||
return value; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/Docker.Registry.DotNet/Application/Authentication/AuthenticationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2017-2022 Rich Quackenbush, Jaben Cargman | ||
// and Docker.Registry.DotNet Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace Docker.Registry.DotNet.Application.Authentication; | ||
|
||
/// <summary> | ||
/// Authentication provider. | ||
/// </summary> | ||
public abstract class AuthenticationProvider | ||
{ | ||
/// <summary> | ||
/// Called on initial connection | ||
/// </summary> | ||
/// <param name="request"></param> | ||
/// <param name="uriBuilder"></param> | ||
/// <returns></returns> | ||
public abstract Task Authenticate(HttpRequestMessage request, IRegistryUriBuilder uriBuilder); | ||
|
||
/// <summary> | ||
/// Called when connection is challenged. | ||
/// </summary> | ||
/// <param name="request"></param> | ||
/// <param name="response"></param> | ||
/// <param name="builder"></param> | ||
/// <returns></returns> | ||
public abstract Task Authenticate( | ||
HttpRequestMessage request, | ||
HttpResponseMessage response, | ||
IRegistryUriBuilder builder); | ||
|
||
/// <summary> | ||
/// Gets the schema header from the http response. | ||
/// </summary> | ||
/// <param name="response"></param> | ||
/// <param name="schema"></param> | ||
/// <returns></returns> | ||
protected AuthenticationHeaderValue TryGetSchemaHeader( | ||
HttpResponseMessage response, | ||
string schema) | ||
{ | ||
var header = response.GetHeaderBySchema(schema); | ||
|
||
if (header == null) | ||
throw new InvalidOperationException( | ||
$"No WWW-Authenticate challenge was found for schema {schema}"); | ||
|
||
return header; | ||
} | ||
} |
Oops, something went wrong.