Skip to content

Commit

Permalink
Merge abdcf16 into 6969ed4
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Jan 4, 2019
2 parents 6969ed4 + abdcf16 commit 810ad48
Show file tree
Hide file tree
Showing 12 changed files with 616 additions and 33 deletions.
14 changes: 14 additions & 0 deletions .travis.yml
@@ -0,0 +1,14 @@
language: php

php:
- "7.2"
- "7.1"
- "7.0"
- "5.6"

install:
- composer install

script:
- vendor/bin/phpunit

108 changes: 103 additions & 5 deletions README.md
@@ -1,4 +1,7 @@
# Jwt-Wrapper for Firebase Jwt

[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg.com-brightgreen.svg)](http://opensource.byjg.com)
[![Build Status](https://travis-ci.org/byjg/jwt-wrapper.svg?branch=master)](https://travis-ci.org/byjg/jwt-wrapper)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/byjg/jwt-wrapper/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/byjg/jwt-wrapper/?branch=master)

A very simple wrapper for create, encode, decode JWT Tokens and abstract the PHP JWT Component
Expand Down Expand Up @@ -38,20 +41,112 @@ Resource

# Create your Jwt Secret Key

## Good
You can use two type of secret keys. A Hash (HS512) that is faster, or a RSA (RS512) that is more secure.

## Hash Key

```
openssl rand -base64 64 # set here the size of your key
```

## RSA

```
openssl rand -base64 32
ssh-keygen -t rsa -C "Jwt RSA Key" -b 2048 -f private.pem
openssl rsa -in private.pem -outform PEM -pubout -out public.pem
```

## Harder
**Note**: Save without password

# Create JWT Token (Hash Encoding):

```php
<?php
$server = "example.com";
$secret = new \ByJG\Util\JwtKeySecret(base64_encode("secrect_key_for_test"));

$jwtWrapper = new \ByJG\Util\JwtWrapper($server, $secret);

$token = $jwtWrapper->createJwtData([
"key" => "value",
"key2" => "value2"
]);
```

# Create JWT Token (RSA Encoding):

```php
<?php
$server = "example.com";
$secret = <<<TEXT
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA5PMdWRa+rUJmg6QMNAPIXa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ulLdWzfTLtKn7BGcUi46dCkofzfZQd5uIEukLhaU
bRqcK45UxgHg4kmsDufaJKZaCWjl3hVZrZPMQSFlWsF41bSCshzxbr3y/3lOGhA4
E+w3W+S/Uk0ZNGkzUltYy6kCgYEA0gRNeBr9z7rhG4O3j3qC3dCxCfYZ0Na8hy5v
M0PJJQ9QYTa04iyOjVItcyE1jaoHtLtoA+9syJBB7RoHIBufzcVg1Pbzf7jOYeLP
+jbTYp3Kk/vjKsQwfj/rJM+oRu3eF9qo5dbxT6btI++zVGV7lbEOFN6Sx30EV6gT
bwKkZXkCgYEAnEtN43xL8bRFybMc1ZJErjc0VocnoQxCHm7LuAtLOEUw6CwwFj9Q
GOl+GViVuDHUNQvURLn+6gg4tAemYlob912xIPaU44+lZzTMHBOJBGMJKi8WogKi
V5+cz9l31uuAgNfjL63jZPaAzKs8Zx6R3O5RuezympwijCIGWILbO2Q=
-----END RSA PRIVATE KEY-----
TEXT;

$public = <<<TEXT
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5PMdWRa+rUJmg6QMNAPI
Xa+BJVN7W0vxPN3WTK/OIv5gxgmj2inHGGc6f90TW/to948LnqGtcD3CD9KsI55M
ubafwBYjcds1o9opZ0vYwwdIV80cOVZX1IUZFTbnyyKcXeFmKt49A52haCiy4iNx
cRK38tOCApjZySx/NzMDeaXuWe+1nd3pbgYa/I8MkECa5EyabhZJPJo9fGoSZIkl
Nnyq4TfAUSwl+KN/zjj3CXad1oDT7XDDgMJDUu/Vxs7h3CQI9zILSYcL9zwttbLn
JW1WcLlAAIaAfABtSZboznsStMnYto01wVknXKyERFs7FLHYqKQANIvRhFTptseh
owIDAQAB
-----END PUBLIC KEY-----
TEXT;

# Note that if you want to use RSA just pass the 3rd argument (public key)
# See above how to create the RSA Key pair.
$jwtKey = new \ByJG\Util\JwtRsaKey($secret, $public);
$jwtWrapper = new \ByJG\Util\JwtWrapper($server, $jwtKey);

$token = $jwtWrapper->createJwtData([
"key" => "value",
"key2" => "value2"
]);
```
openssl rand -base64 64

# Extracting

```php
<?php
# If exists $_SERVER['HTTP_AUTHENTICATION'] = "Bearer $TOKEN"
$data = $jwtWrapper->extractData();

# If you want decode directly:
$data = $jwtWrapper->extractData($token);
```

**Note**: Do not forget do run base64_decode before pass to JwtWrapper
# Install

```bash
composer require "byjg/jwt-wrapper=2.0.*"
```

# Running a sample test

Expand All @@ -67,3 +162,6 @@ Access from you web browser the client.html
http://localhost:8080/client.html
```

----
[Open source ByJG](http://opensource.byjg.com)

17 changes: 15 additions & 2 deletions _config.yml
Expand Up @@ -41,13 +41,26 @@ social:
hash: opensourcebyjg
account:
facebook:
enabled: false
enabled: true
url: https://opensource.byjg.com/
profileUrl:

author:
twitter: byjg

twitter:
card: summary
username: byjg

logo: https://opensource.byjg.com/images/logo_byjg.png

analytics:
google: UA-130014324-1

plugins:
- jekyll-seo-tag

# Build settings
markdown: kramdown
remote_theme: allejo/jekyll-docs-theme
remote_theme: byjg/jekyll-docs-theme

6 changes: 5 additions & 1 deletion composer.json
Expand Up @@ -8,6 +8,10 @@
},
"license": "MIT",
"require": {
"firebase/php-jwt": "^3.0"
"firebase/php-jwt": "^5.0",
"ext-openssl": "*"
},
"require-dev": {
"phpunit/phpunit": ">5.7"
}
}
28 changes: 28 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<!-- see http://www.phpunit.de/wiki/Documentation -->
<phpunit bootstrap="./vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">

<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>

<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

</phpunit>
10 changes: 10 additions & 0 deletions src/JwtKeyInterface.php
@@ -0,0 +1,10 @@
<?php

namespace ByJG\Util;

interface JwtKeyInterface
{
public function getPublicKey();
public function getPrivateKey();
public function getAlghoritm();
}
44 changes: 44 additions & 0 deletions src/JwtKeySecret.php
@@ -0,0 +1,44 @@
<?php

namespace ByJG\Util;

class JwtKeySecret implements JwtKeyInterface
{

protected $key;

/**
* JwtKeySecret constructor.
* @param $key
* @param bool $decode
*/
public function __construct($key, $decode = true)
{
$this->key = ($decode ? base64_decode($key) : $key);
}

/**
* @param $key
* @param bool $decode
* @return JwtKeySecret
*/
public static function getInstance($key, $decode = true)
{
return new JwtKeySecret($key, $decode);
}

public function getPublicKey()
{
return $this->key;
}

public function getPrivateKey()
{
return $this->key;
}

public function getAlghoritm()
{
return 'HS512';
}
}
45 changes: 45 additions & 0 deletions src/JwtRsaKey.php
@@ -0,0 +1,45 @@
<?php

namespace ByJG\Util;

class JwtRsaKey implements JwtKeyInterface
{
protected $private;
protected $public;

/**
* JwtRsaKey constructor.
* @param $private
* @param $public
*/
public function __construct($private, $public)
{
$this->private = $private;
$this->public = $public;
}

/**
* @param $private
* @param $public
* @return JwtRsaKey
*/
public static function getInstance($private, $public)
{
return new JwtRsaKey($private, $public);
}

public function getPublicKey()
{
return $this->public;
}

public function getPrivateKey()
{
return $this->private;
}

public function getAlghoritm()
{
return 'RS512';
}
}

0 comments on commit 810ad48

Please sign in to comment.