Skip to content

Commit

Permalink
Merge pull request #3 from HichemTab-tech/make-the-package-compatible…
Browse files Browse the repository at this point in the history
…-with-php-7.1

transform to php 7.1
  • Loading branch information
HichemTab-tech committed Mar 27, 2024
2 parents d8f3ddc + 7791bf5 commit 241f12c
Showing 1 changed file with 46 additions and 36 deletions.
82 changes: 46 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ TokensValidation is a PHP library designed to generate and verify authentication

## Installation

> ⚠️ **WARNING**: This version is compatible with PHP 7.1. If you are using PHP 8, it is recommended to use the [latest version](https://github.com/HichemTab-tech/tokens-validation) of this library.
The TokensValidation library can be installed via Composer by running the following command:

```bash
Expand Down Expand Up @@ -89,14 +91,17 @@ The First example is an Auto generating and verifying:
- To generate an authentication token using cookies, call the following method:
```PHP
$authToken = TokensValidation::createNewAuthToken(
userId: $uid,
usingCookies: true
$uid,
"",
true
);

// you can print $authToken for additional informations about the created token.
```
This method generates a new authentication token for the given user ID and saves it in a cookie. The second argument specifies whether to use cookies or not.

> ⚠️ It's **highly recommended** to use the [latest version](https://github.com/HichemTab-tech/tokens-validation) of the library which is compatible with **PHP 8.0** and above.
To check the authentication token, call the following method:
```PHP
$result = TokensValidation::checkAuthToken();
Expand Down Expand Up @@ -130,8 +135,9 @@ To generate an authentication token and handle it yourself, call the following m

```PHP
$authToken = TokensValidation::createNewAuthToken(
userId: $uid,
usingCookies: false
$uid,
"",
false
);

echo $authToken->getUserId();
Expand Down Expand Up @@ -195,14 +201,14 @@ You can add an extra layer of security to authentication tokens by using a brows

```PHP
$authToken = TokensValidation::createNewAuthToken(
userId: $uid,
fingerPrint: $somefingerprint
$uid,
$somefingerprint
);
```
To check the authentication token with a fingerprint, call the **checkAuthToken()** method with the fingerprint argument.

```PHP
$result = TokensValidation::checkAuthToken(authToken: $authToken, fingerPrint: $somefingerprint);
$result = TokensValidation::checkAuthToken($somefingerprint, $authToken);
```

- *to generate the fingerprint, you can use for example https://github.com/Valve/fingerprintjs2*
Expand All @@ -220,8 +226,8 @@ In this case, the library generates a lengthy token that includes an encrypted u

```PHP
$confirmationToken = TokensValidation::createNewConfirmationToken(
userId: $uid,
confirmationType: ConfirmationsTokenTypes::IN_URL
$uid,
ConfirmationsTokenTypes::IN_URL
);

echo $confirmationToken->getContent();// if you want to view the long confirmation token
Expand All @@ -234,7 +240,7 @@ $confirmationToken = TokensValidation::createNewConfirmationToken(
You can utilize these lines of code to verify the contents of a URL.

```PHP
$result = TokensValidation::checkConfirmationUrl(url: $url);
$result = TokensValidation::checkConfirmationUrl($url);
if ($result->isValidationSucceed()) {
//for example :
echo $result->getUserId();
Expand All @@ -248,7 +254,7 @@ $result = TokensValidation::checkConfirmationUrl(url: $url);
Or you can inject **$_GET** directly:

```PHP
$result = TokensValidation::checkConfirmationUrlParamsFromGET(_GET_ARRAY: $_GET);
$result = TokensValidation::checkConfirmationUrlParamsFromGET($_GET);
```

To override the ConfirmationUrl builder methods, create a class that extends the **ConfirmationUrlBuilder** class and implements the **getUrl(ConfirmationToken $confirmationToken, string $baseUrl)**, **getUserIdAndTokenFromUrl(string $url)**, and **getUserIdAndTokenFromGET(array $_GET_ARRAY)** methods. Then, set the $ConfirmationUrlBuilder property to the name of your new class. Here's an example:
Expand Down Expand Up @@ -297,14 +303,14 @@ In this case, the user needs to enter the confirmation token generated by the li

```PHP
$confirmationToken = TokensValidation::createNewConfirmationToken(
userId: $uid,
confirmationType: ConfirmationsTokenTypes::SMALL_CODE
$uid,
ConfirmationsTokenTypes::SMALL_CODE
);

echo $confirmationToken->getContent();


$result = TokensValidation::checkConfirmationCode(code: $token);
$result = TokensValidation::checkConfirmationCode($token);
if ($result->isValidationSucceed()) {
//for example :
echo $result->getUserId();
Expand All @@ -320,16 +326,16 @@ To ensure that each confirmation code is used for its intended purpose, you can

```PHP
$confirmationToken = TokensValidation::createNewConfirmationToken(
userId: $uid,
confirmationType: ConfirmationsTokenTypes::SMALL_CODE,
whatFor: "email-confirmation"
$uid,
ConfirmationsTokenTypes::SMALL_CODE,
"email-confirmation"
);
```


To check it :
```PHP
$result = TokensValidation::checkConfirmationCode(code: $token, whatFor: "email-confirmation");
$result = TokensValidation::checkConfirmationCode($token, null, "email-confirmation");
```

If the "whatFor" parameter does not match the intended purpose of the confirmation code, the validation process will fail.
Expand All @@ -340,10 +346,10 @@ you want just to check the token if its valid, then check it later in another po
This parameter allows you to specify whether the token will be deleted after the validation succeeded or not.

```PHP
$confirmationToken = TokensValidation::createNewConfirmationToken(
userId: $uid,
confirmationType: ConfirmationsTokenTypes::SMALL_CODE,
whatFor: "email-confirmation",
$confirmationToken = TokensValidation::checkConfirmationCode(
$uid,
ConfirmationsTokenTypes::SMALL_CODE,
"email-confirmation",
deleteAfterCheck: false, //true by default
);
```
Expand All @@ -357,10 +363,11 @@ the library returns the existed token only with different expiration date.

```PHP
$confirmationToken = TokensValidation::createNewConfirmationToken(
userId: $uid,
confirmationType: ConfirmationsTokenTypes::SMALL_CODE,
whatFor: "email-confirmation",
singleTokenPerTime: true
$uid,
ConfirmationsTokenTypes::SMALL_CODE,
"email-confirmation",
-1,
true
);
```

Expand Down Expand Up @@ -391,8 +398,10 @@ TokensValidation::setConfirmationTokenExpirationDelay(60 * 60); // seconds
You have the option to provide a custom expiration delay by passing the "expirationDelay" parameter to the function which generates the token for either the confirmation token or authentication token. You can accomplish this by using the following code:
```PHP
$confirmationToken = TokensValidation::createNewConfirmationToken(
userId: $uid,
expirationDelay: 60*60 // seconds
$uid,
ConfirmationsTokenTypes::SMALL_CODE,
"default",
60*60 // seconds
);
```

Expand Down Expand Up @@ -436,9 +445,10 @@ you can create an invitation by calling this code:

```PHP
$invitation = TokensValidation::createInvitation(
userId: $uid,
target_email: "[email protected]",
whatFor: "become-admin",
$uid,
"[email protected]",
-1,
"become-admin",
);

echo $invitation->getUrl();
Expand All @@ -463,8 +473,8 @@ Typically, when using the invitation feature for actions such as sign up, the us
<?php

$invitation = TokensValidation::checkInvitationToken(
token: $_GET['token'],
whatFor: "administration",
$_GET['token'],
"administration",
);

if (!$invitation->isValidationSucceed()) {
Expand Down Expand Up @@ -492,9 +502,9 @@ After the user inputs the required information and submits the form, the token n
...

$invitation = TokensValidation::checkInvitationToken(
token: $_GET['token'],
whatFor: "administration",
thenAccept: true
$_GET['token'],
"administration",
true
);

if (!$invitation->isValidationSucceed()) {
Expand Down

0 comments on commit 241f12c

Please sign in to comment.