auth
Showing
11 changed files
with
265 additions
and
71 deletions
| ... | @@ -159,15 +159,6 @@ class ApiController extends Controller | ... | @@ -159,15 +159,6 @@ class ApiController extends Controller |
| 159 | 159 | ||
| 160 | } | 160 | } |
| 161 | 161 | ||
| 162 | public function getApi($json) | ||
| 163 | { | ||
| 164 | $file = Storage::disk('public')->get('interface.js'); | ||
| 165 | return $file; | ||
| 166 | |||
| 167 | return (new Response($file, 200)) | ||
| 168 | ->header('Content-Type', 'text/html'); | ||
| 169 | } | ||
| 170 | |||
| 171 | 162 | ||
| 172 | 163 | ||
| 173 | public function getPdf($json) | 164 | public function getPdf($json) | ... | ... |
| 1 | <?php namespace App\Http\Controllers; | ||
| 2 | |||
| 3 | use JWTAuth; | ||
| 4 | use Tymon\JWTAuth\Exceptions\JWTException; | ||
| 5 | use Illuminate\Http\Request; | ||
| 6 | use App\User; | ||
| 7 | |||
| 8 | |||
| 9 | class AuthenticateController extends Controller | ||
| 10 | { | ||
| 11 | public function authenticate(Request $request) | ||
| 12 | { | ||
| 13 | // grab credentials from the request | ||
| 14 | //return User::create(['email' => 'jeffmbalicki@gmail.com', 'password' => bcrypt('518862')]); | ||
| 15 | $credentials = $request->only('email', 'password'); | ||
| 16 | |||
| 17 | try { | ||
| 18 | // attempt to verify the credentials and create a token for the user | ||
| 19 | if (! $token = JWTAuth::attempt($credentials)) { | ||
| 20 | return response()->json(['error' => 'invalid_credentials'], 401); | ||
| 21 | } | ||
| 22 | } catch (JWTException $e) { | ||
| 23 | // something went wrong whilst attempting to encode the token | ||
| 24 | return response()->json(['error' => 'could_not_create_token'], 500); | ||
| 25 | } | ||
| 26 | |||
| 27 | // all good so return the token | ||
| 28 | return response()->json(compact('token')); | ||
| 29 | } | ||
| 30 | public function getAuthenticatedUser() | ||
| 31 | { | ||
| 32 | try { | ||
| 33 | |||
| 34 | if (! $user = JWTAuth::parseToken()->authenticate()) { | ||
| 35 | return response()->json(['user_not_found'], 404); | ||
| 36 | } | ||
| 37 | |||
| 38 | } catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) { | ||
| 39 | |||
| 40 | return response()->json(['token_expired'], $e->getStatusCode()); | ||
| 41 | |||
| 42 | } catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) { | ||
| 43 | |||
| 44 | return response()->json(['token_invalid'], $e->getStatusCode()); | ||
| 45 | |||
| 46 | } catch (Tymon\JWTAuth\Exceptions\JWTException $e) { | ||
| 47 | |||
| 48 | return response()->json(['token_absent'], $e->getStatusCode()); | ||
| 49 | |||
| 50 | } | ||
| 51 | |||
| 52 | // the token is valid and we have found the user via the sub claim | ||
| 53 | return response()->json(compact('user')); | ||
| 54 | } | ||
| 55 | } | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -69,40 +69,7 @@ class FileEntryController extends Controller { | ... | @@ -69,40 +69,7 @@ class FileEntryController extends Controller { |
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | 71 | ||
| 72 | public function postUpload(){ | 72 | |
| 73 | |||
| 74 | $files = Input::file('files'); | ||
| 75 | |||
| 76 | $json = array( | ||
| 77 | 'files' => array() | ||
| 78 | ); | ||
| 79 | |||
| 80 | foreach( $files as $file ): | ||
| 81 | |||
| 82 | $filename = $file->getClientOriginalName().".".$file->getClientOriginalExtension(); | ||
| 83 | |||
| 84 | $json['files'][] = array( | ||
| 85 | 'name' => $filename, | ||
| 86 | 'size' => $file->getSize(), | ||
| 87 | 'type' => $file->getMimeType(), | ||
| 88 | 'url' => '/uploads/files/'.$filename, | ||
| 89 | 'deleteType' => 'DELETE', | ||
| 90 | 'deleteUrl' => self::$route.'/deleteFile/'.$filename, | ||
| 91 | ); | ||
| 92 | |||
| 93 | $upload = $file->move( public_path().'/files', $filename ); | ||
| 94 | |||
| 95 | |||
| 96 | endforeach; | ||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | return Response::json($json); | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | 73 | ||
| 107 | 74 | ||
| 108 | 75 | ||
| ... | @@ -113,7 +80,7 @@ class FileEntryController extends Controller { | ... | @@ -113,7 +80,7 @@ class FileEntryController extends Controller { |
| 113 | $file = Storage::disk('public')->get('js/interface.js'); | 80 | $file = Storage::disk('public')->get('js/interface.js'); |
| 114 | 81 | ||
| 115 | return (new Response($file, 200)) | 82 | return (new Response($file, 200)) |
| 116 | ->header('Content-Type', 'text/html'); | 83 | ->header('Content-Type', 'application/x-javascript'); |
| 117 | } | 84 | } |
| 118 | 85 | ||
| 119 | } | 86 | } | ... | ... |
| ... | @@ -28,7 +28,8 @@ class Kernel extends HttpKernel | ... | @@ -28,7 +28,8 @@ class Kernel extends HttpKernel |
| 28 | \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, | 28 | \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, |
| 29 | \Illuminate\Session\Middleware\StartSession::class, | 29 | \Illuminate\Session\Middleware\StartSession::class, |
| 30 | \Illuminate\View\Middleware\ShareErrorsFromSession::class, | 30 | \Illuminate\View\Middleware\ShareErrorsFromSession::class, |
| 31 | \App\Http\Middleware\VerifyCsrfToken::class, | 31 | // \App\Http\Middleware\VerifyCsrfToken::class, |
| 32 | |||
| 32 | ], | 33 | ], |
| 33 | 34 | ||
| 34 | 'api' => [ | 35 | 'api' => [ |
| ... | @@ -50,5 +51,7 @@ class Kernel extends HttpKernel | ... | @@ -50,5 +51,7 @@ class Kernel extends HttpKernel |
| 50 | 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, | 51 | 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, |
| 51 | 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, | 52 | 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, |
| 52 | 'cors' => \App\Http\Middleware\Cors::class, | 53 | 'cors' => \App\Http\Middleware\Cors::class, |
| 54 | 'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class, | ||
| 55 | 'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class | ||
| 53 | ]; | 56 | ]; |
| 54 | } | 57 | } | ... | ... |
| ... | @@ -11,7 +11,7 @@ class VerifyCsrfToken extends BaseVerifier | ... | @@ -11,7 +11,7 @@ class VerifyCsrfToken extends BaseVerifier |
| 11 | * | 11 | * |
| 12 | * @var array | 12 | * @var array |
| 13 | */ | 13 | */ |
| 14 | protected $except = [ 'api/update' | 14 | protected $except = [ 'api/update', 'api/authenticate' |
| 15 | // | 15 | // |
| 16 | ]; | 16 | ]; |
| 17 | } | 17 | } | ... | ... |
| ... | @@ -29,10 +29,14 @@ Route::group(array('prefix' => 'api/v1'), function($json) | ... | @@ -29,10 +29,14 @@ Route::group(array('prefix' => 'api/v1'), function($json) |
| 29 | Route::resource('get-list', 'ApiController@getList'); | 29 | Route::resource('get-list', 'ApiController@getList'); |
| 30 | Route::resource('get-pdf', 'ApiController@getpdf'); | 30 | Route::resource('get-pdf', 'ApiController@getpdf'); |
| 31 | Route::resource('api', 'FileEntryController@getApi'); | 31 | Route::resource('api', 'FileEntryController@getApi'); |
| 32 | |||
| 32 | 33 | ||
| 33 | }); | 34 | }); |
| 34 | 35 | ||
| 35 | Route::any('api/update', 'ApiController@update'); | 36 | Route::any('api/update', 'ApiController@update'); |
| 37 | Route::post('api/authenticate', 'AuthenticateController@authenticate'); | ||
| 38 | Route::get('api/authenticate/user', 'AuthenticateController@getAuthenticatedUser'); | ||
| 39 | |||
| 36 | 40 | ||
| 37 | Route::any('fileentry/postUpload', 'FileEntryController@postUpload'); | 41 | Route::any('fileentry/postUpload', 'FileEntryController@postUpload'); |
| 38 | 42 | ... | ... |
| 1 | <?php | 1 | <? |
| 2 | |||
| 3 | namespace App; | 2 | namespace App; |
| 3 | use Illuminate\Auth\Authenticatable; use Illuminate\Database\Eloquent\Model; use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Foundation\Auth\Access\Authorizable; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; | ||
| 4 | class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract { use Authenticatable, Authorizable, CanResetPassword; | ||
| 5 | /** | ||
| 6 | * The database table used by the model. | ||
| 7 | * | ||
| 8 | * @var string | ||
| 9 | */ | ||
| 10 | protected $table = 'users'; | ||
| 4 | 11 | ||
| 5 | use Illuminate\Foundation\Auth\User as Authenticatable; | 12 | /** |
| 6 | 13 | * The attributes that are mass assignable. | |
| 7 | class User extends Authenticatable | 14 | * |
| 8 | { | 15 | * @var array |
| 9 | /** | 16 | */ |
| 10 | * The attributes that are mass assignable. | 17 | protected $fillable = ['first_name', 'last_name', 'username', 'email', 'password']; |
| 11 | * | 18 | /** |
| 12 | * @var array | 19 | * The attributes excluded from the model's JSON form. |
| 13 | */ | 20 | * |
| 14 | protected $fillable = [ | 21 | * @var array |
| 15 | 'name', 'email', 'password', | 22 | */ |
| 16 | ]; | 23 | protected $hidden = ['password']; |
| 17 | 24 | } | |
| 18 | /** | ||
| 19 | * The attributes that should be hidden for arrays. | ||
| 20 | * | ||
| 21 | * @var array | ||
| 22 | */ | ||
| 23 | protected $hidden = [ | ||
| 24 | 'password', 'remember_token', | ||
| 25 | ]; | ||
| 26 | } | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -147,7 +147,7 @@ return [ | ... | @@ -147,7 +147,7 @@ return [ |
| 147 | Illuminate\Translation\TranslationServiceProvider::class, | 147 | Illuminate\Translation\TranslationServiceProvider::class, |
| 148 | Illuminate\Validation\ValidationServiceProvider::class, | 148 | Illuminate\Validation\ValidationServiceProvider::class, |
| 149 | Illuminate\View\ViewServiceProvider::class, | 149 | Illuminate\View\ViewServiceProvider::class, |
| 150 | 150 | Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class, | |
| 151 | /* | 151 | /* |
| 152 | * Application Service Providers... | 152 | * Application Service Providers... |
| 153 | */ | 153 | */ |
| ... | @@ -201,6 +201,7 @@ return [ | ... | @@ -201,6 +201,7 @@ return [ |
| 201 | 'URL' => Illuminate\Support\Facades\URL::class, | 201 | 'URL' => Illuminate\Support\Facades\URL::class, |
| 202 | 'Validator' => Illuminate\Support\Facades\Validator::class, | 202 | 'Validator' => Illuminate\Support\Facades\Validator::class, |
| 203 | 'View' => Illuminate\Support\Facades\View::class, | 203 | 'View' => Illuminate\Support\Facades\View::class, |
| 204 | 'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class, | ||
| 204 | 205 | ||
| 205 | ], | 206 | ], |
| 206 | 207 | ... | ... |
config/jwt.php
0 → 100644
| 1 | <?php | ||
| 2 | |||
| 3 | /* | ||
| 4 | * This file is part of jwt-auth. | ||
| 5 | * | ||
| 6 | * (c) Sean Tymon <tymon148@gmail.com> | ||
| 7 | * | ||
| 8 | * For the full copyright and license information, please view the LICENSE | ||
| 9 | * file that was distributed with this source code. | ||
| 10 | */ | ||
| 11 | |||
| 12 | return [ | ||
| 13 | |||
| 14 | /* | ||
| 15 | |-------------------------------------------------------------------------- | ||
| 16 | | JWT Authentication Secret | ||
| 17 | |-------------------------------------------------------------------------- | ||
| 18 | | | ||
| 19 | | Don't forget to set this, as it will be used to sign your tokens. | ||
| 20 | | A helper command is provided for this: `php artisan jwt:generate` | ||
| 21 | | | ||
| 22 | */ | ||
| 23 | |||
| 24 | 'secret' => env('JWT_SECRET', 'O1RUwmBjmDjsYJDpAgGB5GZLwnhDeGqS'), | ||
| 25 | |||
| 26 | /* | ||
| 27 | |-------------------------------------------------------------------------- | ||
| 28 | | JWT time to live | ||
| 29 | |-------------------------------------------------------------------------- | ||
| 30 | | | ||
| 31 | | Specify the length of time (in minutes) that the token will be valid for. | ||
| 32 | | Defaults to 1 hour | ||
| 33 | | | ||
| 34 | */ | ||
| 35 | |||
| 36 | 'ttl' => 60, | ||
| 37 | |||
| 38 | /* | ||
| 39 | |-------------------------------------------------------------------------- | ||
| 40 | | Refresh time to live | ||
| 41 | |-------------------------------------------------------------------------- | ||
| 42 | | | ||
| 43 | | Specify the length of time (in minutes) that the token can be refreshed | ||
| 44 | | within. I.E. The user can refresh their token within a 2 week window of | ||
| 45 | | the original token being created until they must re-authenticate. | ||
| 46 | | Defaults to 2 weeks | ||
| 47 | | | ||
| 48 | */ | ||
| 49 | |||
| 50 | 'refresh_ttl' => 20160, | ||
| 51 | |||
| 52 | /* | ||
| 53 | |-------------------------------------------------------------------------- | ||
| 54 | | JWT hashing algorithm | ||
| 55 | |-------------------------------------------------------------------------- | ||
| 56 | | | ||
| 57 | | Specify the hashing algorithm that will be used to sign the token. | ||
| 58 | | | ||
| 59 | | See here: https://github.com/namshi/jose/tree/2.2.0/src/Namshi/JOSE/Signer | ||
| 60 | | for possible values | ||
| 61 | | | ||
| 62 | */ | ||
| 63 | |||
| 64 | 'algo' => 'HS256', | ||
| 65 | |||
| 66 | /* | ||
| 67 | |-------------------------------------------------------------------------- | ||
| 68 | | User Model namespace | ||
| 69 | |-------------------------------------------------------------------------- | ||
| 70 | | | ||
| 71 | | Specify the full namespace to your User model. | ||
| 72 | | e.g. 'Acme\Entities\User' | ||
| 73 | | | ||
| 74 | */ | ||
| 75 | |||
| 76 | 'user' => 'App\User', | ||
| 77 | |||
| 78 | /* | ||
| 79 | |-------------------------------------------------------------------------- | ||
| 80 | | User identifier | ||
| 81 | |-------------------------------------------------------------------------- | ||
| 82 | | | ||
| 83 | | Specify a unique property of the user that will be added as the 'sub' | ||
| 84 | | claim of the token payload. | ||
| 85 | | | ||
| 86 | */ | ||
| 87 | |||
| 88 | 'identifier' => 'userid', | ||
| 89 | |||
| 90 | /* | ||
| 91 | |-------------------------------------------------------------------------- | ||
| 92 | | Required Claims | ||
| 93 | |-------------------------------------------------------------------------- | ||
| 94 | | | ||
| 95 | | Specify the required claims that must exist in any token. | ||
| 96 | | A TokenInvalidException will be thrown if any of these claims are not | ||
| 97 | | present in the payload. | ||
| 98 | | | ||
| 99 | */ | ||
| 100 | |||
| 101 | 'required_claims' => ['iss', 'iat', 'exp', 'nbf', 'sub', 'jti'], | ||
| 102 | |||
| 103 | /* | ||
| 104 | |-------------------------------------------------------------------------- | ||
| 105 | | Blacklist Enabled | ||
| 106 | |-------------------------------------------------------------------------- | ||
| 107 | | | ||
| 108 | | In order to invalidate tokens, you must have the the blacklist enabled. | ||
| 109 | | If you do not want or need this functionality, then set this to false. | ||
| 110 | | | ||
| 111 | */ | ||
| 112 | |||
| 113 | 'blacklist_enabled' => env('JWT_BLACKLIST_ENABLED', true), | ||
| 114 | |||
| 115 | /* | ||
| 116 | |-------------------------------------------------------------------------- | ||
| 117 | | Providers | ||
| 118 | |-------------------------------------------------------------------------- | ||
| 119 | | | ||
| 120 | | Specify the various providers used throughout the package. | ||
| 121 | | | ||
| 122 | */ | ||
| 123 | |||
| 124 | 'providers' => [ | ||
| 125 | |||
| 126 | /* | ||
| 127 | |-------------------------------------------------------------------------- | ||
| 128 | | User Provider | ||
| 129 | |-------------------------------------------------------------------------- | ||
| 130 | | | ||
| 131 | | Specify the provider that is used to find the user based | ||
| 132 | | on the subject claim | ||
| 133 | | | ||
| 134 | */ | ||
| 135 | |||
| 136 | 'user' => 'Tymon\JWTAuth\Providers\User\EloquentUserAdapter', | ||
| 137 | |||
| 138 | /* | ||
| 139 | |-------------------------------------------------------------------------- | ||
| 140 | | JWT Provider | ||
| 141 | |-------------------------------------------------------------------------- | ||
| 142 | | | ||
| 143 | | Specify the provider that is used to create and decode the tokens. | ||
| 144 | | | ||
| 145 | */ | ||
| 146 | |||
| 147 | 'jwt' => 'Tymon\JWTAuth\Providers\JWT\NamshiAdapter', | ||
| 148 | |||
| 149 | /* | ||
| 150 | |-------------------------------------------------------------------------- | ||
| 151 | | Authentication Provider | ||
| 152 | |-------------------------------------------------------------------------- | ||
| 153 | | | ||
| 154 | | Specify the provider that is used to authenticate users. | ||
| 155 | | | ||
| 156 | */ | ||
| 157 | |||
| 158 | 'auth' => 'Tymon\JWTAuth\Providers\Auth\IlluminateAuthAdapter', | ||
| 159 | |||
| 160 | /* | ||
| 161 | |-------------------------------------------------------------------------- | ||
| 162 | | Storage Provider | ||
| 163 | |-------------------------------------------------------------------------- | ||
| 164 | | | ||
| 165 | | Specify the provider that is used to store tokens in the blacklist | ||
| 166 | | | ||
| 167 | */ | ||
| 168 | |||
| 169 | 'storage' => 'Tymon\JWTAuth\Providers\Storage\IlluminateCacheAdapter', | ||
| 170 | |||
| 171 | ], | ||
| 172 | |||
| 173 | ]; |
| ... | @@ -15,6 +15,8 @@ | ... | @@ -15,6 +15,8 @@ |
| 15 | RewriteCond %{REQUEST_FILENAME} !-f | 15 | RewriteCond %{REQUEST_FILENAME} !-f |
| 16 | RewriteRule ^ index.php [L] | 16 | RewriteRule ^ index.php [L] |
| 17 | 17 | ||
| 18 | RewriteCond %{HTTP:Authorization} ^(.*) | ||
| 19 | RewriteRule .* - [e=HTTP_AUTHORIZATION:%1] | ||
| 18 | # Handle Authorization Header | 20 | # Handle Authorization Header |
| 19 | 21 | ||
| 20 | </IfModule> | 22 | </IfModule> | ... | ... |
public/files/js/interface.js
0 → 100644
This diff is collapsed.
Click to expand it.
-
Please register or sign in to post a comment