Skip to content

Commit 124f021

Browse files
author
Mykolas Mankevicius
committed
Initial commit
0 parents  commit 124f021

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Statamic\Addons\DynamicToken;
4+
5+
use Illuminate\Http\Request;
6+
use Statamic\Extend\Controller;
7+
8+
class DynamicTokenController extends Controller
9+
{
10+
/**
11+
* Get refreshed CSRF token.
12+
*
13+
* @return string
14+
*/
15+
public function getRefresh(Request $request)
16+
{
17+
// checks that the request is comming from your own website.
18+
$referer = $request->headers->get('referer');
19+
// where APP_URL WOULD BE `site.com`
20+
$appUrl = env('APP_URL');
21+
$httpUrl = "http://{$appUrl}";
22+
$httpsUrl = "https://{$appUrl}";
23+
$startWithAppUrl = starts_with($referer, $httpUrl) || starts_with($referer, $httpsUrl);
24+
if (empty($referer) || !$startWithAppUrl) {
25+
abort(404);
26+
}
27+
28+
return csrf_token();
29+
}
30+
}

DynamicToken/DynamicTokenTags.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Statamic\Addons\DynamicToken;
4+
5+
use Statamic\Extend\Tags;
6+
7+
class DynamicTokenTags extends Tags
8+
{
9+
/**
10+
* The {{ dynamic_token }} tag
11+
* inserts a script which will add tokens to all forms which have an input with a name="_token"
12+
* refreshes said token every 15 minutes
13+
* @return string
14+
*/
15+
public function index()
16+
{
17+
$route = '/!/DynamicToken/refresh';
18+
$selector = 'form input[name="_token"]';
19+
return "
20+
<script>
21+
if (document.querySelectorAll('{$selector}').length > 0) {
22+
//add a ponyfill for IE11
23+
if (window.NodeList && !NodeList.prototype.forEach) {
24+
NodeList.prototype.forEach = function(callback, thisArg) {
25+
thisArg = thisArg || window;
26+
for (var i = 0; i < this.length; i++) {
27+
callback.call(thisArg, this[i], i, this);
28+
}
29+
};
30+
}
31+
32+
// simple httprequest
33+
function httpGetAsync(theUrl, callback) {
34+
var xmlHttp = new XMLHttpRequest();
35+
xmlHttp.onreadystatechange = function() {
36+
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
37+
callback(xmlHttp.responseText);
38+
};
39+
xmlHttp.open('GET', theUrl, true); // true for asynchronous
40+
xmlHttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
41+
xmlHttp.send(null);
42+
}
43+
44+
function setToken(token) {
45+
document
46+
.querySelectorAll('{$selector}')
47+
.forEach(function(item) {
48+
item.value = token;
49+
});
50+
}
51+
52+
function updateToken() {
53+
httpGetAsync('{$route}', setToken);
54+
}
55+
56+
updateToken();
57+
58+
setInterval(updateToken, 15 * 60 * 1000); // Every 15 minutes.
59+
}
60+
</script>";
61+
}
62+
}

DynamicToken/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "mykolas-mankevicius/dynamic-token",
3+
"require": {
4+
}
5+
}

DynamicToken/meta.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: 'Dynamic Token'
2+
version: '1.0'
3+
description: 'With {{ dynamic_token }} tag, adds dynamic crsf tokens to all forms which contain input name="_token" effectivelly providing dynamic forms in static pages'
4+
developer: 'Mykolas Mankevicius'
5+
developer_url: 'https://www.co-openhagen.com/'
6+
commercial: true

README.MD

Whitespace-only changes.

0 commit comments

Comments
 (0)