Comments (1)
Cool! I'm not crazy!
So let me explain what I'm trying to do: a maximal encapsulation of all this JWT stuff to be used by "Application developpers". I'm looking for the simplest API that can be imagined.
My approach so far is the following one:
- A single service called IApplicationIdentity that reference a ILocalIdentity and a set of IRemoteParty. ILocalIdentity and IRemoteParty are
interface ITrustedParty { string PartyName {get;} }
:
/// <summary>
/// Primary interface that encapsulates an application identity and its trusted parties.
/// This identity is theoretically independent of a process or an application domain but
/// in practice, an identity is tied to an <see cref="AppDomain"/>.
/// </summary>
public interface IApplicationIdentity
{
/// <summary>
/// Gets this domain application's identity.
/// </summary>
ILocalIdentity Local { get; }
/// <summary>
/// Gets the available trusted parties.
/// </summary>
IReadOnlyDictionary<JsonEncodedText, IRemoteParty> TrustedParties { get; }
/// <summary>
/// Writes a signed Json Web token binary representation of claims for a target <paramref name="audience"/> (that can be
/// this <see cref="Local"/> itself).
/// </summary>
/// <param name="claims">The payload.</param>
/// <param name="audience">The target audience.</param>
/// <param name="output">The binary output.</param>
/// <param name="options">Options that defaults to <see cref="JwtWriteOptions.Default"/>.</param>
void Sign( IEnumerable<KeyValuePair<string, object>> claims, ITrustedParty audience, IBufferWriter<byte> output, JwtWriteOptions? options = null );
/// <summary>
/// Writes an encrypted and signed token binary representation of claims for a target <paramref name="audience"/> (that can be
/// this <see cref="Local"/> itself).
/// </summary>
/// <param name="claims">The payload.</param>
/// <param name="audience">The target audience.</param>
/// <param name="output">The binary output.</param>
/// <param name="options">Options that defaults to <see cref="JwtWriteOptions.Default"/>.</param>
void Encrypt( IEnumerable<KeyValuePair<string, object>> claims, ITrustedParty audience, IBufferWriter<byte> output, JwtWriteOptions? options = null );
/// <summary>
/// Attempts to validate a Json Web Token.
/// </summary>
/// <param name="monitor">The monitor to use.</param>
/// <param name="utf8Token">The token.</param>
/// <returns>The <see cref="ValidatedJwt"/> for which <see cref="ValidatedJwt.Success"/> can be false.</returns>
ValidatedJwt Validate( IActivityMonitor monitor, ReadOnlySpan<byte> utf8Token );
}
The ValidatedJwt
is... minimalist:
/// <summary>
/// Minimalist definition of a token validation result.
/// </summary>
/// <remarks>
/// There is no status or other error description: validation failures often require a lot of details
/// and should be extensible (to handle specific rules and validators): we simply use the <see cref="CK.Core.ActivityMonitor"/>
/// to log any validation details (warnings or errors).
/// </remarks>
public readonly struct ValidatedJwt
{
readonly ReadOnlyMemory<byte>? _payload;
internal ValidatedJwt( ReadOnlyMemory<byte>? payload, bool enc )
{
_payload = payload;
IsEncrypted = enc;
}
/// <summary>
/// Gets whether the token validation succeeded.
/// </summary>
public bool Success => _payload is not null;
/// <summary>
/// Gets whether the token has been validated and was encrypted.
/// </summary>
public bool IsEncrypted { get; }
/// <summary>
/// Gets the raw Utf8 encoded payload.
/// This is null when <see cref="Success"/> is false.
/// </summary>
/// <returns>The payload text or null if <see cref="Success"/> is false.</returns>
public ReadOnlyMemory<byte>? Payload => _payload;
}
Key renewal, exchanges (and bindings to explicit certificates like PEM, PKCS#12, etc.) take place under the hood (via other abstractions), but the whole idea is here: this easy to use ApplicationIdentity somehow implements/encapsulates your https://github.com/uruk-project/Jwt/blob/master/docs/Security_considerations.md.
It's hard to make simple things...
from jwt.
Related Issues (20)
- Use an `IBufferWriter` when decompressing a JWT HOT 1
- Extract JWK logic into a separate assembly
- Extract cryptography logic into a separate assembly
- Signature validation fails when no signature algorithm is specified HOT 4
- Operation is not supported on this platform. HOT 8
- AES-GCM key wrapping is generating a nonce with low entropy
- Adds support for encrypted JWK
- Adds a CLI HOT 1
- MacOS build fail
- Adding already existing member to JsonObject creates a duplicate
- JwsDescriptor.Payload setter weird behavior. HOT 7
- Typo in the AlgorithmId.ES256X? HOT 4
- JWKS ToString broken on 1.9.1 HOT 2
- Manual refresh for JwksHttpKeyProvider HOT 2
- Read SignatureAlgorithm from X509Certificate if available HOT 14
- https://jwt.io/#debugger-io?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.3ZlNebfhKEZ_HqKPIZFoI6Ro_YBlS7z2Oct4zfleOIk HOT 1
- Encrypted key present in ECDH-ES algorithm results in Invalid JWE HOT 9
- Hash-Algorithm for key derivation (ECDH-ES) HOT 2
- ECJwk GetCanonicalizeSize wrong - Kid randomly wrong HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jwt.