모든 표준 알고리즘을 완전히 지원하는 JSON 웹 토큰(JWT) 생성, 서명 및 검증을 위한 간단한 Dart 구현입니다. 다양한 플랫폼에서 안전한 토큰 처리를 가능하게 하며, Flutter 및 기타 Dart 애플리케이션의 인증 및 권한 부여 워크플로우에 이상적입니다. 패키지는 가볍고 잘 문서화되어 있으며 보안을 희생하지 않고도 쉽게 통합할 수 있도록 설계되었습니다.
사용 사례
사용자 인증
API 인증
보안 세션 관리
토큰 기반 액세스 제어
서비스 간 통신
주요 기능
모든 JWT 알고리즘을 지원합니다
사용하기 쉬운 API
크로스 플랫폼 호환성
내장 검증
보안 토큰 생성
적합한 대상
인증이 필요한 플러터 앱
JWT를 사용하는 백엔드 서비스
모바일 및 웹 애플리케이션
보안 토큰이 필요한 API
간결성과 보안을 우선시하는 개발자
고려 사항
신중한 키 관리가 필요합니다
고성능 토큰 집약 작업에 적합하지 않습니다
취약점을 방지하기 위해 올바른 구성에 의존합니다
JWT 전용 사용 사례에 한정
표준 클레임을 초과하여 내장된 토큰 만료 처리가 없습니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
JSON Web Token (JWT)
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.
// Generate a JSON Web Token// You can provide the payload as a key-value map or a stringfinal 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}');
} onJWTExpiredException {
print('jwt expired');
} onJWTExceptioncatch (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, HS512final hmacKey = SecretKey('secret passphrase');
// RS256, RS384, RS512, PS256, PS384, PS512final 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, ES512final 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-----
''');
// EdDSAfinal 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