diff --git a/.env b/.env deleted file mode 100644 index bb73053..0000000 --- a/.env +++ /dev/null @@ -1,22 +0,0 @@ -APP_ENV=local -APP_DEBUG=true -APP_KEY=F8Lj#2v%!@$ku6FXrTBscBSs^O$VOvus - -APP_LOCALE=en -APP_FALLBACK_LOCALE=en - -# DB_CONNECTION=mysql -# DB_HOST=localhost -# DB_PORT=3306 -# DB_DATABASE=homestead -# DB_USERNAME=homestead -# DB_PASSWORD=secret - -CACHE_DRIVER=file -SESSION_DRIVER=file -QUEUE_DRIVER=file - -VERSION=2.0.0 Alpha 1 -VERSION_RELMONTH=January -VERSION_RELDAY=18 -VERSION_RELYEAR=2016 diff --git a/.gitignore b/.gitignore index ab95405..e7cac24 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,7 @@ #/vendor bootstrap/cache/ storage/ -env.*.php env -.env.php .env .env.bak .env.example diff --git a/app/Factories/UserFactory.php b/app/Factories/UserFactory.php index 480c3c0..ae75a91 100644 --- a/app/Factories/UserFactory.php +++ b/app/Factories/UserFactory.php @@ -5,8 +5,8 @@ use App\Models\User; use App\Helpers\CryptoHelper; -class UserFactory { - public static function createUser($username, $email, $password, $active=0, $ip='127.0.0.1') { +class UserFactory { + public static function createUser($username, $email, $password, $active=0, $ip='127.0.0.1', $api_key=false, $api_active=0) { $hashed_password = Hash::make($password); $recovery_key = CryptoHelper::generateRandomHex(50); @@ -17,6 +17,10 @@ public static function createUser($username, $email, $password, $active=0, $ip=' $user->recovery_key = $recovery_key; $user->active = $active; $user->ip = $ip; + + $user->api_key = $api_key; + $user->api_active = $api_active; + $user->save(); return $user; diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index 60c3d91..65cd2ac 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -29,6 +29,12 @@ public function displayAdminPage(Request $request) { $admin_links = Link::paginate(15); } + $user = UserHelper::getUserByUsername($username); + + if (!$user) { + return redirect(route('index'))->with('error', 'Invalid or disabled account.'); + } + $user_links = Link::where('creator', $username) ->paginate(15); @@ -36,7 +42,10 @@ public function displayAdminPage(Request $request) { 'role' => $role, 'admin_users' => $admin_users, 'admin_links' => $admin_links, - 'user_links' => $user_links + 'user_links' => $user_links, + 'api_key' => $user->api_key, + 'api_active' => $user->api_active, + 'api_quota' => $user->api_quota ]); } diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php index 9f246c5..ecf7f44 100644 --- a/app/Http/Controllers/AjaxController.php +++ b/app/Http/Controllers/AjaxController.php @@ -63,7 +63,7 @@ public function generateNewAPIKey(Request $request) { abort(404, 'User not found.'); } - $new_api_key = CryptoHelper::generateRandomHex(15); + $new_api_key = CryptoHelper::generateRandomHex(env('_API_KEY_LENGTH')); $user->api_key = $new_api_key; $user->save(); diff --git a/app/Http/Controllers/Api/ApiController.php b/app/Http/Controllers/Api/ApiController.php index 6cf7600..dc42f34 100644 --- a/app/Http/Controllers/Api/ApiController.php +++ b/app/Http/Controllers/Api/ApiController.php @@ -10,16 +10,32 @@ class ApiController extends Controller { protected static function getApiUserInfo(Request $request) { $api_key = $request->input('key'); - $user = User::where('active', 1) - ->where('api_key', $api_key) - ->where('api_active', 1) - ->first(); - if (!$user) { - abort(401, "Invalid authentication token."); + if (!$api_key) { + // no API key provided -- check whether anonymous API is on + if (env('SETTING_ANON_API') == 'on') { + $username = 'ANONIP-' . $request->ip(); + } + else { + abort(401, "Authentication token required."); + } + $user = (object) [ + 'username' => $username + ]; + } + else { + $user = User::where('active', 1) + ->where('api_key', $api_key) + ->where('api_active', 1) + ->first(); + + if (!$user) { + abort(401, "Invalid authentication token."); + } + $username = $user->username; } - $api_limit_reached = ApiHelper::checkUserApiQuota($user->username); + $api_limit_reached = ApiHelper::checkUserApiQuota($username); if ($api_limit_reached) { abort(403, "Quota exceeded."); diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index 048ddf8..8ab78f6 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -102,6 +102,9 @@ public static function performSetup(Request $request) { $st_base = $request->input('setting:base'); + $st_auto_api_key = $request->input('setting:auto_api_key'); + $st_anon_api = $request->input('setting:anon_api'); + $mail_host = $request->input('app:smtp_server'); $mail_port = $request->input('app:smtp_port'); $mail_username = $request->input('app:smtp_username'); @@ -147,6 +150,8 @@ public static function performSetup(Request $request) { 'MAIL_FROM_NAME' => $mail_from_name, 'ST_BASE' => $st_base, + 'ST_AUTO_API' => $st_auto_api_key, + 'ST_ANON_API' => $st_anon_api ])->render(); $handle = fopen('../.env', 'w'); diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 795b325..4a5e15a 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -3,7 +3,10 @@ use Mail; use App\Models\User; use Illuminate\Http\Request; + +use App\Helpers\CryptoHelper; use App\Helpers\UserHelper; + use App\Factories\UserFactory; class UserController extends Controller { @@ -91,8 +94,17 @@ public function performSignup(Request $request) { $response = redirect(route('login'))->with('success', 'Thanks for signing up! Please confirm your email to continue..'); $active = 0; } - $user = UserFactory::createUser($username, $email, $password, $active, $ip); + $api_active = false; + $api_key = null; + if (env('SETTING_AUTO_API') == 'on') { + // if automatic API key assignment is on + $api_active = 1; + $api_key = CryptoHelper::generateRandomHex(env('_API_KEY_LENGTH')); + } + + + $user = UserFactory::createUser($username, $email, $password, $active, $ip, $api_key, $api_active); return $response; } diff --git a/database/migrations/2015_11_04_015823_create_users_table.php b/database/migrations/2015_11_04_015823_create_users_table.php index 61d7ce4..285016d 100644 --- a/database/migrations/2015_11_04_015823_create_users_table.php +++ b/database/migrations/2015_11_04_015823_create_users_table.php @@ -26,7 +26,7 @@ public function up() $table->string('role'); $table->string('active'); - $table->string('api_key'); + $table->string('api_key')->nullable(); $table->boolean('api_active')->default(0); $table->string('api_quota')->default(60); diff --git a/public/css/admin.css b/public/css/admin.css index 220e085..a6cd537 100644 --- a/public/css/admin.css +++ b/public/css/admin.css @@ -18,3 +18,7 @@ .hidden-metadata { display: none; } + +.api-quota { + display: inline; +} diff --git a/public/css/setup.css b/public/css/setup.css index c4dc5c5..b6119ba 100644 --- a/public/css/setup.css +++ b/public/css/setup.css @@ -15,7 +15,7 @@ } body { - background-size: 100%; + background-size: 100% 100%; background-attachment: fixed; background-position: center; background-repeat: no-repeat; @@ -61,6 +61,10 @@ body { color: grey; } +.footer-well { + margin-top: 30px; +} + h4, p { margin-top: 20px; } diff --git a/public/js/shorten_result.js b/public/js/shorten_result.js index 9e3150b..5c6f4a3 100644 --- a/public/js/shorten_result.js +++ b/public/js/shorten_result.js @@ -8,6 +8,7 @@ $('.result-box').click(select_text); $('.result-box').change(function () { $(this).val(original_link); }); + $(function () { original_link = $('.result-box').val(); select_text(); diff --git a/resources/views/admin.blade.php b/resources/views/admin.blade.php index 085a34f..99d3b42 100644 --- a/resources/views/admin.blade.php +++ b/resources/views/admin.blade.php @@ -14,6 +14,10 @@ @if ($role == 'admin')
API keys and documentation for developers.
++ Documentation: + http://docs.polr.me/en/latest/developer-guide/api/ +
+ +{{$api_quota}}