Skip to content

Commit b884044

Browse files
authored
[dotnet] Remove Microsoft.IdentityModel.Tokens as dependency (#12777)
* Remove Microsoft.IdentityModel.Tokens as dependency * Make it internal * Remove unused methods * Add standard copyright file notice * Add links to original source as comments
1 parent bf4b286 commit b884044

9 files changed

+155
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// <copyright file="Base64UrlEncoder.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System;
20+
21+
namespace OpenQA.Selenium.Internal
22+
{
23+
/// <summary>
24+
/// Encodes and Decodes strings as Base64Url encoding.
25+
/// </summary>
26+
internal static class Base64UrlEncoder
27+
{
28+
private const char base64PadCharacter = '=';
29+
private const string doubleBase64PadCharacter = "==";
30+
private const char base64Character62 = '+';
31+
private const char base64Character63 = '/';
32+
private const char base64UrlCharacter62 = '-';
33+
private const char base64UrlCharacter63 = '_';
34+
35+
/// <summary>
36+
/// Encoding table
37+
/// </summary>
38+
internal static readonly char[] s_base64Table =
39+
{
40+
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
41+
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
42+
'0','1','2','3','4','5','6','7','8','9',
43+
base64UrlCharacter62,
44+
base64UrlCharacter63
45+
};
46+
47+
// https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/6.19.0/src/Microsoft.IdentityModel.Tokens/Base64UrlEncoder.cs#L85
48+
49+
/// <summary>
50+
/// Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation which is encoded with base-64-url digits.
51+
/// </summary>
52+
/// <param name="inArray">An array of 8-bit unsigned integers.</param>
53+
/// <returns>The string representation in base 64 url encoding of length elements of inArray, starting at position offset.</returns>
54+
/// <exception cref="ArgumentNullException">'inArray' is null.</exception>
55+
public static string Encode(byte[] inArray)
56+
{
57+
_ = inArray ?? throw new ArgumentNullException(nameof(inArray));
58+
59+
if (inArray.Length == 0)
60+
return string.Empty;
61+
62+
var length = inArray.Length;
63+
64+
int lengthmod3 = length % 3;
65+
int limit = length - lengthmod3;
66+
char[] output = new char[(length + 2) / 3 * 4];
67+
char[] table = s_base64Table;
68+
int i, j = 0;
69+
70+
// takes 3 bytes from inArray and insert 4 bytes into output
71+
for (i = 0; i < limit; i += 3)
72+
{
73+
byte d0 = inArray[i];
74+
byte d1 = inArray[i + 1];
75+
byte d2 = inArray[i + 2];
76+
77+
output[j + 0] = table[d0 >> 2];
78+
output[j + 1] = table[((d0 & 0x03) << 4) | (d1 >> 4)];
79+
output[j + 2] = table[((d1 & 0x0f) << 2) | (d2 >> 6)];
80+
output[j + 3] = table[d2 & 0x3f];
81+
j += 4;
82+
}
83+
84+
//Where we left off before
85+
i = limit;
86+
87+
switch (lengthmod3)
88+
{
89+
case 2:
90+
{
91+
byte d0 = inArray[i];
92+
byte d1 = inArray[i + 1];
93+
94+
output[j + 0] = table[d0 >> 2];
95+
output[j + 1] = table[((d0 & 0x03) << 4) | (d1 >> 4)];
96+
output[j + 2] = table[(d1 & 0x0f) << 2];
97+
j += 3;
98+
}
99+
break;
100+
101+
case 1:
102+
{
103+
byte d0 = inArray[i];
104+
105+
output[j + 0] = table[d0 >> 2];
106+
output[j + 1] = table[(d0 & 0x03) << 4];
107+
j += 2;
108+
}
109+
break;
110+
111+
//default or case 0: no further operations are needed.
112+
}
113+
114+
return new string(output, 0, j);
115+
}
116+
117+
// https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/6.19.0/src/Microsoft.IdentityModel.Tokens/Base64UrlEncoder.cs#L179
118+
119+
/// <summary>
120+
/// Converts the specified string, which encodes binary data as base-64-url digits, to an equivalent 8-bit unsigned integer array.</summary>
121+
/// <param name="str">base64Url encoded string.</param>
122+
/// <returns>UTF8 bytes.</returns>
123+
public static byte[] DecodeBytes(string str)
124+
{
125+
_ = str ?? throw new ArgumentNullException(nameof(str));
126+
127+
// 62nd char of encoding
128+
str = str.Replace(base64UrlCharacter62, base64Character62);
129+
130+
// 63rd char of encoding
131+
str = str.Replace(base64UrlCharacter63, base64Character63);
132+
133+
// check for padding
134+
switch (str.Length % 4)
135+
{
136+
case 0:
137+
// No pad chars in this case
138+
break;
139+
case 2:
140+
// Two pad chars
141+
str += doubleBase64PadCharacter;
142+
break;
143+
case 3:
144+
// One pad char
145+
str += base64PadCharacter;
146+
break;
147+
default:
148+
throw new FormatException(str);
149+
}
150+
151+
return Convert.FromBase64String(str);
152+
}
153+
}
154+
}

dotnet/src/webdriver/VirtualAuth/Credential.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
// limitations under the License.
1717
// </copyright>
1818

19+
using OpenQA.Selenium.Internal;
1920
using System.Collections.Generic;
20-
using Microsoft.IdentityModel.Tokens;
2121

2222
namespace OpenQA.Selenium.VirtualAuth
2323
{

dotnet/src/webdriver/WebDriver.StrongNamed.nuspec

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
<tags>selenium webdriver browser automation</tags>
2828
<dependencies>
2929
<group targetFramework="netstandard2.0">
30-
<dependency id="Microsoft.IdentityModel.Tokens" version="6.19.0" exclude="Build,Analyzers" />
3130
<dependency id="Newtonsoft.Json" version="13.0.1" exclude="Build,Analyzers" />
3231
<dependency id="System.Drawing.Common" version="7.0.0" exclude="Build,Analyzers" />
3332
</group>

dotnet/src/webdriver/WebDriver.cs

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
using OpenQA.Selenium.Interactions;
2525
using OpenQA.Selenium.Internal;
2626
using OpenQA.Selenium.VirtualAuth;
27-
using Microsoft.IdentityModel.Tokens;
2827

2928
namespace OpenQA.Selenium
3029
{

dotnet/src/webdriver/WebDriver.csproj

-5
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,7 @@
6060
</ItemGroup>
6161

6262
<ItemGroup>
63-
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.19.0" />
6463
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
65-
</ItemGroup>
66-
67-
<ItemGroup>
68-
<!--PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.19.0" PrivateAssets="All" /-->
6964
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
7065
</ItemGroup>
7166

dotnet/src/webdriver/WebDriver.nuspec

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
<tags>selenium webdriver browser automation</tags>
2828
<dependencies>
2929
<group targetFramework="netstandard2.0">
30-
<dependency id="Microsoft.IdentityModel.Tokens" version="6.19.0" exclude="Build,Analyzers" />
3130
<dependency id="Newtonsoft.Json" version="13.0.1" exclude="Build,Analyzers" />
3231
<dependency id="System.Drawing.Common" version="7.0.0" exclude="Build,Analyzers" />
3332
</group>
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)