# @ohos/jsonwebtoken

## Introduction

JSON Web Token (JWT) is an open standard (RFC 7519) for securely transmitting JSON objects between parties. The information to be transmitted is digitally signed, and therefore can be verified and trusted. 

[@ohos/jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) provides JWT implementation in OpenHarmony. This library is adapted from [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) v9.0.1.

<img src="screenshot/sign_EN.jpeg" width="50%"/>

## How to Install

```
ohpm install @ohos/jsonwebtoken
```

For details about the OpenHarmony ohpm environment configuration, see [OpenHarmony HAR](https://gitcode.com/openharmony-tpc/docs/blob/master/OpenHarmony_har_usage.en.md).

## How to Use

### Basic Usage Examples

1. Synchronously sign data with the default algorithm HMAC SHA256.
    ```js
    import jwt from "@ohos/jsonwebtoken";
    let token = jwt.sign({ foo: 'bar' }, 'shhhhh');
    ```
2. Asynchronously sign data.
    ```js
    jwt.sign({ foo: 'bar' }, privateKey, { algorithm: 'RS256' }, function(err, token) {
      console.log(token);
    });
    ```
2. Use a private key to sign data synchronously with RSA SHA256.
    ```js
    // Sign with RSA SHA256
    let privateKey = "";
    let token = jwt.sign({ foo: 'bar' }, privateKey, { algorithm: 'RS256' });
    ```


4. Use a public key to verify a signature with RSA SHA256.
    ```js
    let publicKey = ""
    const decoded =  jwt.verify(signatrue,publicKey);
    ```
## Available APIs

| API                         | Description | Remarks|
|------------------------------|-----|---|
| sign(payload,secret,options) | Signs data. |  |
| verify(signature,publicKey)  | Verifies a signature. |  |

For more information, see [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken/blob/master/README.md). For details about unit test cases, see [TEST.md](https://gitcode.com/openharmony-sig/ohos_jsonwebtoken/blob/master/TEST.md).

## About obfuscation
- Code obfuscation, please see[Code Obfuscation](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/arkts-utils/source-obfuscation.md)
- If you want the jsonwebtoken library not to be obfuscated during code obfuscation, you need to add corresponding exclusion rules in the obfuscation rule configuration file obfuscation-rules.txt：
```
-keep
./oh_modules/@ohos/jsonwebtoken
```

## Constraints

This project has been verified in the following version:

- DevEco Studio: 5.0.1 Beta1(5.1.0.258), SDK: API12 Release(5.0.0.71)

- DevEco Studio: NEXT Beta1-5.0.3.806, SDK: API12 Release(5.0.0.66) 

- DevEco Studio: 4.0 Canary2 (4.0.1.400), SDK: API10 (4.0.0.24) Beta1

## How to Contribute

If you find any problem when using the project, submit an [issue](https://gitcode.com/openharmony-sig/ohos_jsonwebtoken/issues) or a [PR](https://gitcode.com/openharmony-sig/ohos_jsonwebtoken/pulls).

## License

This project is licensed under [The MIT License](https://gitcode.com/openharmony-sig/ohos_jsonwebtoken/blob/master/LICENSE).
