forked from vidar-team/hctf_backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add jwt auth && fix the team datatable
- Loading branch information
Showing
21 changed files
with
373 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,6 @@ Homestead.yaml | |
npm-debug.log | ||
yarn-error.log | ||
.env | ||
package-lock.json | ||
composer.lock | ||
*~ |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Http\Request; | ||
use App\Team; | ||
use Mockery\Exception; | ||
use APIReturn; | ||
use JWTAuth; | ||
use Tymon\JWTAuth\Exceptions\JWTException; | ||
|
||
class TeamController extends Controller | ||
{ | ||
private $team; | ||
public function __construct(Team $team) { | ||
$this->team = $team; | ||
} | ||
|
||
public function login(Request $request) | ||
{ | ||
$credentials = $request->only('email', 'password'); | ||
$access_token = null; | ||
|
||
try { | ||
if (!$access_token = JWTAuth::attempt($credentials)) { | ||
return APIReturn::error(401, ['invalid_email_or_password'], 401); | ||
} | ||
} catch (JWTAuthException $err) { | ||
return APIReturn::error(500, ['failed_to_create_token'], 500); | ||
} | ||
return response()->json(compact('access_token')); | ||
} | ||
|
||
public function register(Request $request) { | ||
$input = $request->only('teamName', 'email', 'password'); | ||
try { | ||
$team = $this->team->create([ | ||
'teamName' => $input['teamName'], | ||
'email' => $input['email'], | ||
'password' => bcrypt($input['password']), | ||
'signUpTime' => Carbon::now('Asia/Shanghai'), | ||
'lastLoginTime' => Carbon::now('Asia/Shanghai'), | ||
]); | ||
} catch (Exception $err) { | ||
return APIReturn::error(500, ['msg' => 'Team/Email already exists.'], 500); | ||
} | ||
|
||
return APIReturn::success([ | ||
'msg' => 'Welcome to HCTF 2017!', | ||
]); | ||
} | ||
|
||
public function getAuthInfo(Request $request) { | ||
$team = JWTAuth::parseToken()->authenticate(); | ||
return APIReturn::success(['team' => $team]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use Tymon\JWTAuth\Exceptions\TokenExpiredException; | ||
use Tymon\JWTAuth\Exceptions\TokenInvalidException; | ||
use JWTAuth; | ||
use Tymon\JWTAuth\Exceptions\JWTException; | ||
|
||
|
||
class VerifyJWTToken | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
if (! $token = $this->auth->setRequest($request)->getToken()) { | ||
return $this->respond('tymon.jwt.absent', 'token_not_provided', 400); | ||
} | ||
|
||
try { | ||
$user = $this->auth->authenticate($token); | ||
} catch (TokenExpiredException $err) { | ||
return $this->respond('Token expired', 'token_expired', $err->getStatusCode(), [$err]); | ||
} catch (TokenInvalidException $err) { | ||
return $this->respond('Token invalid', 'token_invalid', $err->getStatusCode(), [$err]); | ||
} | ||
|
||
if (! $user) { | ||
return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404); | ||
} | ||
|
||
$this->events->fire('tymon.jwt.valid', $user); | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Notifications\Notifiable; | ||
use Illuminate\Foundation\Auth\User as Authenticatable; | ||
|
||
class Team extends Authenticatable | ||
{ | ||
use Notifiable; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
| id | int(10) unsigned | NO | PRI | <null> | auto_increment | | ||
| teamName | varchar(255) | NO | | <null> | | | ||
| email | varchar(255) | YES | UNI | <null> | | | ||
| password | varchar(255) | NO | | <null> | | | ||
| signUpTime | datetime | NO | | <null> | | | ||
| lastLoginTime | datetime | NO | | <null> | | | ||
| score | decimal(8,2) | NO | | 0.00 | | | ||
| banned | tinyint(1) | NO | | 0 | | | ||
| remember_token | varchar(100) | YES | | <null> | | | ||
* | ||
*/ | ||
protected $fillable = [ | ||
'teamName', 'email', 'password', 'signUpTime', 'lastLoginTime' | ||
]; | ||
|
||
/** | ||
* The attributes that should be hidden for arrays. | ||
* | ||
* @var array | ||
*/ | ||
protected $hidden = [ | ||
'password', 'remember_token', | ||
]; | ||
|
||
public $timestamps = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,4 @@ class User extends Authenticatable | |
'password', 'remember_token', | ||
]; | ||
} | ||
|
Oops, something went wrong.