Skip to content

Commit

Permalink
doc: adjust the documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
msharbaji committed Mar 5, 2024
1 parent 3aaacef commit cf22078
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 31 deletions.
1 change: 1 addition & 0 deletions .github/workflows/check-dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- '.github/workflows/**'
- '.github/ISSUE_TEMPLATE/**'
- '**.md'
- 'examples/**'
workflow_dispatch:
workflow_call:

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
paths-ignore:
- '**.md'
- '.github/ISSUE_TEMPLATE/**'
- 'examples/**'
workflow_call:

jobs:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- '.github/workflows/**'
- '.github/ISSUE_TEMPLATE/**'
- '**.md'
- 'examples/**'
pull_request:
branches: [ main ]
types: [ ready_for_review ]
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
paths-ignore:
- '.github/ISSUE_TEMPLATE/**'
- '**.md'
- 'examples/**'
workflow_dispatch:

jobs:
Expand Down
38 changes: 16 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# :closed_lock_with_key: grpc-hmac-interceptor

This TypeScript library provides an HMAC authentication interceptor for gRPC, simplifying the process of setting up HMAC authentication for both server and client. It utilizes grpc-js, the official gRPC library for Node.js, employing interceptors to seamlessly integrate HMAC authentication into the gRPC server and client.
This TypeScript library provides an HMAC authentication interceptor for gRPC, simplifying the process of setting up HMAC authentication for both server and client. It utilizes [grpc-js], the official gRPC library for Node.js, employing interceptors to seamlessly integrate HMAC authentication into the gRPC server and client.

## :rocket: Usage

Expand Down Expand Up @@ -42,39 +42,33 @@ Add the HMAC client interceptor to the gRPC client.
---
## ✏️ [Example]

### Requirements
### :cook: Requirements

```shell
# Build the ts-grpc-hmac library
npm run all

# Package the ts-grpc-hmac library
npm pack

# Go to the example directory
# go to example directory
pushd example

# Install the required packages
# install the dependencies
npm install
# Generate the gRPC code
npm run generate

# Run the example
# Update the grpc-hmac-interceptor to the latest version
npm install grpc-hmac-interceptor@latest # <latest> is the latest version

# run the example # it will start server with HMAC interceptor and two clients with HMAC interceptor, one with valid HMAC and other with invalid HMAC signature
./run.sh
```

### :tv: Demo:
[![asciicast](https://asciinema.org/a/59n7aYYFa7LJML5KJ7kQVtbIp.svg)](https://asciinema.org/a/59n7aYYFa7LJML5KJ7kQVtbIp)

## :key: HMAC Authentication

Steps for generating the HMAC:

1. **Encode Request Payload**: check the request payload if not empty, encode the request payload using the gob encoder. This encoding method serializes Go data structures into a binary format. If the payload is not empty, encode the request payload using the gob encoder. This encoding method serializes data structures into a binary format.

2. **Concatenate with Method Name**: Concatenate the encoded request payload (or just the full method name if the payload is empty) with the full method name of the gRPC service. Use a semicolon (;) as the separator between the payload and the method name.

3. **Encrypt with Secret**: Encrypt the concatenated message using the SHA512_256 algorithm and a secret key. HMAC typically involves using a cryptographic hash function (in this case, SHA512_256) along with a secret key to generate a fixed-size hash value.

4. **Base64 Encode**: Base64 encode the encrypted message to ensure that it is transmitted safely.
1. **Encode Request Payload**: stringify the request payload
2. **Concatenate with Method Name**: build a message by concatenation `request=<stringified request>;method=<method name>`, where `request` is the stringified request payload and `method` is the name of the method being called, e.g. `request={"name":"John"};method=/example.UserService/GetUser`. If the request payload is empty, the message will be just method name, e.g. `method=/example.UserService/GetUser`.
3. **Encrypt with Secret**: encrypt the concatenated message using the SHA512_256 algorithm and a secret key. HMAC typically involves using a cryptographic hash function (in this case, SHA512_256) along with a secret key to generate a fixed-size hash value.
4. **Base64 Encode**: encode the encrypted message to base64 to ensure that it is transmitted safely.


Steps for verifying the HMAC:
Expand All @@ -83,9 +77,9 @@ Steps for verifying the HMAC:
2. **Server Interceptor**: The server interceptor will extract the `x-hmac-key-id` and `x-hmac-signature` from the incoming request metadata, and then verify the HMAC signature using the `x-hmac-key-id` and the secret key associated with the key id.
3. if signature is **valid**, the request will be processed, **otherwise** `UNAUTHENTICATED` error will be returned.

## CONTRIBUTING
## :computer: CONTRIBUTING

We welcome contributions to ts-grpc-hmac! Please see the [CONTRIBUTING.md](./CONTRIBUTING.md) file for more information.

[Example]: ./example/README.md

[grpc-js]: https://github.com/grpc/grpc-node/tree/master/packages/grpc-js
11 changes: 5 additions & 6 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"devDependencies": {
"@grpc/grpc-js": "^1.10.1",
"@grpc/proto-loader": "^0.7.10",
"grpc-hmac-interceptor": "file:../grpc-hmac-interceptor-1.0.0.tgz",
"grpc-hmac-interceptor": "^1.0.1",
"tsx": "^4.7.1",
"typescript": "^5.3.3"
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cf22078

Please sign in to comment.