0

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?

1 Answer 1

3

You can not add Java libraries/classes to Flutter directly, or the other way around. Flutter uses Dart and no other language.

If you want to use the provided Java code on Android, you have to build yourself a Flutter plugin. You can read on how to do that here: https://flutter.dev/docs/development/packages-and-plugins/developing-packages

But I doubt you need a plugin to generate a JWT, there are lots of packages out there that deal with JWTs: https://pub.dev/packages?q=jwt

Regarding your dependency error:

Please post the stacktrace next time and not a screenshot.

dart_jsonwebtoken uses an older version of the crypto package than Flutter. You can try overriding the dependency.

You can read about dependencies in general and overrides here: https://dart.dev/tools/pub/dependencies

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.