Skip to content

Commit 3ee2b81

Browse files
isra-felwyunchi-ms
authored andcommitted
Use Base64UrlHelper class in common lib ... (#14412)
... instead of duplicating in Az.Accounts and Az.KeyVault
1 parent b8324dd commit 3ee2b81

File tree

7 files changed

+21
-239
lines changed

7 files changed

+21
-239
lines changed

src/Accounts/Accounts/Token/GetAzureRmAccessToken.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
using Microsoft.Azure.Commands.Common.Authentication;
2222
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
2323
using Microsoft.Azure.Commands.Profile.Models;
24-
using Microsoft.Azure.Commands.Profile.Utilities;
2524
using Microsoft.Azure.Commands.ResourceManager.Common;
2625
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2726
using Microsoft.Azure.PowerShell.Authenticators;
27+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2828

2929
namespace Microsoft.Azure.Commands.Profile
3030
{
@@ -117,7 +117,7 @@ public override void ExecuteCmdlet()
117117
try
118118
{
119119
var tokenParts = accessToken.AccessToken.Split('.');
120-
var decodedToken = Base64UrlHelpers.DecodeToString(tokenParts[1]);
120+
var decodedToken = Base64UrlHelper.DecodeToString(tokenParts[1]);
121121

122122
var tokenDocument = JsonDocument.Parse(decodedToken);
123123
int expSeconds = tokenDocument.RootElement.EnumerateObject()

src/Accounts/Accounts/Utilities/Base64UrlHelpers.cs

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/KeyVault/KeyVault/SecurityDomain/Models/Base64UrlEncoder.cs

Lines changed: 0 additions & 125 deletions
This file was deleted.

src/KeyVault/KeyVault/SecurityDomain/Models/CertKeys.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
34

45
namespace Microsoft.Azure.Commands.KeyVault.SecurityDomain.Models
56
{
@@ -26,7 +27,7 @@ public void LoadKey(KeyPath path)
2627
{
2728
CertKey certKey = new CertKey();
2829
certKey.Load(path);
29-
string encodedThumbprint = Base64UrlEncoder.Encode(certKey.GetThumbprint());
30+
string encodedThumbprint = Base64UrlHelper.Encode(certKey.GetThumbprint());
3031
_keys.Add(encodedThumbprint, certKey);
3132
}
3233

src/KeyVault/KeyVault/SecurityDomain/Models/JWE.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Security.Cryptography.X509Certificates;
77
using Microsoft.Azure.Commands.KeyVault.SecurityDomain.Common;
8+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
89

910
namespace Microsoft.Azure.Commands.KeyVault.SecurityDomain.Models
1011
{
@@ -52,11 +53,11 @@ public JweDecode(string compact_jwe)
5253
}
5354

5455
encoded_header = parts[0];
55-
string header = Base64UrlEncoder.Decode(encoded_header);
56-
encrypted_key = Base64UrlEncoder.DecodeBytes(parts[1]);
57-
init_vector = Base64UrlEncoder.DecodeBytes(parts[2]);
58-
ciphertext = Base64UrlEncoder.DecodeBytes(parts[3]);
59-
auth_tag = Base64UrlEncoder.DecodeBytes(parts[4]);
56+
string header = Base64UrlHelper.DecodeToString(encoded_header);
57+
encrypted_key = Base64UrlHelper.DecodeToBytes(parts[1]);
58+
init_vector = Base64UrlHelper.DecodeToBytes(parts[2]);
59+
ciphertext = Base64UrlHelper.DecodeToBytes(parts[3]);
60+
auth_tag = Base64UrlHelper.DecodeToBytes(parts[4]);
6061

6162
protected_header = JsonConvert.DeserializeObject<JWE_header>(header);
6263
}
@@ -79,7 +80,7 @@ public void EncodeHeader()
7980
Formatting.None,
8081
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
8182

82-
encoded_header = Base64UrlEncoder.Encode(header_json);
83+
encoded_header = Base64UrlHelper.Encode(header_json);
8384
}
8485

8586
public string EncodeCompact()
@@ -88,25 +89,25 @@ public string EncodeCompact()
8889

8990
if (encrypted_key != null)
9091
{
91-
ret += Base64UrlEncoder.Encode(encrypted_key);
92+
ret += Base64UrlHelper.Encode(encrypted_key);
9293
}
9394

9495
ret += ".";
9596
if (init_vector != null)
9697
{
97-
ret += Base64UrlEncoder.Encode(init_vector);
98+
ret += Base64UrlHelper.Encode(init_vector);
9899
}
99100

100101
ret += ".";
101102
if (ciphertext != null)
102103
{
103-
ret += Base64UrlEncoder.Encode(ciphertext);
104+
ret += Base64UrlHelper.Encode(ciphertext);
104105
}
105106

106107
ret += ".";
107108
if (auth_tag != null)
108109
{
109-
ret += Base64UrlEncoder.Encode(auth_tag);
110+
ret += Base64UrlHelper.Encode(auth_tag);
110111
}
111112

112113
return ret;

src/KeyVault/KeyVault/SecurityDomain/Models/JWK.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Security.Cryptography;
44
using System.Security.Cryptography.X509Certificates;
55
using Microsoft.Azure.Commands.KeyVault.SecurityDomain.Common;
6+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
67
using Newtonsoft.Json;
78

89
namespace Microsoft.Azure.Commands.KeyVault.SecurityDomain.Models
@@ -96,12 +97,12 @@ public string ToJson()
9697

9798
void SetExponent(byte[] exp)
9899
{
99-
e = Base64UrlEncoder.Encode(exp);
100+
e = Base64UrlHelper.Encode(exp);
100101
}
101102

102103
void SetModulus(byte[] modulus)
103104
{
104-
n = Base64UrlEncoder.Encode(modulus);
105+
n = Base64UrlHelper.Encode(modulus);
105106
}
106107

107108
void SetKeyType(JwkKeyType keyType)
@@ -236,12 +237,12 @@ public static byte[] ToByteArray(String HexString)
236237

237238
void SetX5t(X509Certificate2 cert)
238239
{
239-
x5t = Base64UrlEncoder.Encode(ToByteArray(cert.Thumbprint));
240+
x5t = Base64UrlHelper.Encode(ToByteArray(cert.Thumbprint));
240241
}
241242

242243
void SetX5t256(X509Certificate2 cert)
243244
{
244-
x5t_S256 = Base64UrlEncoder.Encode(Utils.Sha256Thumbprint(cert));
245+
x5t_S256 = Base64UrlHelper.Encode(Utils.Sha256Thumbprint(cert));
245246
}
246247

247248
public string kty { get; set; }

src/KeyVault/KeyVault/SecurityDomain/Models/SecurityDomainClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ public SecurityDomainRestoreData EncryptForRestore(PlaintextList plaintextList,
493493
JWE jwe_wrapped = new JWE();
494494
jwe_wrapped.Encrypt(cert, master_key);
495495
securityDomainRestoreData.WrappedKey.enc_key = jwe_wrapped.EncodeCompact();
496-
securityDomainRestoreData.WrappedKey.x5t_256 = Base64UrlEncoder.Encode(Utils.Sha256Thumbprint(cert));
496+
securityDomainRestoreData.WrappedKey.x5t_256 = Base64UrlHelper.Encode(Utils.Sha256Thumbprint(cert));
497497
return securityDomainRestoreData;
498498
}
499499
catch (Exception ex)

0 commit comments

Comments
 (0)