-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encrypted Authorization response (#170)
* encryption Signed-off-by: Kevin <[email protected]> * fix build Signed-off-by: Kevin <[email protected]> --------- Signed-off-by: Kevin <[email protected]>
- Loading branch information
Showing
13 changed files
with
173 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/WalletFramework.Oid4Vc/Oid4Vp/AuthResponse/Encryption/EncryptedAuthorizationResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Hyperledger.Aries.Extensions; | ||
using Jose; | ||
using LanguageExt; | ||
using WalletFramework.Oid4Vc.Oid4Vp.Jwk; | ||
using WalletFramework.Oid4Vc.Oid4Vp.Models; | ||
|
||
namespace WalletFramework.Oid4Vc.Oid4Vp.AuthResponse.Encryption; | ||
|
||
public record EncryptedAuthorizationResponse(string Jwe) | ||
{ | ||
public override string ToString() => Jwe; | ||
} | ||
|
||
public static class EncryptedAuthorizationResponseFun | ||
{ | ||
public static FormUrlEncodedContent ToFormUrl(this EncryptedAuthorizationResponse response) | ||
{ | ||
var content = new Dictionary<string, string> | ||
{ | ||
{ "response", response.ToString() }, | ||
}; | ||
|
||
return new FormUrlEncodedContent(content); | ||
} | ||
|
||
public static EncryptedAuthorizationResponse Encrypt( | ||
this AuthorizationResponse response, | ||
AuthorizationRequest authorizationRequest, | ||
Option<Nonce> mdocNonce) | ||
{ | ||
var verifierPubKey = authorizationRequest.ClientMetadata!.Jwks.First(); | ||
|
||
var headers = new Dictionary<string, object> | ||
{ | ||
{ "apv", authorizationRequest.Nonce }, | ||
{ "kid", verifierPubKey.Kid } | ||
}; | ||
|
||
mdocNonce.IfSome(nonce => headers.Add("apu", nonce.AsBase64Url.ToString())); | ||
|
||
var jwe = JWE.EncryptBytes( | ||
response.ToJson().GetUTF8Bytes(), | ||
new [] { new JweRecipient(JweAlgorithm.ECDH_ES, verifierPubKey.ToEcdh()) }, | ||
JweEncryption.A256GCM, | ||
mode: SerializationMode.Compact, | ||
extraProtectedHeaders: headers); | ||
|
||
return new EncryptedAuthorizationResponse(jwe); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Security.Cryptography; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace WalletFramework.Oid4Vc.Oid4Vp.Jwk; | ||
|
||
public static class JwkFun | ||
{ | ||
public static ECDiffieHellman ToEcdh(this JsonWebKey jwk) | ||
{ | ||
var ecParameters = new ECParameters | ||
{ | ||
Q = | ||
{ | ||
X = Base64UrlEncoder.DecodeBytes(jwk.X), | ||
Y = Base64UrlEncoder.DecodeBytes(jwk.Y) | ||
}, | ||
// TODO: Map curve from jwk | ||
Curve = ECCurve.NamedCurves.nistP256 | ||
}; | ||
|
||
return ECDiffieHellman.Create(ecParameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/WalletFramework.Oid4Vc/Oid4Vp/Models/ClientJwksConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Microsoft.IdentityModel.Tokens; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using WalletFramework.Core.Functional; | ||
using WalletFramework.Core.Json; | ||
|
||
namespace WalletFramework.Oid4Vc.Oid4Vp.Models; | ||
|
||
public class ClientJwksConverter : JsonConverter<List<JsonWebKey>> | ||
{ | ||
public override bool CanRead => true; | ||
|
||
public override bool CanWrite => false; | ||
|
||
public override void WriteJson(JsonWriter writer, List<JsonWebKey>? value, JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override List<JsonWebKey> ReadJson( | ||
JsonReader reader, | ||
Type objectType, | ||
List<JsonWebKey>? existingValue, | ||
bool hasExistingValue, | ||
JsonSerializer serializer) | ||
{ | ||
var json = JObject.Load(reader); | ||
var validKeysArray = | ||
from keys in json.GetByKey("keys") | ||
from keysArray in keys.ToJArray() | ||
select keysArray; | ||
|
||
var result = validKeysArray.OnSuccess(keysArray => | ||
{ | ||
return keysArray.Select(keyToken => JsonWebKey.Create(keyToken.ToString())).ToList(); | ||
}).UnwrapOrThrow(); | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters