Skip to content

Commit

Permalink
Fix readme
Browse files Browse the repository at this point in the history
  • Loading branch information
matteobaccan committed Jun 30, 2023
1 parent 6c0ec7b commit 1e17fd7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 70 deletions.
13 changes: 13 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"cSpell.words": [
"Codacy",
"Expration",
"Iiwia",
"Iiwibm",
"Ijox",
"Ikpva",
"Nqpvp",
"Qssw",
"XVCJ"
]
}
140 changes: 70 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ You can find more information about JWT on the [official website](https://jwt.io

Harbour JWT supports the following algorithms:

- HS256
- HS384
- HS512
- HS256
- HS384
- HS512

<!--
(CVE-2015-2951) The alg=none signature-bypass vulnerability
Expand All @@ -37,102 +37,102 @@ JWT is a class library that can allow you to generate and validate JWT tokens

To create a token you must

1. Load jwt.hrb library
1. Load jwt.hrb library

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

2. Create an empty JWT object
2. Create an empty JWT object

```xBase
LOCAL oJWT
LOCAL cToken
```xBase
LOCAL oJWT
LOCAL cToken
// Object
oJWT := &("JWT():new()")
```
// 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
3. Configure a valid header, setting Type = JWT and an available Algorithm. At the moment the Algorithms available are: HS256, HS384, and HS512

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

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

```xBase
METHOD SetIssuer( cIssuer )
METHOD SetSubject( cSubject )
METHOD SetAudience( cAudience )
METHOD SetExpration( nExpiration )
METHOD SetNotBefore( nNotBefore )
METHOD SetIssuedAt( nIssuedAt )
METHOD SetJWTId( cJWTId )
```
```xBase
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
A simple payload can be formed by: Subject, Name, and IssueAt

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

5. Finally you must indicate a secret
5. Finally you must indicate a secret

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

6. Now you can get a token
6. Now you can get a token

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

```Text
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
```
```Text
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
```

### Token verification

Token verifications are also simple

1. Load jwt.hrb library
1. Load jwt.hrb library

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

2. Create an empty JWT object
2. Create an empty JWT object

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

3. Verify the token
3. Verify the token

```xBase
oJWT:SetSecret("MySecret")
oJWT:Verify("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ik1hdHRlbyBCYWNjYW4iLCJpYXQiOjE1MTYyMzkwMjJ9.YR8QF52kgj0owYlP9TkEy_lNhC-Qdq38tqNNNqpvpK0")
```
```xBase
oJWT:SetSecret("MySecret")
oJWT:Verify("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ik1hdHRlbyBCYWNjYW4iLCJpYXQiOjE1MTYyMzkwMjJ9.YR8QF52kgj0owYlP9TkEy_lNhC-Qdq38tqNNNqpvpK0")
```

Verify return a .T. if the token is valid. Otherwise with
Verify return a .T. if the token is valid. Otherwise with

```xBase
oJWT:GetError()
```
```xBase
oJWT:GetError()
```

you can get the decode error
you can get the decode error

## Contribution

Expand Down

0 comments on commit 1e17fd7

Please sign in to comment.