|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:typed_data'; |
| 3 | + |
| 4 | +/// Base64URL encoding and decoding utilities for JWT operations. |
| 5 | +/// Extracted and adapted from RFC 4648 specification. |
| 6 | +class Base64Url { |
| 7 | + static const String _chars = |
| 8 | + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'; |
| 9 | + static const int _bits = 6; |
| 10 | + |
| 11 | + /// Decodes a base64url encoded string to bytes |
| 12 | + /// |
| 13 | + /// [input] The base64url encoded string to decode |
| 14 | + /// [loose] If true, allows lenient parsing that doesn't strictly validate padding |
| 15 | + static Uint8List decode(String input, {bool loose = false}) { |
| 16 | + // Remove padding characters |
| 17 | + String string = input.replaceAll('=', ''); |
| 18 | + |
| 19 | + // Build character lookup table |
| 20 | + final Map<String, int> codes = {}; |
| 21 | + for (int i = 0; i < _chars.length; i++) { |
| 22 | + codes[_chars[i]] = i; |
| 23 | + } |
| 24 | + |
| 25 | + // For loose mode or when there's actual content, skip strict validation |
| 26 | + // The validation below will catch actual errors during decoding |
| 27 | + if (!loose && string.isNotEmpty) { |
| 28 | + final remainder = (string.length * _bits) % 8; |
| 29 | + // Allow if remainder is 0 or if it's 2 or 4 (valid base64 partial bytes) |
| 30 | + if (remainder != 0 && remainder != 2 && remainder != 4) { |
| 31 | + throw FormatException('Invalid base64url string length'); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // Calculate output size |
| 36 | + final int outputLength = (string.length * _bits) ~/ 8; |
| 37 | + final Uint8List out = Uint8List(outputLength); |
| 38 | + |
| 39 | + // Decode the string |
| 40 | + int bits = 0; // Number of bits currently in the buffer |
| 41 | + int buffer = 0; // Bits waiting to be written out, MSB first |
| 42 | + int written = 0; // Next byte to write |
| 43 | + |
| 44 | + for (int i = 0; i < string.length; i++) { |
| 45 | + final String char = string[i]; |
| 46 | + final int? value = codes[char]; |
| 47 | + |
| 48 | + if (value == null) { |
| 49 | + throw FormatException('Invalid character in base64url string: $char'); |
| 50 | + } |
| 51 | + |
| 52 | + // Append the bits to the buffer |
| 53 | + buffer = (buffer << _bits) | value; |
| 54 | + bits += _bits; |
| 55 | + |
| 56 | + // Write out some bits if the buffer has a byte's worth |
| 57 | + if (bits >= 8) { |
| 58 | + bits -= 8; |
| 59 | + out[written++] = 0xff & (buffer >> bits); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Verify that we have received just enough bits |
| 64 | + if (bits >= _bits || (0xff & (buffer << (8 - bits))) != 0) { |
| 65 | + if (!loose) { |
| 66 | + throw FormatException('Unexpected end of base64url data'); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return out; |
| 71 | + } |
| 72 | + |
| 73 | + /// Encodes bytes to a base64url encoded string |
| 74 | + /// |
| 75 | + /// [data] The bytes to encode |
| 76 | + /// [pad] If true, adds padding characters to the output |
| 77 | + static String encode(List<int> data, {bool pad = false}) { |
| 78 | + final int mask = (1 << _bits) - 1; |
| 79 | + String out = ''; |
| 80 | + |
| 81 | + int bits = 0; // Number of bits currently in the buffer |
| 82 | + int buffer = 0; // Bits waiting to be written out, MSB first |
| 83 | + |
| 84 | + for (int i = 0; i < data.length; i++) { |
| 85 | + // Slurp data into the buffer |
| 86 | + buffer = (buffer << 8) | (0xff & data[i]); |
| 87 | + bits += 8; |
| 88 | + |
| 89 | + // Write out as much as we can |
| 90 | + while (bits > _bits) { |
| 91 | + bits -= _bits; |
| 92 | + out += _chars[mask & (buffer >> bits)]; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Handle partial character |
| 97 | + if (bits > 0) { |
| 98 | + out += _chars[mask & (buffer << (_bits - bits))]; |
| 99 | + } |
| 100 | + |
| 101 | + // Add padding characters until we hit a byte boundary |
| 102 | + if (pad) { |
| 103 | + while ((out.length * _bits) % 8 != 0) { |
| 104 | + out += '='; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return out; |
| 109 | + } |
| 110 | + |
| 111 | + /// Decodes a base64url string to a UTF-8 string |
| 112 | + static String decodeToString(String input, {bool loose = false}) { |
| 113 | + final bytes = decode(input, loose: loose); |
| 114 | + return utf8.decode(bytes); |
| 115 | + } |
| 116 | + |
| 117 | + /// Encodes a UTF-8 string to base64url |
| 118 | + static String encodeFromString(String input, {bool pad = false}) { |
| 119 | + final bytes = utf8.encode(input); |
| 120 | + return encode(bytes, pad: pad); |
| 121 | + } |
| 122 | +} |
0 commit comments