Skip to content

Generate a JWT with a RSA signature #166

@stephane-ein

Description

@stephane-ein

Hello,

I would like to generate a JWT with a RSA signature and I try to do the following :

let private_key = "-----BEGIN PRIVATE KEY-----\nMIIjioNDS78DsqqpCnWsI@9122 ... WA==-----END PRIVATE KEY-----"
let signing_secret = Secret::bytes_from_str(private_key.as_str());

let expected_claims = ClaimsSet::<CreateSessionClaims> {
    registered: RegisteredClaims {
        ..Default::default()
    }
};

let expected_jwt = JWT::new_decoded(From::from(
    RegisteredHeader {
        algorithm: SignatureAlgorithm::RS256,
        ..Default::default()
    }),expected_claims.clone());

let token = expected_jwt.into_encoded(&signing_secret).unwrap();
let token = token.unwrap_encoded().to_string();

But I got the following error :

GenericError("Invalid secret type. A RsaKeyPair is required")

I looked through the documentation and the code and it seems we cannot generate a secret from a RSA string value but only from a file (with rsa_keypair_from_file) or from jwk::RSAKeyParameters.

Is there a way to generate an encoding key with RSA like this library https://github.com/Keats/jsonwebtoken ?

let private_key = "-----BEGIN PRIVATE KEY-----\nMIIjioNDS78DsqqpCnWsI@9122 ... WA==-----END PRIVATE KEY-----"
let token = encode(&Header::new(Algorithm::RS256), &my_claims, &EncodingKey::from_rsa_pem(private_key.to_string()))?)?;

Regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions