Skip to content

Commit 7b544ab

Browse files
committed
Updating code style and handling of encoding algorithm
1 parent d236dcd commit 7b544ab

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

config/config.php.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
return array(
3-
'jwtKey' => '', // Key for signing the JWT's, I suggest generate it with base64_encode(openssl_random_pseudo_bytes(64))
3+
'jwt' => array(
4+
'key' => '', // Key for signing the JWT's, I suggest generate it with base64_encode(openssl_random_pseudo_bytes(64))
5+
'algorithm' => 'HS512' // Algorithm used to sign the token, see https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40#section-3
6+
),
47
'database' => array(
58
'user' => '', // Database username
69
'password' => '', // Database password

public/login.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@
8686
* keep it secure! You'll need the exact key to verify the
8787
* token later.
8888
*/
89-
$secretKey = base64_decode($config->get('jwtKey'));
89+
$secretKey = base64_decode($config->get('jwt')->get('key'));
90+
91+
/*
92+
* Extract the algorithm from the config file too
93+
*/
94+
$algorithm = $config->get('jwt')->get('algorithm');
9095

9196
/*
9297
* Encode the array to a JWT string.
@@ -97,7 +102,7 @@
97102
$jwt = JWT::encode(
98103
$data, //Data to be encoded in the JWT
99104
$secretKey, // The signing key
100-
'HS512' // Algorithm used to sign the token, see https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40#section-3
105+
$algorithm // Algorithm used to sign the token, see https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40#section-3
101106
);
102107

103108
$unencodedArray = ['jwt' => $jwt];

public/resource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
/*
3232
* decode the jwt using the key from config
3333
*/
34-
$secretKey = base64_decode($config->get('jwtKey'));
34+
$secretKey = base64_decode($config->get('jwt')->get('key'));
3535

36-
$token = JWT::decode($jwt, $secretKey, array('HS512'));
36+
$token = JWT::decode($jwt, $secretKey, [$config->get('jwt')->get('algorithm')]);
3737

3838
$asset = base64_encode(file_get_contents('http://lorempixel.com/200/300/cats/'));
3939

0 commit comments

Comments
 (0)