-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b658822
commit 9d1af94
Showing
8 changed files
with
138 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters