portico_auth_tokens 1.0.0 copy "portico_auth_tokens: ^1.0.0" to clipboard
portico_auth_tokens: ^1.0.0 copied to clipboard

A library for token management (JWT/JWE) and validation.

example/main.dart

import 'package:portico_auth_tokens/portico_auth_tokens.dart';
import 'package:jose_plus/jose.dart';

void main() async {
  print('--- Token Management Example ---');

  // 1. Generate Keys
  // NOTE: IN PRODUCTION, LOAD THESE SECURELY!
  final signingKey = JsonWebKey.generate(JsonWebAlgorithm.es384.name);
  final encryptingKey = JsonWebKey.generate(JsonWebAlgorithm.a128kw.name);

  // 2. Initialize Storage
  final storage = AuthTokensInMemoryStorage();

  // 3. Initialize Manager
  final manager = AuthTokensManager(
    signingKey,
    encryptingKey,
    storage,
    issuer: 'https://api.myapp.com',
    audience: 'https://app.myapp.com',
  );

  // 4. Mint Tokens
  print('Minting tokens for [email protected]...');
  final tokens = await manager.mintTokens('[email protected]');
  final accessToken = tokens.accessToken;
  final refreshToken = tokens.refreshToken;

  print('Access Token (JWT): ${accessToken.substring(0, 20)}...');
  print('Refresh Token (JWE): ${refreshToken.substring(0, 20)}...');

  // 5. Validate Access Token
  print('\nVerifying Access Token...');
  try {
    final record = await manager.validateToken(accessToken);
    print('Token Valid! User: ${record.userId}, Serial: ${record.serial}');
  } catch (e) {
    print('Validation failed: $e');
  }

  // 6. Refresh Tokens
  print('\nExchanging Refresh Token...');
  try {
    final newTokens = await manager.newAccessToken(refreshToken);
    print('Refresh Successful!');
    print('New Access Token: ${(newTokens.accessToken).substring(0, 20)}...');
  } catch (e) {
    print('Refresh failed: $e');
  }
}
1
likes
160
points
75
downloads

Publisher

verified publishermcdole.org

Weekly Downloads

A library for token management (JWT/JWE) and validation.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

clock, equatable, jose_plus, json_annotation, logging, meta, namer, uuid

More

Packages that depend on portico_auth_tokens