搜尋套件、儲存庫與 APP 分類

套件詳情v3.4.1

dart_jsonwebtoken

一個易於使用的 Dart JSON Web Token (JWT) 實現,支援所有演算法。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
209萬
喜歡數
360
Pub 分數
160

安裝套件

flutter pub add dart_jsonwebtoken

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個簡單的 Dart 實現,用於建立、簽署和驗證 JSON Web Tokens (JWT),完全支援所有標準演算法。它能在多個平台實現安全的權杖處理,非常適合在 Flutter 和其他 Dart 應用程式中使用於驗證與授權工作流程。此套件輕巧、文件完整,並設計為易於整合,同時不犧牲安全性。

適用情境

  • 使用者身分驗證
  • API 授權
  • 安全會話管理
  • 基於令牌的存取控制
  • 跨服務通訊

主要特色

  • 支援所有 JWT 算法
  • 易於使用的 API
  • 跨平台相容性
  • 內建驗證
  • 安全令牌生成

適合對象

  • 需要驗證的Flutter應用
  • 使用 JWT 的後端服務
  • 移動和網路應用
  • 需要安全令牌的 API
  • 注重簡單性和安全性開發者

使用建議

  • 需要謹慎管理金鑰
  • 不適合高性能量的高Token負載工作
  • 依賴於正確配置以避免漏洞
  • 僅限 JWT 特定用例
  • 沒有內建的權杖過期處理,僅限標準聲明

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

JSON Web Token (JWT)

pub version

An easy to use JSON Web Token implementation in Dart (all algorithms supported).

JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.

dart_jsonwebtoken allows you to sign, decode and verify JWT.

Examples

Check out the Example File for a full example code of all the different algorithms.

You can also check out the jwt.io website for more information.

Usage

Import
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
Creating & signing a JWT
// Generate a JSON Web Token
// You can provide the payload as a key-value map or a string
final jwt = JWT(
  // Payload
  {
    'id': 123,
    'server': {
      'id': '3e4fc296',
      'loc': 'euw-2',
    }
  },
  issuer: 'https://github.com/jonasroussel/dart_jsonwebtoken',
);

// Sign it (default with HS256 algorithm)
final token = jwt.sign(SecretKey('secret passphrase'));

print('Signed token: $token\n');
Verifying a JWT
try {
  // Verify a token (SecretKey for HMAC & PublicKey for all the others)
  final jwt = JWT.verify(token, SecretKey('secret passphrase'));

  print('Payload: ${jwt.payload}');
} on JWTExpiredException {
  print('jwt expired');
} on JWTException catch (ex) {
  print(ex.message); // ex: invalid signature
}
Decoding without verification

You can decode a token without checking its signature (e.g. for inspection). Do not use this for authorization—always verify with JWT.verify when trusting the token.

final jwt = JWT.decode(token);

print('Payload: ${jwt.payload}');
Key creation for all algorithms

The raw PEM content below is for learning only. In production, read private and public keys from files and pass their contents (as strings) to the constructors.

// HS256, HS384, HS512
final hmacKey = SecretKey('secret passphrase');


// RS256, RS384, RS512, PS256, PS384, PS512
final rsaPrivKey = RSAPrivateKey('''
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAq5QLAv9kYTgelglIhC17KdfUoinkwvQ4F0TZAp7qgmu19dCx
...
-----END RSA PRIVATE KEY-----
''');

// You can also extract the public key from a certificate with RSAPublicKey.cert(...)
final rsaPubKey = RSAPublicKey('''
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq5QLAv9kYTgelglIhC17
...
-----END PUBLIC KEY-----
'''
);


// ES256, ES256K, ES384, ES512
final ecPrivKey = ECPrivateKey('''
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgevZzL1gdAFr88hb2
...
-----END PRIVATE KEY-----
''');

// You can also extract the public key from a certificate with ECPublicKey.cert(...)
final ecPubKey = ECPublicKey('''
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEEVs/o5+uQbTjL3chynL4wXgUg2R9
...
-----END PUBLIC KEY-----
''');


// EdDSA
final edPrivKey = EdDSAPrivateKey.fromPEM('''-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEICXCjBHvjArjXquUI5jo3x5SHI4ofZA2azwJ39IC/Qct
-----END PRIVATE KEY-----
''');

final edPubKey = EdDSAPublicKey.fromPEM('''-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAEi7MNW0Q9T83UA3Rw+8DbspMgqeuxCqa2wXaWS+tHqY=
-----END PUBLIC KEY-----
''');
Supported Algorithms
JWT Algorithms Digital Signature or MAC Algorithm
HS256 HMAC using SHA-256 hash algorithm
HS384 HMAC using SHA-384 hash algorithm
HS512 HMAC using SHA-512 hash algorithm
PS256 RSASSA-PSS using SHA-256 hash algorithm
PS384 RSASSA-PSS using SHA-384 hash algorithm
PS512 RSASSA-PSS using SHA-512 hash algorithm
RS256 RSASSA-PKCS1-v1_5 using SHA-256 hash algorithm
RS384 RSASSA-PKCS1-v1_5 using SHA-384 hash algorithm
RS512 RSASSA-PKCS1-v1_5 using SHA-512 hash algorithm
ES256 ECDSA using P-256 curve and SHA-256 hash algorithm
ES256K ECDSA using secp256k curve and SHA-256 hash algorithm
ES384 ECDSA using P-384 curve and SHA-384 hash algorithm
ES512 ECDSA using P-521 curve and SHA-512 hash algorithm
EdDSA EdDSA using ed25519 curve and SHA-512 hash algorithm

所屬分類

依賴

依賴清單 3 項

原始碼儲存庫

使用此套件的專案

所在榜單