如何在Flutter中从RSA私钥生成令牌?

huangapple go评论118阅读模式
英文:

How to generate a token from a RSA private key in Flutter?

问题

  1. import 'dart:convert';
  2. import 'package:crypto_keys/crypto_keys.dart';
  3. import 'package:jose/jose.dart';
  4. import 'package:x509/x509.dart';
  5. String pkey = "XXXXXXXXXXXXXsfds";
  6. void keyGenerator() async {
  7. await example2();
  8. }
  9. void example2() async {
  10. var builder = JsonWebSignatureBuilder();
  11. builder.stringContent = 'It is me';
  12. builder.setProtectedHeader('createdAt', DateTime.now().toIso8601String());
  13. builder.addRecipient(
  14. JsonWebKey.fromJson({
  15. 'kty': 'RSA',
  16. 'kid': pkey,
  17. }),
  18. algorithm: 'RS256');
  19. var jws = builder.build();
  20. print('jws compact serialization: ${jws.toCompactSerialization()}');
  21. print('jws json serialization: ${jws.toJson()}');
  22. }
  23. void tokenGenerator() {
  24. var builder = JWTBuilder();
  25. var token = builder
  26. ..audience = "live-tv"
  27. ..issuedAt = DateTime.now()
  28. ..expiresAt = DateTime.now().add(Duration(minutes: 3))
  29. ..getToken();
  30. var signer = JWTRsaSha256Signer(privateKey: RSAPrivateKey.fromString(pkey));
  31. var signedToken = builder.getSignedToken(signer);
  32. print("token");
  33. print(signedToken);
  34. var stringToken = signedToken.toString();
  35. var decodedToken = JWT.parse(stringToken);
  36. print(decodedToken.verify(signer));
  37. }

Note: The provided code snippets have been translated from the original content you provided.

英文:

