dvsekhvalnov / jose-jwt

Ultimate Javascript Object Signing and Encryption (JOSE), JSON Web Token (JWT) and Json Web Keys (JWK) Implementation for .NET and .NET Core
MIT License
936 stars 184 forks source link

JWK Format KeyType = oct #210

Closed andezapriansyah closed 1 year ago

andezapriansyah commented 1 year ago

Hello @dvsekhvalnov , how to make a jwk so that it can be included in the JWT. Encode ini .net core? i used code var secretKey = new Jwk(secret); Still an error occurs in the jwk key,

jwk format i expected like this : {"kty":"oct","k":"Fdh9u8rINxfivbria1JKVT1u232VQBZYKx1HGAGPABa"}

dvsekhvalnov commented 1 year ago

Hey @andezapriansyah ,

you can serialize key to IDictionary for instance and then include as extraHeaders to encode: https://github.com/dvsekhvalnov/jose-jwt#reading-jwk https://github.com/dvsekhvalnov/jose-jwt#adding-extra-headers

something like:

var secretKey = new Jwk(secret);
var headers = new Dictionary<string, object>()
{
     { "key", secretKey.ToDictionary() },
};

string token = Jose.JWT.Encode(payload, ...... , extraHeaders: headers);

or if you can serialize JWK to json string directly: yourkey.ToJson(JWT.DefaultSettings.JsonMapper);

andezapriansyah commented 1 year ago

the secret must be a byte[] ? that I have is a string

i tried to change it from string to byte but got an error : AesKeyWrap management algorithm expected key of size 256 bits, but was given 344 bits

this is my code: string secret = "QvIP2QTvOk4O0rYx4a9In1WMB9P4KIxzULdVtZceScu"; byte[] secretKey = Encoding.UTF8.GetBytes(secret); string token = JWT.Encode(payloads, secretKey, JweAlgorithm.A256KW, JweEncryption.A256CBC_HS512, JweCompression.DEF);

dvsekhvalnov commented 1 year ago

Not sure, how it is related to Jwk?

For encrypting token, key can take variety of formats, see here: https://github.com/dvsekhvalnov/jose-jwt#creating-encrypted-tokens

for AesKeyWrap it will expect byte[] array.

andezapriansyah commented 1 year ago

thanks a lot, it's working. with code like this :

Jwk publicKey = Jwk.FromDictionary(new Dictionary<string, object>()
{
    {"kty", "oct"},
    {"k", "[secret]"}
});
dvsekhvalnov commented 1 year ago

ok ) feel free to close if no more questions.