Skip to content
Snippets Groups Projects

Resolve "Implement Method for Changing the member key"

19 files
+ 499
77
Compare changes
  • Side-by-side
  • Inline
Files
19
@@ -6,37 +6,31 @@ use Cookie;
use Illuminate\Http\Request;
use LaravelLocalization;
use \App\Models\Key;
use \Carbon\Carbon;
use Validator;
class KeyController extends Controller
{
public function index(Request $request)
{
$redirUrl = $request->input('redirUrl', "");
$cookie = Cookie::get('key');
$key = $request->input('keyToSet', '');
if (empty($key) && empty($cookie)) {
$key = 'enter_key_here';
} elseif (empty($key) && !empty($cookie)) {
$key = $cookie;
} elseif (!empty($key)) {
$key = $request->input('key');
}
// How many Ad Free searches should a user get max when he creates a new key
const KEYCHANGE_ADFREE_SEARCHES = 150;
public function index(\App\Models\Key $key, Request $request)
{
$cookieLink = LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('loadSettings', Cookie::get()));
return view('key')
->with('title', trans('titles.key'))
->with('cookie', $key)
->with('keystatus', $key->getStatus())
->with('cookie', $key->key)
->with('cookieLink', $cookieLink);
}
public function setKey(Request $request)
{
$redirUrl = $request->input('redirUrl', "");
$keyToSet = $request->input('keyToSet');
$key = new Key($request->input('keyToSet', ''));
if ($key->getStatus()) {
$status = $key->getStatus();
if ($status !== null) {
# Valid Key
$host = $request->header("X_Forwarded_Host", "");
if (empty($host)) {
@@ -46,10 +40,7 @@ class KeyController extends Controller
$settings = Cookie::get();
$settings['key'] = $keyToSet;
$cookieLink = LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('loadSettings', $settings));
return view('key')
->with('title', trans('titles.key'))
->with('cookie', $keyToSet)
->with('cookieLink', $cookieLink);
return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('keyindex')));
} else {
$cookieLink = LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('loadSettings', Cookie::get()));
return view('key')
@@ -74,4 +65,152 @@ class KeyController extends Controller
return redirect($url);
}
}
public function changeKeyIndex(\App\Models\Key $key, Request $request){
if(!$key->canChange()){
return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('keyindex')));
}
return view('keychange', [
"title" => trans('titles.keychange'),
"key" => $key->key,
"css" => [mix('css/keychange/index.css')]
]);
}
public function removeCurrent(\App\Models\Key $key, Request $request){
if(!$key->canChange()){
return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('keyindex')));
}
// Reduce Current Key
$res = $key->reduce(self::KEYCHANGE_ADFREE_SEARCHES);
if(empty($res) || empty($res->status) || $res->status !== "success"){
return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('keyindex')));
}
// Redirect to Cookie Remove URL with redirect to step two
$validUntil = Carbon::now("Europe/London")->addDays(2);
$format = "Y-m-d H:i:s";
$data = [
"validUntil" => $validUntil->format($format),
"password" => hash_hmac("sha256", $validUntil->format($format), env("APP_KEY", "WEAK_KEY")),
];
$targetUrl = LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('changeKeyTwo', $data));
$redirUrl = LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('removeCookie', [
"ir" => $targetUrl
]));
return redirect($redirUrl);
}
public function generateNew(\App\Models\Key $key, Request $request){
// Validate Request Data
$validUntil = $request->input('validUntil', '');
$password = $request->input('password', '');
$format = "Y-m-d H:i:s";
// Check if Validuntil
$valid = true;
if(empty($validUntil)){
$valid = false;
}else{
$validUntil = Carbon::createFromFormat($format, $validUntil, "Europe/London");
if(!$validUntil){
$valid = false;
}
}
if($valid && Carbon::now()->diffInSeconds($validUntil) <= 0){
$valid = false;
}
if($valid){
// Check if hash matches
$expectedHash = hash_hmac("sha256", $validUntil->format($format), env("APP_KEY", "WEAK_KEY"));
if(!hash_equals($expectedHash, $password)){
$valid = false;
}
}
if(!$valid){
return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('keyindex')));
}
// Check if the key already was generated
if (!$key->checkForChange("", $password)) {
return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('keyindex')));
}
return view('keychangetwo', [
"title" => trans('titles.keychange'),
"validUntil" => $validUntil,
"css" => [mix('css/keychange/index.css')]
]);
}
public function generateNewPost(\App\Models\Key $key, Request $request){
// Validate Request Data
$validUntil = $request->input('validUntil', '');
$password = $request->input('password', '');
$format = "Y-m-d H:i:s";
// Check if Validuntil
$valid = true;
if(empty($validUntil)){
$valid = false;
}else{
$validUntil = Carbon::createFromFormat($format, $validUntil, "Europe/London");
if(!$validUntil){
$valid = false;
}
}
if($valid && Carbon::now()->diffInSeconds($validUntil) <= 0){
$valid = false;
}
if($valid){
// Check if hash matches
$expectedHash = hash_hmac("sha256", $validUntil->format($format), env("APP_KEY", "WEAK_KEY"));
if(!hash_equals($expectedHash, $password)){
$valid = false;
}
}
if(!$valid){
return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('keyindex')));
}
$validator = Validator::make($request->all(), [
'newkey' => 'required|min:4|max:20',
]);
if($validator->fails()) {
$data = [
"validUntil" => $validUntil->format($format),
"password" => hash_hmac("sha256", $validUntil->format($format), env("APP_KEY", "WEAK_KEY")),
"newkey" => $request->input('newkey', ''),
];
$targetUrl = LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('changeKeyTwo', $data));
return redirect($targetUrl);
}
$newkey = $request->input('newkey', '');
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$randomSuffix = "";
$suffixCount = 3;
for($i = 0; $i < $suffixCount; $i++){
$randomSuffix .= $characters[rand(0, strlen($characters)-1)];
}
$newkey = $newkey . $randomSuffix;
if($key->checkForChange($newkey, $password)){
$result = $key->generateKey(null, self::KEYCHANGE_ADFREE_SEARCHES, $newkey, "Schlüssel gewechselt. Hash $password");
if(!empty($result)){
Cookie::queue('key', $result, 525600, '/', null, false, false);
return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('changeKeyThree', ["newkey" => $result])));
}
}
$data = [
"validUntil" => $validUntil->format($format),
"password" => hash_hmac("sha256", $validUntil->format($format), env("APP_KEY", "WEAK_KEY")),
];
$targetUrl = LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('changeKeyTwo', $data));
return redirect($targetUrl);
}
}
Loading