Skip to content

Commit ecde4c9

Browse files
committed
Adding login, and resource fetch
1 parent 57f9294 commit ecde4c9

File tree

5 files changed

+99
-48
lines changed

5 files changed

+99
-48
lines changed

assets/r34.png

218 KB
Loading

public/index.html

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,53 @@
11
<!DOCTYPE html>
22
<html class="no-js">
3-
<head>
4-
<meta charset="utf-8">
5-
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
6-
<title></title>
7-
<meta name="description" content="">
8-
<meta name="viewport" content="width=device-width, initial-scale=1">
9-
10-
<link rel="stylesheet" href="css/normalize.min.css">
11-
<link rel="stylesheet" href="css/main.css">
12-
13-
<!--[if lt IE 9]>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
6+
<title></title>
7+
<meta name="description" content="">
8+
<meta name="viewport" content="width=device-width, initial-scale=1">
9+
<link rel="stylesheet" href="css/normalize.min.css">
10+
<link rel="stylesheet" href="css/main.css">
11+
<!--[if lt IE 9]>
1412
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
1513
<script>window.html5 || document.write('<script src="js/vendor/html5shiv.js"><\/script>')</script>
16-
<![endif]-->
17-
</head>
18-
<body>
19-
<h1>Login</h1>
20-
<div id="loginForm">
21-
<form>
22-
<table>
23-
<tr>
24-
<td>Usarname</td>
25-
<td><input type="text" name="username" id="username"></td>
26-
</tr>
27-
<tr>
28-
<td>Password</td>
29-
<td><input type="password" name="password" id="password"></td>
30-
</tr>
31-
<tr>
32-
<td>&nbsp;</td>
33-
<td><input type="button" name="submit" value="Submit" id="submit"></td>
34-
</tr>
35-
</table>
36-
</form>
37-
</div>
38-
<h1>Current JWT</h1>
39-
<div id="jwt"></div>
40-
<h1>Resource</h1>
41-
<div id="resource"></div>
42-
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
43-
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.1.min.js"><\/script>')</script>
44-
45-
<script src="js/main.js"></script>
46-
</body>
14+
<![endif]-->
15+
</head>
16+
<body>
17+
<div style="margin:10px">
18+
<div id="loginForm">
19+
<h1>Login</h1>
20+
<form id="frmLogin">
21+
<table>
22+
<tr>
23+
<td>Usarname</td>
24+
<td><input type="text" name="username" id="username"></td>
25+
</tr>
26+
<tr>
27+
<td>Password</td>
28+
<td><input type="password" name="password" id="password"></td>
29+
</tr>
30+
<tr>
31+
<td>&nbsp;</td>
32+
<td><input type="button" name="submit" value="Submit" id="submit"></td>
33+
</tr>
34+
</table>
35+
</form>
36+
</div>
37+
<div id="jwt" style="display:none">
38+
<h1>Current JWT</h1>
39+
<div id="token"></div>
40+
</div>
41+
<div id="resource" style="display:none">
42+
<h1>Resource</h1>
43+
<p>
44+
<input type="submit" name="btnGetResource" id="btnGetResource" value="Get Resource">
45+
<a href="resource.php" target="_blank">Try opening the resource directly</a></p>
46+
<div id="resourceContainer"></div>
47+
</div>
48+
</div>
49+
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
50+
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.1.min.js"><\/script>')</script>
51+
<script src="js/main.js"></script>
52+
</body>
4753
</html>

public/js/main.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
$(function(){
2+
3+
var store = store || {};
4+
5+
$("#submit").click(function(e){
6+
e.preventDefault();
7+
$.post('login.php', $("#frmLogin").serialize(), function(data){
8+
store.jwt = data;
9+
$("#token").html(store.jwt);
10+
$("#loginForm").hide();
11+
$("#jwt").show()
12+
$("#resource").show();
13+
}).fail(function(){
14+
alert('error');
15+
});
16+
});
17+
18+
$("#btnGetResource").click(function(e){
19+
e.preventDefault();
20+
$.ajax({
21+
url: 'resource.php',
22+
beforeSend: function(request){
23+
request.setRequestHeader('Authorization', 'Bearer ' + store.jwt);
24+
},
25+
type: 'GET',
26+
success: function(data) {
27+
$("#resourceContainer").html('<img src="data:image/png;base64,' + data.img + '" />');
28+
},
29+
error: function(err) {
30+
alert(err);
31+
}
32+
});
33+
});
34+
});

public/login.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,15 @@
6161
*/
6262
echo JWT::encode($data, $config->jwtKey);
6363

64+
} else {
65+
header('HTTP/1.0 401 Unauthorized');
6466
}
65-
}
67+
} else {
68+
header('HTTP/1.0 404 Not Found');
69+
}
6670
} catch (Exception $e) {
67-
echo $e;
71+
header('HTTP/1.0 500 Internal Server Error');
6872
}
73+
} else {
74+
header('HTTP/1.0 400 Bad Request');
6975
}

public/resource.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
/*
1414
* Look for the 'authorization' header
1515
*/
16-
if (array_key_exists('authorization', $headers)) {
16+
if (array_key_exists('Authorization', $headers)) {
1717

1818
/*
1919
* Extract the jwt from the Bearer
2020
*/
21-
list($jwt) = sscanf( $headers['authorization'], 'Bearer %s');
21+
list($jwt) = sscanf( $headers['Authorization'], 'Bearer %s');
2222

2323
if ($jwt) {
2424
try {
@@ -29,11 +29,16 @@
2929
*/
3030
$token = JWT::decode($jwt, $config->jwtKey);
3131

32+
$asset = base64_encode(file_get_contents('assets/r34.png'));
33+
3234
/*
33-
* return the id from the token
35+
* return protected asset
3436
*/
3537
header('Content-type: application/json');
36-
echo json_encode(['userId' => $token->id]);
38+
echo json_encode([
39+
'userId' => $token->id,
40+
'img' => $asset
41+
]);
3742
} catch (Exception $e) {
3843
/*
3944
* the token was not able to be decoded.

0 commit comments

Comments
 (0)