An open API service indexing awesome lists of open source software.

https://github.com/barisyild/haxe-jwt


https://github.com/barisyild/haxe-jwt

haxe jwt

Last synced: 4 months ago
JSON representation

Awesome Lists containing this project

README

          

# Haxe JWT

A JSON Web Token (JWT) creation and verification library for Haxe.

## Features

- JWT token signing
- JWT token verification
- Multiple algorithm support (HMAC-based)
- Type-safe API
- Cross-platform compatibility

## Installation

To include this library in your project, you can use the source files from the src/ directory.

## Usage

### Creating JWT Tokens

```haxe
import haxe.Jwt;
import haxe.format.JwtAlgorithm;

var payload = {user: "root", role: "super"};
var secret = "supersecret";
var algorithm = JwtAlgorithm.HS256; // or other supported algorithms
var token = Jwt.sign(payload, secret, algorithm);
```

### Verifying JWT Tokens

```haxe
import haxe.Jwt;
import haxe.format.JwtAlgorithm;

var token = "your.jwt.token";
var secret = "supersecret";
var algorithm = JwtAlgorithm.HS256;

try {
var verifiedPayload = Jwt.verify(token, secret, algorithm);
trace('User: ${verifiedPayload.user}');
} catch (e) {
trace('Token verification error: $e');
}
```

## API Reference

### Jwt Class

#### `static function sign(payload:Dynamic, secret:String, algorithm:JwtAlgorithm):String`

Creates a JWT token using the given payload.

**Parameters:**
- `payload`: Data to be embedded in the token
- `secret`: Secret key used for signing
- `algorithm`: Algorithm to use

**Returns:** JWT token string

#### `static function verify(token:String, secret:String, algorithm:JwtAlgorithm):Dynamic`

Verifies a JWT token and returns the payload.

**Parameters:**
- `token`: JWT token to verify
- `secret`: Secret key used for verification
- `algorithm`: Expected algorithm

**Returns:** Verified payload

**Throws:**
- "Invalid token format" - Token format is invalid
- "Invalid algorithm" - Algorithm mismatch
- "Invalid signature" - Signature verification failed

## Supported Algorithms

The library supports HMAC-based algorithms. Based on the test files, HS256, HS384 and HS512 algorithms are supported.

## Development

### Running Tests

Test files are located in the `tests/` directory. Tests cover:

- Token creation and verification
- Verification failure with wrong secret
- Algorithm mismatch errors

## Contributing

To contribute to this project:

1. Fork the repository
2. Create a feature branch
3. Commit your changes
4. Submit a pull request