# ForeFront.ai

![ForeFront.ai Logo](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR0F7PGn9gAVFcnlfQK-Vc4KTMbj7DXcRGhuA&usqp=CAU)

This model has access to GPT-3.5 Turbo and GPT-4. Honestly, I consider it the best for use, it is very accurate in the answers and easy to access, apart from that it is unlimited and without problems with the application.

[How to Use](https://github.com/uesleibros/OpenGPT/tree/main/opengpt/forefront/DOC.md)


## JWT

This is a big problem, as you know, JSON Web Token is an internet standard for creating optionally signed and/or encrypted data whose payload contains JSON that asserts some number of claims. Tokens are signed using a private secret or public/private key. Which means that whenever we make a request to the API to get the AI response, the token is changed.

![JWT Illustrator](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQcdAvlunISGsCpy8F8WAeTGkvwUdCOuT3y3A&usqp=CAU)

When we get the response from the `/chat` API it asks us for this JWT token. It is constantly updated for platform security and robot attack prevention.

### Solution

As you know, the only solution so far is to create a new account for each question, which is quite time consuming. The solution is to make a request on the API that generates the JWT token and then use it, but for that, you need to have the `session_id`.

![JWT Solution](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQdrx_ajRwFhXT158XH19u1B0S_Gr48mexWYw&usqp=CAU)

#### Session ID

To solve this problem, you send the request to the route that generates this new `refresh_token` or JWT if you prefer. For that, you need to use the `session_id` that is generated after creating your account, with the initials `sess_....`. Passed by the URL of the route `/tokens` the response obtained: 

```json
{
  "object": "token",
  "jwt": "jwt_token" 
}
``` 

With that you can proceed making the requests without problems, keeping this structure:

```makefile 
Step 1: Check JWT Token is Valid
Step 2: Generate new JWT Token 
Step 3: Get completion response using JWT Token 
```

### Algorithm

The algorithm that ForeFront.ai uses is `RS256`. Used a lot for these systems. They are encrypted and use a private key to perform encryption. Each JWT Token is good for something, for example. In cookies, the `__client` is what defines that you are logged into your account, containing some information, usually this format comes like this:

```js
{
  "azp": "https://chat.forefront.ai",                       //  Authorized party
  "exp": 1682912078,                                       //   Expiration time
  "iat": 1682912018,                                      //    Issued at
  "iss": "https://clerk.forefront.ai",                   //     Issuer
  "nbf": 1682912008,                                    //      Not valid before
  "sid": "sess_XXXXXXXXXXXXXXXXXXXXXXXXXXX",           //       Session ID
  "sub": "user_XXXXXXXXXXXXXXXXXXXXXXXXXXX"           //        Subject
}
```

It's tricky to be able to access and manipulate these values ​​with a well-architected system, so I owe congratulations to ForeFront.ai

## X-Signature

The X-Signature is a cryptographic signature used for authentication and integrity verification. It is generated by encrypting a base64-encoded string using the AES-256-CBC algorithm with a provided session key.

### Algorithm

The X-Signature encryption process utilizes the AES-256-CBC algorithm.

AES stands for Advanced Encryption Standard, which is a symmetric encryption algorithm widely adopted as a secure and efficient encryption standard. It supports key sizes of 128, 192, and 256 bits.

CBC (Cipher Block Chaining) is one of the modes of operation for the AES algorithm. In CBC mode, each plaintext block is combined with the previous ciphertext block before encryption. This ensures that each ciphertext block is dependent on the previous blocks, adding an extra layer of security.

The AES-256 variant of the algorithm employs a 256-bit key size, making it highly secure against brute-force attacks. It operates on 128-bit blocks and uses a series of substitution, permutation, and mixing operations to encrypt the data.

CBC mode requires an Initialization Vector (IV), which is a random value used to initialize the encryption process. The IV is combined with the first plaintext block before encryption and then with each subsequent block in a chain-like manner.

By using AES-256-CBC, the X-Signature encryption process ensures that the data is encrypted using a strong and widely accepted encryption algorithm, providing confidentiality and security for the signature.

I hope this clarifies the algorithm used in the X-Signature encryption process. Let me know if you have any further questions!

![X-Signature](https://wiki.newae.com/images/8/88/Aes256_cbc.png)

Here are the steps to get that signature.

- Generate a hash key, the provided session key is hashed using the SHA-256 algorithm. This ensures that the key has a fixed length and provides a secure representation for encryption.

- Generate an Initialization Vector (IV), the IV is a random sequence of bytes that is used as an additional input for the encryption process. It is crucial for generating unique encrypted outputs even for the same input.

- Create an AES cipher object, the AES cipher object is created using the hash key and IV. AES (Advanced Encryption Standard) is a symmetric encryption algorithm widely used for its security and efficiency.

- Pad the input data, the input data is padded to ensure it is a multiple of the AES block size. Padding ensures that the data fits into blocks for encryption.

- Encrypt the padded data, the padded data is encrypted using the AES cipher object created earlier. The encryption process transforms the data into an unreadable form using the provided key and IV.

- Return the encrypted signature, the output of the encryption process is the concatenation of the IV (converted to a hexadecimal string) and the encrypted data (also converted to a hexadecimal string). This forms the encrypted signature, which is the X-Signature.

In summary, the encryption process involves generating a hash key, creating an AES cipher object with the key and IV, padding the data, encrypting the padded data, and returning the IV and encrypted data as the X-Signature.

![AES Design](https://assets.website-files.com/5ff66329429d880392f6cba2/618e3ef1d4fd65b58fac771b_AES%20design.png)
