Skip to content

Commit ad69193

Browse files
committed
Add JWT to login forms
1 parent 47c1ecc commit ad69193

File tree

8 files changed

+93
-6
lines changed

8 files changed

+93
-6
lines changed

app/Http/Controllers/Auth/LoginController.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Pterodactyl\Http\Controllers\Auth;
44

5+
use Lcobucci\JWT\Builder;
56
use Illuminate\Http\Request;
67
use Illuminate\Http\JsonResponse;
78
use Illuminate\Contracts\View\View;
9+
use Lcobucci\JWT\Signer\Hmac\Sha256;
810
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
911

1012
class LoginController extends AbstractLoginController
@@ -63,11 +65,26 @@ public function login(Request $request): JsonResponse
6365
'request_ip' => $request->ip(),
6466
], 5);
6567

66-
return response()->json(['complete' => false, 'token' => $token]);
68+
return response()->json(['complete' => false, 'login_token' => $token]);
6769
}
6870

71+
$signer = new Sha256();
72+
$token = (new Builder)->setIssuer('http://pterodactyl.local')
73+
->setAudience('http://pterodactyl.local')
74+
->setId(str_random(12), true)
75+
->setIssuedAt(time())
76+
->setNotBefore(time())
77+
->setExpiration(time() + 3600)
78+
->set('uid', $user->id)
79+
->sign($signer, env('APP_JWT_KEY'))
80+
->getToken();
81+
6982
$this->auth->guard()->login($user, true);
7083

71-
return response()->json(['complete' => true]);
84+
return response()->json([
85+
'complete' => true,
86+
'intended' => $this->redirectPath(),
87+
'token' => $token->__toString(),
88+
]);
7289
}
7390
}

app/Http/Middleware/Api/AuthenticateKey.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function handle(Request $request, Closure $next, int $keyType)
6363
}
6464

6565
$raw = $request->bearerToken();
66+
6667
$identifier = substr($raw, 0, ApiKey::IDENTIFIER_LENGTH);
6768
$token = substr($raw, ApiKey::IDENTIFIER_LENGTH);
6869

resources/assets/scripts/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import { Ziggy } from './helpers/ziggy';
88
import Locales from './../../../resources/lang/locales';
99
import { flash } from './mixins/flash';
1010
import { routes } from './routes';
11+
import { storeData } from './store';
1112

1213
window.events = new Vue;
1314
window.Ziggy = Ziggy;
1415

1516
Vue.use(Vuex);
16-
17-
const store = new Vuex.Store();
17+
const store = new Vuex.Store(storeData);
1818
const route = require('./../../../vendor/tightenco/ziggy/src/js/route').default;
1919

2020
Vue.config.productionTip = false;

resources/assets/scripts/bootstrap.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ try {
1919
window.axios = require('axios');
2020

2121
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
22+
window.axios.defaults.headers.common['Authorization'] = 'Bearer ' + localStorage.token || '';
2223

2324
/**
2425
* Next we will register the CSRF Token as a common header with Axios so that

resources/assets/scripts/components/auth/LoginForm.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,20 @@
8282
})
8383
.then(function (response) {
8484
if (response.data.complete) {
85-
return window.location = '/';
85+
localStorage.setItem('token', response.data.token);
86+
self.$store.dispatch('login');
87+
return window.location = response.data.intended;
8688
}
8789
8890
self.$props.user.password = '';
8991
self.$data.showSpinner = false;
90-
self.$router.push({name: 'checkpoint', query: {token: response.data.token}});
92+
self.$router.push({name: 'checkpoint', query: {token: response.data.login_token}});
9193
})
9294
.catch(function (err) {
9395
self.$props.user.password = '';
9496
self.$data.showSpinner = false;
97+
self.$store.dispatch('logout');
98+
9599
if (!err.response) {
96100
return console.error(err);
97101
}

resources/assets/scripts/components/auth/TwoFactorForm.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@
4949
authentication_code: this.$data.code,
5050
})
5151
.then(function (response) {
52+
localStorage.setItem('token', response.data.token);
53+
self.$store.dispatch('login');
5254
window.location = response.data.intended;
5355
})
5456
.catch(function (err) {
57+
self.$store.dispatch('logout');
5558
if (!err.response) {
5659
return console.error(err);
5760
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import JwtDecode from 'jwt-decode';
2+
3+
const User = function () {
4+
this.id = 0;
5+
this.admin = false;
6+
this.email = '';
7+
};
8+
9+
/**
10+
* Return a new instance of the user model using a JWT.
11+
*
12+
* @param {string} token
13+
* @returns {User}
14+
*/
15+
User.prototype.fromJwt = function (token) {
16+
return this.newModel(JwtDecode(token));
17+
};
18+
19+
/**
20+
* Return an instance of this user model with the properties set on it.
21+
*
22+
* @param {object} obj
23+
* @returns {User}
24+
*/
25+
User.prototype.newModel = function (obj) {
26+
this.id = obj.id;
27+
this.admin = obj.admin;
28+
this.email = obj.email;
29+
30+
return this;
31+
};
32+
33+
export default User;

resources/assets/scripts/store.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import User from './models/user';
2+
3+
export const storeData = {
4+
state: {
5+
user: null,
6+
},
7+
actions: {
8+
login: function ({ commit }) {
9+
commit('login');
10+
},
11+
logout: function ({ commit }) {
12+
commit('logout');
13+
},
14+
},
15+
getters: {
16+
user: function (state) {
17+
return state.user;
18+
},
19+
},
20+
mutations: {
21+
login: function (state) {
22+
state.user = new User().fromJwt(localStorage.getItem('token'));
23+
},
24+
logout: function (state) {
25+
state.user = null;
26+
}
27+
}
28+
};

0 commit comments

Comments
 (0)