Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| TinyDriveTokenController | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 1 |
| getToken | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\v1; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use Firebase\JWT\JWT; |
| 7 | use Illuminate\Http\JsonResponse; |
| 8 | use Illuminate\Http\Request; |
| 9 | use Illuminate\Support\Facades\Storage; |
| 10 | |
| 11 | class TinyDriveTokenController extends Controller |
| 12 | { |
| 13 | public function getToken(Request $request): JsonResponse |
| 14 | { |
| 15 | $user = $request->user(); |
| 16 | $privateKey = Storage::get('jwt_private_key.pem'); |
| 17 | if (! $privateKey) { |
| 18 | return response()->json(['token' => null, 'error' => 'privateKey is null or cannot be loaded']); |
| 19 | } |
| 20 | $payload = [ |
| 21 | 'sub' => $user->id, // unique user id string |
| 22 | 'name' => $user->first_name.' '.$user->last_name, // full name of user |
| 23 | // Optional custom user root path |
| 24 | // When this is set the user will only be able to manage and see files in the specified root |
| 25 | // directory. This makes it possible to have a dedicated home directory for each user. |
| 26 | 'https://claims.tiny.cloud/drive/root' => '/'.$user->id, |
| 27 | 'exp' => time() + 60 * 30, // 30 minute expiration |
| 28 | ]; |
| 29 | $token = JWT::encode($payload, $privateKey, 'RS256'); |
| 30 | |
| 31 | return response()->json(['token' => $token]); |
| 32 | } |
| 33 | } |