I am working on a function in Java which is responsible to generate a RSA token for my API's. The java person gave the following dependency to be added
api 'io.jsonwebtoken:jjwt-api:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2'
runtimeOnly('io.jsonwebtoken:jjwt-orgjson:0.11.2') {
exclude group: 'org.json', module: 'json' //provided by Android natively
}
But In flutter ,you cannot add this like that in pubspec.yaml file.
It turns out that there is another way to add dependency of the github file(after I did my research) and I found the github link of the dependency and add it to my pubspec file like this,under dependency,
dev_dependencies:
flutter_test:
sdk: flutter
plugin1:
git:
url: git://github.com/jwtk/jjwt.git
But I am getting the following errorpubspec error for git file git://github.com/jwtk/jjwt.git
I searched on pub.dev for any equivalent dependency,which was dart_json webtoken ,but that is throwing the following error:dart_jsonwebtoken error
Basically this is my java class to generate the token.Here the Jwts.builder() excepts a appropriate plugin and that where I am stuck :
import android.util.Log;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class RSAKeyGenerator {
private static PrivateKey getPrivateKey() throws GeneralSecurityException {
String pKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
KeyFactory kf = KeyFactory.getInstance("RSA");
byte[] decode;
decode = android.util.Base64.decode(pKey, android.util.Base64.DEFAULT);
PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(decode);
return kf.generatePrivate(keySpecPKCS8);
}
public static String getJwtToken() {
final long VALIDITY_MS = TimeUnit.MINUTES.toMillis(60);
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
Date exp = new Date(nowMillis + VALIDITY_MS);
PrivateKey privateKey = null;
try {
privateKey = getPrivateKey();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
String jws = Jwts.builder()
.claim("version", "13")
.claim("user_id", "xxxxxxxxxxxxxxxxxxx")
.setIssuedAt(now)
.setExpiration(exp)
.signWith(privateKey, SignatureAlgorithm.RS256)
.setAudience("live-tv")
.compact();
Log.d("111__", jws);
SpUtil.Companion.getInstance().putString(J_TOKEN, jws);
return jws;
}
}
I worked with another plugin in flutter but you can't import that in Java class and I am not sure of that plugin as well.I mean in java class import 'package:xxx.dart' throws an error.How do I solve this scenario?