/// <summary>Encrypt the token.</summary> protected void EncryptToken(ReadOnlySpan <byte> payload, EncodingContext context) { var output = context.BufferWriter; EncryptionAlgorithm enc = _enc; var key = _encryptionKey; if (key is null) { ThrowHelper.ThrowKeyNotFoundException(); return; } KeyManagementAlgorithm alg = _alg; if (key.TryGetKeyWrapper(enc, alg, out var keyWrapper)) { var header = Header; byte[]? wrappedKeyToReturnToPool = null; byte[]? buffer64HeaderToReturnToPool = null; byte[]? arrayCiphertextToReturnToPool = null; int keyWrapSize = keyWrapper.GetKeyWrapSize(); Span <byte> wrappedKey = keyWrapSize <= Constants.MaxStackallocBytes ? stackalloc byte[keyWrapSize] : new Span <byte>(wrappedKeyToReturnToPool = ArrayPool <byte> .Shared.Rent(keyWrapSize), 0, keyWrapSize); var cek = keyWrapper.WrapKey(null, header, wrappedKey); try { using var bufferWriter = new PooledByteBufferWriter(); var writer = new Utf8JsonWriter(bufferWriter, JsonSerializationBehavior.NoJsonValidation); int base64EncodedHeaderLength = 0; ReadOnlySpan <byte> headerJson = default; var headerCache = context.HeaderCache; if (headerCache.TryGetHeader(header, alg, enc, _kid, _typ, _cty, out byte[]? cachedHeader))
private string DebuggerDisplay() { using var bufferWriter = new PooledByteBufferWriter(); using (Utf8JsonWriter writer = new Utf8JsonWriter(bufferWriter, new JsonWriterOptions { Indented = true })) { WriteTo(writer); } var input = bufferWriter.WrittenSpan; return(Utf8.GetString(input)); }
/// <inheritsdoc /> public override string ToString() { using var bufferWriter = new PooledByteBufferWriter(); using (Utf8JsonWriter writer = new Utf8JsonWriter(bufferWriter, new JsonWriterOptions { Indented = true })) { WriteTo(writer); } var input = bufferWriter.WrittenSpan; return(Utf8.GetString(input)); }
/// <inheritdoc/> public override void Encode(EncodingContext context) { using var bufferWriter = new PooledByteBufferWriter(); var ctx = new EncodingContext(bufferWriter, context); if (Payload is null) { ThrowHelper.ThrowInvalidOperationException_UndefinedPayload(); } using var writer = new Utf8JsonWriter(ctx.BufferWriter); Payload.WriteTo(writer); EncryptToken(bufferWriter.WrittenSpan, context); }
public byte[] Serialize() { using var bufferWriter = new PooledByteBufferWriter(); using (var writer = new Utf8JsonWriter(bufferWriter, new JsonWriterOptions { SkipValidation = true })) { writer.WriteStartObject(); WriteTo(writer); writer.WriteEndObject(); } var input = bufferWriter.WrittenSpan; return(input.ToArray()); }
/// <summary> /// Writes a JWT in its compact serialization format. /// </summary> /// <param name="descriptor">The descriptor of the JWT.</param> /// <returns>The array of <see cref="byte"/> representation of the JWT.</returns> public byte[] WriteToken(JwtDescriptor descriptor) { using var bufferWriter = new PooledByteBufferWriter(); WriteToken(descriptor, bufferWriter); return(bufferWriter.WrittenSpan.ToArray()); }
/// <summary> /// Writes a JWT in its compact serialization format and returns it a string. /// </summary> /// <param name="descriptor">The descriptor of the JWT.</param> /// <returns>The <see cref="string"/> retpresention of the JWT.</returns> public string WriteTokenString(JwtDescriptor descriptor) { using var bufferWriter = new PooledByteBufferWriter(); WriteToken(descriptor, bufferWriter); return(Utf8.GetString(bufferWriter.WrittenSpan)); }
/// <inheritsdoc /> public override void Encode(EncodingContext context, IBufferWriter <byte> output) { using var bufferWriter = new PooledByteBufferWriter(); Payload?.Encode(context, bufferWriter); EncryptToken(context, bufferWriter.WrittenSpan, output); }