JsonWebKey.fromPem constructor

JsonWebKey.fromPem(
  1. String pem, {
  2. String? keyId,
})

Parses a PEM encoded public or private key

Implementation

factory JsonWebKey.fromPem(String pem, {String? keyId}) {
  var v = x509.parsePem(pem).first;

  if (v is x509.PrivateKeyInfo) {
    v = v.keyPair;
  }

  if (v is KeyPair) {
    return JsonWebKey.fromCryptoKeys(
        publicKey: v.publicKey, privateKey: v.privateKey, keyId: keyId);
  }
  if (v is x509.X509Certificate) {
    v = v.tbsCertificate.subjectPublicKeyInfo;
  }
  if (v is x509.SubjectPublicKeyInfo) {
    return JsonWebKey.fromCryptoKeys(
      publicKey: v.subjectPublicKey,
      keyId: keyId,
    );
  }
  throw UnsupportedError('Cannot create JWK from ${v.runtimeType}');
}