| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | | // |
| | 5 | |
|
| | 6 | | using System; |
| | 7 | | using Newtonsoft.Json; |
| | 8 | | using Newtonsoft.Json.Linq; |
| | 9 | |
|
| | 10 | | namespace Microsoft.Azure.KeyVault.WebKey |
| | 11 | | { |
| | 12 | | internal class Base64UrlJsonConverter : JsonConverter |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Converts a byte array to a Base64Url encoded string |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="input">The byte array to convert</param> |
| | 18 | | /// <returns>The Base64Url encoded form of the input</returns> |
| | 19 | | private static string ToBase64UrlString(byte[] input) |
| | 20 | | { |
| 3406 | 21 | | if (input == null) |
| 0 | 22 | | throw new ArgumentNullException("input"); |
| | 23 | |
|
| 3406 | 24 | | return Convert.ToBase64String(input).TrimEnd('=').Replace('+', '-').Replace('/', '_'); |
| | 25 | | } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Converts a Base64Url encoded string to a byte array |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="input">The Base64Url encoded string</param> |
| | 31 | | /// <returns>The byte array represented by the enconded string</returns> |
| | 32 | | private static byte[] FromBase64UrlString(string input) |
| | 33 | | { |
| 3870 | 34 | | if (string.IsNullOrEmpty(input)) |
| | 35 | | { |
| 0 | 36 | | throw new ArgumentNullException("input"); |
| | 37 | | } |
| | 38 | |
|
| 3870 | 39 | | return Convert.FromBase64String(Pad(input.Replace('-', '+').Replace('_', '/'))); |
| | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Adds padding to the input |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="input"> the input string </param> |
| | 46 | | /// <returns> the padded string </returns> |
| | 47 | | private static string Pad(string input) |
| | 48 | | { |
| 3870 | 49 | | var count = 3 - ((input.Length + 3) % 4); |
| | 50 | |
|
| 3870 | 51 | | if (count == 0) |
| | 52 | | { |
| 1132 | 53 | | return input; |
| | 54 | | } |
| | 55 | |
|
| 2738 | 56 | | return input + new string('=', count); |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public override bool CanConvert(Type objectType) |
| | 60 | | { |
| 0 | 61 | | if (objectType == typeof(byte[])) |
| 0 | 62 | | return true; |
| | 63 | |
|
| 0 | 64 | | return false; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer seriali |
| | 68 | | { |
| 3950 | 69 | | if (objectType != typeof(byte[])) |
| | 70 | | { |
| 0 | 71 | | return serializer.Deserialize(reader, objectType); |
| | 72 | | } |
| | 73 | | else |
| | 74 | | { |
| 3950 | 75 | | var value = serializer.Deserialize<string>(reader); |
| | 76 | |
|
| 3950 | 77 | | if (!string.IsNullOrEmpty(value)) |
| | 78 | | { |
| 3870 | 79 | | return FromBase64UrlString(value); |
| | 80 | | } |
| | 81 | | } |
| | 82 | |
|
| 80 | 83 | | return null; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
| | 87 | | { |
| 3406 | 88 | | if (value.GetType() != typeof(byte[])) |
| | 89 | | { |
| 0 | 90 | | JToken.FromObject(value).WriteTo(writer); |
| | 91 | | } |
| | 92 | | else |
| | 93 | | { |
| 3406 | 94 | | JToken.FromObject(ToBase64UrlString((byte[])value)).WriteTo(writer); |
| | 95 | | } |
| 3406 | 96 | | } |
| | 97 | | } |
| | 98 | | } |