Permalink
Browse files

some new stuff

  • Loading branch information...
david p
david p committed Jan 3, 2016
1 parent 681f42d commit 25a834d3fe1af46b056023d5d4d8124a82d8740e
Showing 334 changed files with 57,339 additions and 236 deletions.
View
@@ -48,4 +48,17 @@ public static function findByTelegramUser($telegramUser)
->groupBy('apps.id')
->get(array('apps.*'));
}
public static function findByUser($user)
{
return App::where('user_id', '=', $user->id)
->get();
}
public static function findByUserAndId($user, $id)
{
return App::where('id', '=', $id)
->where('user_id', '=', $user->id)
->firstOrFail();
}
}
View
@@ -21,7 +21,7 @@ function generate_client_secret() {
}
function generate_email() {
return random_str(15);
return random_str(8, '0123456789abcdefghijklmnopqrstuvwxyz');
}
function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
@@ -7,16 +7,19 @@
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\App;
class AppController extends Controller
{
/**
* Display a listing of the resource.
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
public function create()
{
return response()->json($request->user());
return view('apps.create');
}
/**
@@ -25,9 +28,28 @@ public function index(Request $request)
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
public function store(Requests\AppRequest $request)
{
$app = new App();
$app->fill($request->all());
$app->user_id = $request->user()->id;
$app->client_id = generate_client_id();
$app->client_secret = generate_client_secret();
$app->save();
return redirect('dashboard');
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Request $request, $id)
{
//
$app = App::findByUserAndId($request->user(), $id);
return view('apps.edit', ['app' => $app]);
}
/**
@@ -37,9 +59,13 @@ public function store(Request $request)
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
public function update(Requests\AppRequest $request, $id)
{
//
$app = App::findOrFail($id);
$app->fill($request->all());
$app->save();
return redirect('dashboard');
}
/**
@@ -48,8 +74,10 @@ public function update(Request $request, $id)
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
public function destroy(Request $request, $id)
{
//
$app = App::findByUserAndId($request->user(), $id);
$app->delete();
return response('');
}
}
@@ -0,0 +1,32 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\App;
class DashboardController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$user = $request->user();
$apps = App::findByUser($user);
return view('dashboard', ['apps' => $apps]);
}
public function profile(Request $request)
{
return view('profile', ['user' => $request->user()]);
}
}
@@ -16,9 +16,6 @@ class HomeController extends Controller
*/
public function index(Request $request)
{
if($request->user()) {
return redirect('/app');
}
return view('welcome');
}
@@ -23,16 +23,19 @@ public function receive(Request $request, $token)
$message = $request->input('message');
if(!array_key_exists('text', $message))
app()->abort(200, 'Missing start command');
app()->abort(200, 'Missing command');
if(starts_with($message['text'], '/start'))
$this->start($message);
//else if(starts_with($message['text'], '/cancel'))
// do nothing
else if(starts_with($message['text'], '/cancel'))
$this->cancel($message);
else if(starts_with($message['text'], '/list'))
$this->listApps($message);
else if(starts_with($message['text'], '/revoke'))
$this->revoke($message);
else
$this->commandReply($message);
\Log::debug($message);
@@ -61,7 +64,11 @@ private function start($message) {
}
$tg->name = $telegramName;
$tg->username = $username;
$tg->status = 'access_granted';
if($app->client_id == 314159265) {
$tg->status = str_replace('state=', '', $token->query_string);
} else {
$tg->status = 'access_granted';
}
$tg->save();
try {
@@ -70,7 +77,7 @@ private function start($message) {
$auth = new Auth();
$auth->app_id = $app->id;
$auth->telegram_user_id = $tg->id;
$auth->email = generate_email().'@telegramlogin.com';
$auth->email = generate_email().'-'.$app->id.'-'.$tg->id.'@telegramlogin.com';
}
$auth->access_token = generate_access_token();
$auth->active = true;
@@ -106,11 +113,30 @@ private function start($message) {
$token->delete();
}
private function cancel($message)
{
$telegramId = $message['from']['id'];
$tg = TelegramUser::findByTelegramId($telegramId);
$tg->status = 'cancel';
$tg->save();
$params = array(
'text' => 'Operation cancelled.',
'chat_id' => $telegramId,
'parse_mode' => 'Markdown',
'reply_markup' => json_encode(['hide_keyboard' => true])
);
\Log::debug($this->send($params));
}
private function listApps($message)
{
$telegramId = $message['from']['id'];
$tg = TelegramUser::findByTelegramId($telegramId);
$tg->status = 'list_apps';
$tg->save();
$apps = App::findByTelegramUser($tg);
if(count($apps)) {
$text = 'Here are your active apps:'.PHP_EOL;
@@ -141,11 +167,13 @@ private function revoke($message)
$telegramId = $message['from']['id'];
$tg = TelegramUser::findByTelegramId($telegramId);
$tg->status = 'revoke_access';
$tg->save();
$apps = App::findByTelegramUser($tg);
$keyboard = array();
foreach($apps as $a) {
$keyboard[] = array($a->name);
$keyboard[] = array('['.$a->client_id.'] - '.$a->name);
}
$markup = array(
@@ -165,6 +193,43 @@ private function revoke($message)
\Log::debug($this->send($params));
}
private function commandReply($message)
{
$telegramId = $message['from']['id'];
$tg = TelegramUser::findByTelegramId($telegramId);
$params = array(
'chat_id' => $telegramId,
'parse_mode' => 'Markdown',
);
if($tg->status == 'revoke_access') {
$clientId = preg_replace('/[^0-9,.]/', '', $message['text']);
try {
$app = App::findByClientId($clientId);
$auth = Auth::findByAppAndTelegramUser($app, $tg);
$auth->active = false;
$auth->save();
$text = 'Access to this app has been revoked.';
$tg->status = 'access_revoked';
$params['reply_markup'] = json_encode(['hide_keyboard' => true]);
} catch(ModelNotFoundException $e) {
$text = 'Unknown app. Please choose an app from the given list:';
}
} else {
$text = 'Unknown command.';
$tg->status = 'unknown_command';
$params['reply_markup'] = json_encode(['hide_keyboard' => true]);
}
$tg->save();
$params['text'] = $text;
\Log::debug($this->send($params));
}
private function send($params)
{
$ch = curl_init();
@@ -9,6 +9,7 @@
use App\Http\Controllers\Controller;
use App\App;
use App\Code;
use App\TelegramUser;
use App\User;
use Auth;
@@ -17,12 +18,12 @@ class UserController extends Controller
public function login(Requests\LoginRequest $request)
{
$user = $this->getUser($request->input('code'));
$user = $this->getUser($request->input('code'), $request->input('state'));
Auth::login($user);
return redirect('app');
return redirect('dashboard');
}
private function getUser($code)
private function getUser($code, $state)
{
$app = App::findOrFail(1);
@@ -37,14 +38,20 @@ private function getUser($code)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$json = curl_exec($ch);
\Log::debug($json);
$result = json_decode($json, true);
if(!array_key_exists('access_token', $result))
app()->abort($result['error'], $result['description']);
if(array_key_exists('active', $result) && !$result['active'])
app()->abort($result['error'], $result['description']);
$tg = TelegramUser::findByTelegramId($result['telegram_user']['telegram_id']);
if($tg->status != $state)
app()->abort(403, 'Invalid state.');
$tg->status = 'access_granted';
$tg->save();
try {
$user = User::findByTelegramId($result['telegram_user']['telegram_id']);
} catch (ModelNotFoundException $e) {
@@ -38,7 +38,7 @@ public function handle($request, Closure $next)
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('auth/login');
return redirect()->guest('/token/314159265?state='.random_str(20));
}
}
@@ -0,0 +1,41 @@
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\App;
class AppRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
if($this->route('app')) {
$id = $this->route('app');
$user = $this->user();
return App::where('id', '=', $id)
->where('user_id', '=', $user->id)
->exists();
}
return $this->user() ? true : false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'string|required',
'redirect_url' => 'url|required',
'website' => 'url'
];
}
}
View
@@ -28,5 +28,9 @@
Route::group(['middleware' => ['auth']], function()
{
Route::get('app', 'AppController@index');
Route::get('dashboard', 'DashboardController@index');
Route::get('profile', 'DashboardController@profile');
Route::resource('app', 'AppController',
['except' => ['index', 'show']]);
});
Oops, something went wrong.

0 comments on commit 25a834d

Please sign in to comment.