Hi Guys I am really confused about so many things.All I know is that I have to generate a token at the end of the day.I am given some RSA private key which is some "XYZ............dsdsfm",(obviously I can't reveal it because of security issues),from there I have to generate some token which will serve as a header for authorisation of my API http request.

The problem is these internet related stuff of JsonWebKey and all that is not in my domain,but still I tried learning them but it is not very clear.I tried to implement some code which was written in Java by someone (and that guy knows only Java and not flutter),in flutter.

For Flutter implementation I tried two plugin in pub.dev...But I am not getting the proper output.

The first one was https://pub.dev/packages/jose>I tried what is similar in the example page of it as shown:

  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:crypto_keys/crypto_keys.dart';
  4. import 'package:jose/jose.dart';
  5. import 'package:x509/x509.dart';
  6. String pkey="XXXXXXXXXXXXXsfds";
  7. void keyGenerator() async {
  8. //await example1();
  9. await example2();
  10. // await example3();
  11. // await example4();
  12. // await example5();
  13. // await example6();
  14. // await example7();
  15. // await example8();
  16. }
  17. // decode and verify a JWS
  18. void example1() async {
  19. var encoded = pkey;
  20. // create a JsonWebSignature from the encoded string
  21. var jws = JsonWebSignature.fromCompactSerialization(encoded);
  22. // extract the payload
  23. var payload = jws.unverifiedPayload;
  24. print('content of jws: ${payload.stringContent}');
  25. print('protected parameters: ${payload.protectedHeader.toJson()}');
  26. // create a JsonWebKey for verifying the signature
  27. var jwk = JsonWebKey.fromJson({
  28. 'kty': 'RSA',
  29. 'alg': 'RS256',
  30. });
  31. var keyStore = JsonWebKeyStore()..addKey(jwk);
  32. // verify the signature
  33. var verified = await jws.verify(keyStore);
  34. print('signature verified: $verified');
  35. }
  36. // create a JWS
  37. void example2() async {
  38. // create a builder
  39. var builder = JsonWebSignatureBuilder();
  40. // set the content
  41. builder.stringContent = 'It is me';
  42. // set some protected header
  43. builder.setProtectedHeader('createdAt', DateTime.now().toIso8601String());
  44. // add a key to sign, you can add multiple keys for different recipients
  45. builder.addRecipient(
  46. JsonWebKey.fromJson({
  47. 'kty': 'RSA',
  48. 'kid': pkey,
  49. }),
  50. algorithm: 'RS256');
  51. // build the jws
  52. var jws = builder.build();
  53. // output the compact serialization
  54. print('jws compact serialization: ${jws.toCompactSerialization()}');
  55. // output the json serialization
  56. print('jws json serialization: ${jws.toJson()}');
  57. }
  58. // decode and decrypt a JWE
  59. void example3() async {
  60. var encoded = 'eyJhbGciOiJSU0ExXzUiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0.'
  61. 'UGhIOguC7IuEvf_NPVaXsGMoLOmwvc1GyqlIKOK1nN94nHPoltGRhWhw7Zx0-kFm'
  62. '1NJn8LE9XShH59_i8J0PH5ZZyNfGy2xGdULU7sHNF6Gp2vPLgNZ__deLKxGHZ7Pc'
  63. 'HALUzoOegEI-8E66jX2E4zyJKx-YxzZIItRzC5hlRirb6Y5Cl_p-ko3YvkkysZIF'
  64. 'NPccxRU7qve1WYPxqbb2Yw8kZqa2rMWI5ng8OtvzlV7elprCbuPhcCdZ6XDP0_F8'
  65. 'rkXds2vE4X-ncOIM8hAYHHi29NX0mcKiRaD0-D-ljQTP-cFPgwCp6X-nZZd9OHBv'
  66. '-B3oWh2TbqmScqXMR4gp_A.'
  67. 'AxY8DCtDaGlsbGljb3RoZQ.'
  68. 'KDlTtXchhZTGufMYmOYGS4HffxPSUrfmqCHXaI9wOGY.'
  69. '9hH0vgRfYgPnAHOd8stkvw';
  70. // create a JsonWebEncryption from the encoded string
  71. var jwe = JsonWebEncryption.fromCompactSerialization(encoded);
  72. // create a JsonWebKey for decrypting the signature
  73. var jwk = JsonWebKey.fromJson(
  74. {
  75. 'kty': 'RSA',
  76. 'n': 'sXchDaQebHnPiGvyDOAT4saGEUetSyo9MKLOoWFsueri23bOdgWp4Dy1Wl'
  77. 'UzewbgBHod5pcM9H95GQRV3JDXboIRROSBigeC5yjU1hGzHHyXss8UDpre'
  78. 'cbAYxknTcQkhslANGRUZmdTOQ5qTRsLAt6BTYuyvVRdhS8exSZEy_c4gs_'
  79. '7svlJJQ4H9_NxsiIoLwAEk7-Q3UXERGYw_75IDrGA84-lA_-Ct4eTlXHBI'
  80. 'Y2EaV7t7LjJaynVJCpkv4LKjTTAumiGUIuQhrNhZLuF_RJLqHpM2kgWFLU'
  81. '7-VTdL1VbC2tejvcI2BlMkEpk1BzBZI0KQB0GaDWFLN-aEAw3vRw',
  82. 'e': 'AQAB',
  83. 'd': 'VFCWOqXr8nvZNyaaJLXdnNPXZKRaWCjkU5Q2egQQpTBMwhprMzWzpR8Sxq'
  84. '1OPThh_J6MUD8Z35wky9b8eEO0pwNS8xlh1lOFRRBoNqDIKVOku0aZb-ry'
  85. 'nq8cxjDTLZQ6Fz7jSjR1Klop-YKaUHc9GsEofQqYruPhzSA-QgajZGPbE_'
  86. '0ZaVDJHfyd7UUBUKunFMScbflYAAOYJqVIVwaYR5zWEEceUjNnTNo_CVSj'
  87. '-VvXLO5VZfCUAVLgW4dpf1SrtZjSt34YLsRarSb127reG_DUwg9Ch-Kyvj'
  88. 'T1SkHgUWRVGcyly7uvVGRSDwsXypdrNinPA4jlhoNdizK2zF2CWQ',
  89. 'p': '9gY2w6I6S6L0juEKsbeDAwpd9WMfgqFoeA9vEyEUuk4kLwBKcoe1x4HG68'
  90. 'ik918hdDSE9vDQSccA3xXHOAFOPJ8R9EeIAbTi1VwBYnbTp87X-xcPWlEP'
  91. 'krdoUKW60tgs1aNd_Nnc9LEVVPMS390zbFxt8TN_biaBgelNgbC95sM',
  92. 'q': 'uKlCKvKv_ZJMVcdIs5vVSU_6cPtYI1ljWytExV_skstvRSNi9r66jdd9-y'
  93. 'BhVfuG4shsp2j7rGnIio901RBeHo6TPKWVVykPu1iYhQXw1jIABfw-MVsN'
  94. '-3bQ76WLdt2SDxsHs7q7zPyUyHXmps7ycZ5c72wGkUwNOjYelmkiNS0',
  95. 'dp': 'w0kZbV63cVRvVX6yk3C8cMxo2qCM4Y8nsq1lmMSYhG4EcL6FWbX5h9yuv'
  96. 'ngs4iLEFk6eALoUS4vIWEwcL4txw9LsWH_zKI-hwoReoP77cOdSL4AVcra'
  97. 'Hawlkpyd2TWjE5evgbhWtOxnZee3cXJBkAi64Ik6jZxbvk-RR3pEhnCs',
  98. 'dq': 'o_8V14SezckO6CNLKs_btPdFiO9_kC1DsuUTd2LAfIIVeMZ7jn1Gus_Ff'
  99. '7B7IVx3p5KuBGOVF8L-qifLb6nQnLysgHDh132NDioZkhH7mI7hPG-PYE_'
  100. 'odApKdnqECHWw0J-F0JWnUd6D2B_1TvF9mXA2Qx-iGYn8OVV1Bsmp6qU',
  101. 'qi': 'eNho5yRBEBxhGBtQRww9QirZsB66TrfFReG_CcteI1aCneT0ELGhYlRlC'
  102. 'tUkTRclIfuEPmNsNDPbLoLqqCVznFbvdB7x-Tl-m0l_eFTj2KiqwGqE9PZ'
  103. 'B9nNTwMVvH3VRRSLWACvPnSiwP8N5Usy-WRXS-V7TbpxIhvepTfE0NNo'
  104. },
  105. );
  106. var keyStore = JsonWebKeyStore()..addKey(jwk);
  107. // decrypt the payload
  108. var payload = await jwe.getPayload(keyStore);
  109. print('decrypted content: ${payload.stringContent}');
  110. }
  111. // create a JWE
  112. void example4() async {
  113. // create a builder
  114. var builder = JsonWebEncryptionBuilder();
  115. // set the content
  116. builder.stringContent = 'This is my bigest secret';
  117. // set some protected header
  118. builder.setProtectedHeader('createdAt', DateTime.now().toIso8601String());
  119. // add a key to encrypt the Content Encryption Key
  120. var jwk = JsonWebKey.fromJson(
  121. {
  122. 'kty': 'RSA',
  123. 'n': 'sXchDaQebHnPiGvyDOAT4saGEUetSyo9MKLOoWFsueri23bOdgWp4Dy1Wl'
  124. 'UzewbgBHod5pcM9H95GQRV3JDXboIRROSBigeC5yjU1hGzHHyXss8UDpre'
  125. 'cbAYxknTcQkhslANGRUZmdTOQ5qTRsLAt6BTYuyvVRdhS8exSZEy_c4gs_'
  126. '7svlJJQ4H9_NxsiIoLwAEk7-Q3UXERGYw_75IDrGA84-lA_-Ct4eTlXHBI'
  127. 'Y2EaV7t7LjJaynVJCpkv4LKjTTAumiGUIuQhrNhZLuF_RJLqHpM2kgWFLU'
  128. '7-VTdL1VbC2tejvcI2BlMkEpk1BzBZI0KQB0GaDWFLN-aEAw3vRw',
  129. 'e': 'AQAB',
  130. 'd': 'VFCWOqXr8nvZNyaaJLXdnNPXZKRaWCjkU5Q2egQQpTBMwhprMzWzpR8Sxq'
  131. '1OPThh_J6MUD8Z35wky9b8eEO0pwNS8xlh1lOFRRBoNqDIKVOku0aZb-ry'
  132. 'nq8cxjDTLZQ6Fz7jSjR1Klop-YKaUHc9GsEofQqYruPhzSA-QgajZGPbE_'
  133. '0ZaVDJHfyd7UUBUKunFMScbflYAAOYJqVIVwaYR5zWEEceUjNnTNo_CVSj'
  134. '-VvXLO5VZfCUAVLgW4dpf1SrtZjSt34YLsRarSb127reG_DUwg9Ch-Kyvj'
  135. 'T1SkHgUWRVGcyly7uvVGRSDwsXypdrNinPA4jlhoNdizK2zF2CWQ',
  136. 'p': '9gY2w6I6S6L0juEKsbeDAwpd9WMfgqFoeA9vEyEUuk4kLwBKcoe1x4HG68'
  137. 'ik918hdDSE9vDQSccA3xXHOAFOPJ8R9EeIAbTi1VwBYnbTp87X-xcPWlEP'
  138. 'krdoUKW60tgs1aNd_Nnc9LEVVPMS390zbFxt8TN_biaBgelNgbC95sM',
  139. 'q': 'uKlCKvKv_ZJMVcdIs5vVSU_6cPtYI1ljWytExV_skstvRSNi9r66jdd9-y'
  140. 'BhVfuG4shsp2j7rGnIio901RBeHo6TPKWVVykPu1iYhQXw1jIABfw-MVsN'
  141. '-3bQ76WLdt2SDxsHs7q7zPyUyHXmps7ycZ5c72wGkUwNOjYelmkiNS0',
  142. 'dp': 'w0kZbV63cVRvVX6yk3C8cMxo2qCM4Y8nsq1lmMSYhG4EcL6FWbX5h9yuv'
  143. 'ngs4iLEFk6eALoUS4vIWEwcL4txw9LsWH_zKI-hwoReoP77cOdSL4AVcra'
  144. 'Hawlkpyd2TWjE5evgbhWtOxnZee3cXJBkAi64Ik6jZxbvk-RR3pEhnCs',
  145. 'dq': 'o_8V14SezckO6CNLKs_btPdFiO9_kC1DsuUTd2LAfIIVeMZ7jn1Gus_Ff'
  146. '7B7IVx3p5KuBGOVF8L-qifLb6nQnLysgHDh132NDioZkhH7mI7hPG-PYE_'
  147. 'odApKdnqECHWw0J-F0JWnUd6D2B_1TvF9mXA2Qx-iGYn8OVV1Bsmp6qU',
  148. 'qi': 'eNho5yRBEBxhGBtQRww9QirZsB66TrfFReG_CcteI1aCneT0ELGhYlRlC'
  149. 'tUkTRclIfuEPmNsNDPbLoLqqCVznFbvdB7x-Tl-m0l_eFTj2KiqwGqE9PZ'
  150. 'B9nNTwMVvH3VRRSLWACvPnSiwP8N5Usy-WRXS-V7TbpxIhvepTfE0NNo'
  151. },
  152. );
  153. builder.addRecipient(jwk, algorithm: 'RSA1_5');
  154. // set the content encryption algorithm to use
  155. builder.encryptionAlgorithm = 'A128CBC-HS256';
  156. // build the jws
  157. var jwe = builder.build();
  158. // output the compact serialization
  159. print('jwe compact serialization: ${jwe.toCompactSerialization()}');
  160. // output the json serialization
  161. print('jwe json serialization: ${jwe.toJson()}');
  162. }
  163. // decode and verify and validate a JWT
  164. void example5() async {
  165. var encoded = 'eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.'
  166. 'eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt'
  167. 'cGxlLmNvbS9pc19yb290Ijp0cnVlfQ.'
  168. 'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk';
  169. // decode the jwt, note: this constructor can only be used for JWT inside JWS
  170. // structures
  171. var jwt = JsonWebToken.unverified(encoded);
  172. // output the claims
  173. print('claims: ${jwt.claims}');
  174. // create key store to verify the signature
  175. var keyStore = JsonWebKeyStore()
  176. ..addKey(JsonWebKey.fromJson({
  177. 'kty': 'oct',
  178. 'k':
  179. 'AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow'
  180. }));
  181. var verified = await jwt.verify(keyStore);
  182. print('verified: $verified');
  183. // alternatively, create and verify the JsonWebToken together, this is also
  184. // applicable for JWT inside JWE
  185. jwt = await JsonWebToken.decodeAndVerify(encoded, keyStore);
  186. // validate the claims
  187. var violations = jwt.claims.validate(issuer: Uri.parse('alice'));
  188. print('violations: $violations');
  189. }
  190. // create a JWT
  191. void example6() async {
  192. var claims = JsonWebTokenClaims.fromJson({
  193. 'exp': Duration(hours: 4).inSeconds,
  194. 'aud':"live-tv",
  195. 'iat':DateTime.now().toString(),
  196. });
  197. // create a builder, decoding the JWT in a JWS, so using a
  198. // JsonWebSignatureBuilder
  199. var builder = JsonWebSignatureBuilder();
  200. // set the content
  201. builder.jsonContent = claims.toJson();
  202. // add a key to sign, can only add one for JWT
  203. builder.addRecipient(
  204. JsonWebKey.fromJson({
  205. 'kty': 'RSA',
  206. 'kid':pkey,
  207. }),
  208. algorithm: 'HS256');
  209. // build the jws
  210. var jws = builder.build();
  211. // output the compact serialization
  212. print('jwt compact serialization: ${jws.toCompactSerialization()}');
  213. }
  214. // create a JWT, sign with RS512
  215. void example7() async {
  216. var claims = JsonWebTokenClaims.fromJson({
  217. 'exp': Duration(hours: 4).inSeconds,
  218. 'iss': 'alice',
  219. });
  220. // create a builder, decoding the JWT in a JWS, so using a
  221. // JsonWebSignatureBuilder
  222. var builder = JsonWebSignatureBuilder();
  223. // set the content
  224. builder.jsonContent = claims.toJson();
  225. // add a key to sign, can only add one for JWT
  226. var key = JsonWebKey.fromPem(File('example/jwtRS512.key').readAsStringSync());
  227. builder.addRecipient(key, algorithm: 'RS512');
  228. // build the jws
  229. var jws = builder.build();
  230. // output the compact serialization
  231. print('jwt compact serialization: ${jws.toCompactSerialization()}');
  232. }
  233. // generate a key for use with ES256 signing
  234. void example8() async {
  235. var alg = JsonWebAlgorithm.getByName('ES256');
  236. var key = alg.generateRandomKey();
  237. print(JsonEncoder.withIndent(' ').convert(key));
  238. final hash = utf8.encode('TEST');
  239. var sig = key.sign(hash);
  240. final valid = key.verify(hash, sig);
  241. print('valid? $valid');
  242. }

I tried each example at a time modifying each of those.Few of them said saying "Compact serialisation should have 3 parts",then I did a bit of research and realised that the token to be generated should have 3 parts.The other error in example 2 was that the JSONWebKey can't be signed.

There is another library I used and that was https://pub.dev/packages/corsac_jwt.

The code is below:

  1. import 'package:corsac_jwt/corsac_jwt.dart';
  2. // ..setClaim('13', {'userId': 'xxxx'})
  3. String pkey="sdfdsfdsfds";
  4. void tokenGenerator() {
  5. var builder = new JWTBuilder();
  6. var token = builder
  7. ..audience="live-tv"
  8. ..issuedAt= new DateTime.now()
  9. ..expiresAt = new DateTime.now().add(new Duration(minutes: 3))
  10. ..getToken(); // returns token without signature
  11. var signer = new JWTRsaSha256Signer(privateKey: pkey);
  12. var signedToken = builder.getSignedToken(signer);
  13. print("token");
  14. print(signedToken); // prints encoded JWT
  15. var stringToken = signedToken.toString();
  16. var decodedToken = new JWT.parse(stringToken);
  17. // Verify signature:
  18. print(decodedToken.verify(signer)); // true
  19. // Validate claims:
  20. // var validator = new JWTValidator() ;// uses DateTime.now() by default
  21. // // set claims you wish to validate
  22. // Set<String> errors = validator.validate(decodedToken);
  23. // print(errors); // (empty list)
  24. }

But here I got the error invalid private key.If I changed it to public,I got invalid public key as well.

I honestly am I a very chaotic situation mentally with time running out to implement.My team mates are know only one thing I know only flutter only.

By the way the Java code we are trying to implement in flutter is given below.Basically what it does is it takes a StringKey,converts to a PrivateKey and then the token

  1. import android.util.Log;
  2. import java.security.GeneralSecurityException;
  3. import java.security.KeyFactory;
  4. import java.security.PrivateKey;
  5. import java.security.spec.PKCS8EncodedKeySpec;
  6. import java.util.Date;
  7. import java.util.concurrent.TimeUnit;
  8. public class RSAKeyGenerator {
  9. private static PrivateKey getPrivateKey() throws GeneralSecurityException {
  10. String pKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  11. KeyFactory kf = KeyFactory.getInstance("RSA");
  12. byte[] decode;
  13. decode = android.util.Base64.decode(pKey, android.util.Base64.DEFAULT);
  14. PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(decode);
  15. return kf.generatePrivate(keySpecPKCS8);
  16. }
  17. public static String getJwtToken() {
  18. final long VALIDITY_MS = TimeUnit.MINUTES.toMillis(60);
  19. long nowMillis = System.currentTimeMillis();
  20. Date now = new Date(nowMillis);
  21. Date exp = new Date(nowMillis + VALIDITY_MS);
  22. PrivateKey privateKey = null;
  23. try {
  24. privateKey = getPrivateKey();
  25. } catch (GeneralSecurityException e) {
  26. e.printStackTrace();
  27. }
  28. String jws = Jwts.builder()
  29. .claim("version", "13")
  30. .claim("user_id", "xxxxxxxxxxxxxxxxxxx")
  31. .setIssuedAt(now)
  32. .setExpiration(exp)
  33. .signWith(privateKey, SignatureAlgorithm.RS256)
  34. .setAudience("live-tv")
  35. .compact();
  36. Log.d("111__", jws);
  37. SpUtil.Companion.getInstance().putString(J_TOKEN, jws);
  38. return jws;
  39. }
  40. }

答案1

得分: 2

经过数小时的研究、试错以及与我的团队成员咨询,我得出了最终的结论。这有效。我之前犯的错误是我将 RSA 密钥传递为字符串,像这样:key="xxxxxxxxxxxx",但实际上它应该在以下格式中:

希望这能帮助到某些人

  1. import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
  2. String token;
  3. String dartJsonWebTokenGenerator() {
  4. Duration delay = new Duration(minutes: 300);
  5. final jwt = JWT(
  6. payload: {
  7. 'version': '13',
  8. 'user_id': 'xxxxxxxxxxxxxxxxx',
  9. },
  10. audience: 'live-tv',
  11. );
  12. // 对其进行签名(默认使用 HS256 算法)
  13. token = jwt.sign(PrivateKey('''
  14. -----BEGIN RSA PRIVATE KEY-----
  15. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7
  16. ...
  17. ...
  18. ...
  19. ...
  20. ...
  21. ...
  22. ...
  23. -----END RSA PRIVATE KEY-----
  24. '''),
  25. algorithm: JWTAlgorithm.RS256, expiresIn: delay);
  26. print('已签名的令牌:$token\n');
  27. return token;
  28. }

请注意,上面的代码为您提供了关于正确使用 RSA 密钥进行签名的示例。

英文:

After hours of research,trial and errors and consulting my team mates I have arrived at the final conclusion.This works.The mistake I was doing was the RSA key I was passing it as a string like key="xxxxxxxxxxxx",but it should be in the format below

Hope this helps someone

  1. import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
  2. String token;
  3. String dartJsonWebTokenGenerator() {
  4. Duration delay = new Duration(minutes: 300);
  5. final jwt = JWT(
  6. payload: {
  7. 'version': '13',
  8. 'user_id': 'xxxxxxxxxxxxxxxxx',
  9. },
  10. audience: 'live-tv',
  11. );
  12. // Sign it (default with HS256 algorithm)
  13. token = jwt.sign(PrivateKey('''-----BEGIN RSA PRIVATE KEY-----
  14. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7
  15. wIxAoKDcHD7mv2R//I0QncGzT1I7cccrhIUB1gXH9wWdTXrafhACXrJ2Drfjg/YN
  16. 9q4TiKH1k2+zTfdU1IDxd6OX2cPNkwn/vpZeZ1pZqVoimRRJbJg8RhXYvZgd6Nfj
  17. Zdw6LY3Zzm3XbLbTUmbVM5ARDweksnr6cg7HmR2OQ5j5UFuhRyIUTSCDbAmDeyHS
  18. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx70YmgZPR
  19. PIncbKBjLzQe5RB/szyswCAiLxfk7rCrUhPpvdJ4fnDgdXQE26t/CBFb8fpOHtiL
  20. 1FtxgURmX/Nh+OhO7pPvK4X7g2GarMdQ3Y6288/STQu/d8yEQDwcro6X9ribay46
  21. Ss69AGJxGJbazUI9fNMETLpR6kIHKli7G7gtTFVY02YPKpqgYs1HFbIgV++gHTaw
  22. XLU3PqroxKkhTuI8JCei1pphS/PAc3S/o0mPxTYLfiojYG/6IDd8WsGonwwk2hqd
  23. e3ad0S56JZNN812vshc8NFMbEU2UzA7L0INf6X+GAhoZojUjZciKwAN/iF9YV0cU
  24. XorRDXcXrXn7hpqAzDq/jMQO78jr2MyaY/dfiSVp6sTS8gNMCuz0g3I7gSkCAwEA
  25. AQKCAgEAiPWqK/qdrFYR4unJwU9ouonlMWGl0BAndIUEcjIt4cPCVwtfuL+aE98q
  26. J8MJYINVTgaJ7VbnKnSkARQkuUYWCLhYF8z9hq+h3Ce2QPJMhwC1GCT3MFGMQQRD
  27. wP/U8q99k9xljOAckw6TnxjTFJCoFUQrbA9QcXb9ypSMv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Z87RZIa09wyf5hnPXQkQAHMRYxuaxvQUq/bSDO6JUbrGMp
  60. J2LQgCreNAF7MsIDU2ijVHJNWp9m6LC1Osb1iMp/fZDy+hkAAHP5xDfDedwKmXLV
  61. 2kSdWPZebq1iIRyJ+BlbXAEgDMkp+k6mebq+kyH4+3odWvw8bRkmD/g1t/bS
  62. -----END RSA PRIVATE KEY-----'''),
  63. algorithm: JWTAlgorithm.RS256, expiresIn: delay);
  64. print('Signed token: $token\n');
  65. return token;
  66. }

huangapple
  • 本文由 发表于 2020年8月24日 17:21:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63558129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定