Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
1105 Внедрение механизмов обеспечения безопасности цепочки поставки программных проектов
Legacy
scanned-projects-new-group
Jwtdemo
Commits
00d734de
Commit
00d734de
authored
8 years ago
by
Sjoerd Langkemper
Browse files
Options
Download
Patches
Plain Diff
Simplified Firebase example
parent
2eac094f
master
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
firebasejwt.php
+17
-47
firebasejwt.php
with
17 additions
and
47 deletions
+17
-47
firebasejwt.php
+
17
−
47
View file @
00d734de
<?php
require
__DIR__
.
'/vendor/autoload.php'
;
require_once
__DIR__
.
'/common.php'
;
use
\
Firebase\JWT\JWT
;
JWT
::
$leeway
=
10
;
// $leeway in seconds
$shared_key
=
"secret"
;
$private_key
=
file_get_contents
(
'private.pem'
);
$public_key
=
file_get_contents
(
'public.pem'
);
$auth_header
=
$_SERVER
[
'HTTP_AUTHORIZATION'
];
if
(
stripos
(
$auth_header
,
'bearer '
)
==
0
)
{
$auth_header
=
substr
(
$auth_header
,
7
);
}
JWT
::
$supported_algs
[
'none'
]
=
[
'hash_hmac'
,
'none'
];
$algorithms
=
[
'none'
=>
'none'
,
'HS256'
=>
$shared_key
,
'RS256'
=>
$public_key
,
];
if
(
$auth_header
)
{
$header
=
json_decode
(
base64_decode
(
substr
(
$auth_header
,
0
,
strpos
(
$auth_header
,
'.'
))));
$key
=
$algorithms
[
$header
->
alg
];
try
{
$decoded
=
JWT
::
decode
(
$auth_header
,
$key
,
array_keys
(
$algorithms
));
echo
"Valid
$algorithm
JWT: "
;
print_r
(
$decoded
);
}
catch
(
Exception
$e
)
{
echo
"Invalid JWT:
$e
\n
"
;
function
encodeJwt
(
$tokenObj
,
$algorithmName
,
$keys
)
{
if
(
$algorithmName
==
'none'
)
{
return
"not supported"
;
}
}
else
{
$token
=
array
(
# Issuer
"iss"
=>
"http://demo.sjoerdlangkemper.nl/"
,
# Issued at
"iat"
=>
time
(),
# Expire
"exp"
=>
time
()
+
120
,
"data"
=>
[
"hello"
=>
"world"
]
);
echo
'<xmp>'
;
$jwt
=
JWT
::
encode
(
$token
,
$shared_key
,
'HS256'
);
echo
"HS256:
$jwt
\n
"
;
$key
=
$keys
;
if
(
is_array
(
$keys
))
{
$key
=
$keys
[
0
];
}
return
JWT
::
encode
(
$tokenObj
,
$key
,
$algorithmName
);
}
$jwt
=
JWT
::
encode
(
$token
,
$private_key
,
'RS256'
);
echo
"RS256:
$jwt
\n
"
;
function
decodeJwt
(
$token
)
{
$keys
=
getAlgorithmKeys
();
$key
=
$keys
[
'RS256'
][
1
];
echo
"Public key:
\n
$public_key
\n
"
;
return
JWT
::
decode
(
$token
,
$key
,
array_keys
(
$keys
))
;
}
include
(
'base.php'
);
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets