Skip to content

Commit

Permalink
First running version
Browse files Browse the repository at this point in the history
  • Loading branch information
matteobaccan committed Mar 10, 2022
1 parent b658822 commit 9d1af94
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 14 deletions.
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,115 @@
# HarbourJwt
JWT Implementation for Harbour

A simple library to work with JSON Web Token and JSON Web Signature for Harbour language

## Installation

Package is available on [GitHub](https://github.com/matteobaccan/HarbourJwt/blob/main/lib/jwt.hrb),

```shell
wget https://github.com/matteobaccan/HarbourJwt/blob/main/lib/jwt.hrb
```

## Documentation
JWT is a class library that can allow you to generate and validate JWT tokens

### Token generation
For create a token you must

1. Load jwt,hrb library

```
LOCAL handle := hb_hrbLoad( "jwt.hrb" )
```

2. Create an empty JWT object

```
LOCAL oJWT
LOCAL cToken
// Object
oJWT := &("JWT():new()")
```

3. Configure a valid header, setting Type = JWT and an available Algorithm. At the moment the Algorithms available are: HS256, HS384 and HS512

```
// Header
oJWT:setAlgorithm("HS256")
oJWT:setType("JWT")
```

4. Load a payload. The properties permitted in a payload are:

```
METHOD SetIssuer( cIssuer )
METHOD SetSubject( cSubject )
METHOD SetAudience( cAudience )
METHOD SetExpration( nExpiration )
METHOD SetNotBefore( nNotBefore )
METHOD SetIssuedAt( nIssuedAt )
METHOD SetJWTId( cJWTId )
```

A simple payload can be formed by: Subject, Name and IssueAt

```
// Payload
oJWT:setSubject("1234567890")
oJWT:setPayloadData("name", "John Doe")
oJWT:setIssuedAt(1516239022)
```

5. Finally you must indicate a secret

```
// Secret
oJWT:setSecret("your-256-bit-secret")
```

6. Now you can get a token

```
// Get Token
cToken = oJWT:Encode()
```

```
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
```

### Token verification
Token verifications are also symple

1. Load jwt.hrb library

```
LOCAL handle := hb_hrbLoad( "jwt.hrb" )
```

2. Create an empty JWT object

```
LOCAL oJWT
// Object
oJWT := &("JWT():new()")
```

3. Verify the token

```
oJWT:Decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ik1hdHRlbyBCYWNjYW4iLCJpYXQiOjE1MTYyMzkwMjJ9.YR8QF52kgj0owYlP9TkEy_lNhC-Qdq38tqNNNqpvpK0", "MySecret")
```

Decode return a .T. if token is valid. Other wise with

```
oJWT:GetError()
```
You can get the decode error

## Contribution
Feel free to update this code with new PR
12 changes: 0 additions & 12 deletions build.bat

This file was deleted.

15 changes: 15 additions & 0 deletions buildandtest.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@echo off
@set path=t:\harbour\bin
@set include=t:\harbour\include

harbour src\jwt.prg /n /w /gh /olib\jwt
if %errorlevel% neq 0 pause

harbour test\jwttest.prg /n /w /gh /oout\jwttest
if %errorlevel% neq 0 pause

cd out
hbrun jwttest.hrb>jwttest.log
type jwttest.log
cd ..

Binary file added lib/jwt.hrb
Binary file not shown.
Binary file added out/jwttest.hrb
Binary file not shown.
8 changes: 8 additions & 0 deletions out/jwttest.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

OK - signature verified
OK - signature verified
OK - signature verified
OK - signature verified
OK - signature verified
OK - signature verified
Token expired
2 changes: 1 addition & 1 deletion src/jwt.prg
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ CLASS JWT
METHOD Base64UrlDecode( cData )
METHOD ByteToString( cData )
METHOD GetSignature( cHeader, cPayload, cSecret, cAlgorithm )
METHOD getposix()
METHOD getposix()

ENDCLASS

Expand Down
2 changes: 1 addition & 1 deletion test/jwttest.prg
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "hbhrb.ch"

function main
LOCAL handle := hb_hrbLoad( "jwt.hrb" )
LOCAL handle := hb_hrbLoad( "../lib/jwt.hrb" )
LOCAL oJWT
LOCAL cToken

Expand Down

0 comments on commit 9d1af94

Please sign in to comment.