-
Notifications
You must be signed in to change notification settings - Fork 19
/
AuthenticateParser.cs
117 lines (99 loc) · 3.85 KB
/
AuthenticateParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Docker.Registry.DotNet.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)
{
int equalPos = pair.IndexOf("=", StringComparison.Ordinal);
if (equalPos < 1)
throw new FormatException("No '=' found.");
return pair.Substring(0, equalPos);
}
private static string GetValue(string pair)
{
int equalPos = pair.IndexOf("=", StringComparison.Ordinal);
if (equalPos < 1)
throw new FormatException("No '=' found.");
string value = pair.Substring(equalPos + 1).Trim();
//Trim quotes
if (value.StartsWith("\"") && value.EndsWith("\""))
{
value = value.Substring(1, value.Length - 2);
}
return value;
}
}
}