Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#82 - Authenticate with discord #77

Merged
merged 58 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
f8662ac
Update Mission.php
TomBurch Aug 19, 2020
1371ec6
Implement ValidateMissionContents
TomBurch Feb 21, 2021
3ea61ca
Merge branch 'master' of https://github.com/TomBurch/ARCOMM
TomBurch Feb 21, 2021
70de06f
Fix + translate PBOMission errors
TomBurch Feb 21, 2021
3f2fcc4
Remove PBOMission changes from master
TomBurch Feb 21, 2021
f11a3e8
Merge branch 'master' of https://github.com/ARCOMM/ARCHUB
TomBurch Mar 6, 2021
06de1ea
Merge pull request #2 from ARCOMM/master
TomBurch Mar 15, 2021
8c316bb
Merge branch 'ARCOMM:master' into master
TomBurch May 28, 2021
1c72564
Merge branch 'ARCOMM:master' into master
TomBurch May 30, 2021
f0febc1
Merge branch 'ARCOMM:master' into master
TomBurch May 30, 2021
ec94996
Merge branch 'ARCOMM:master' into master
TomBurch May 31, 2021
5e7e930
Upgrade laravel/framework 5.8 -> 6 and upgrade/remove other dependencies
TomBurch May 31, 2021
2064c84
Update Mission.php
TomBurch May 31, 2021
dc76412
Create 2021_06_01_202948_medialibrary_v8_update_media_table.php
TomBurch Jun 1, 2021
b6f9727
Update media-library.php
TomBurch Jun 1, 2021
86d8f24
Merge branch 'master' into 55-upgrade_laravel_framework
BorderKeeper Jun 9, 2021
2a3cd3c
Scrap old login page and countdown
TomBurch Jun 12, 2021
034e137
Basic discord authentication
TomBurch Jun 12, 2021
54eb0a2
Update discord, remove permissions/steam/username
TomBurch Jun 12, 2021
b294178
Permission comments
TomBurch Jun 12, 2021
42b35ab
Remove attendance
TomBurch Jun 13, 2021
4e1c468
Start adding permissions
TomBurch Jun 13, 2021
233bfff
More permissions
TomBurch Jun 14, 2021
1354933
Add admin and senior tester roles
TomBurch Jun 14, 2021
26f3c45
Change login button
TomBurch Jun 16, 2021
008ab31
Remove absences
TomBurch Jun 16, 2021
28e927f
Remove extra absences
TomBurch Jun 16, 2021
031309b
name -> username
TomBurch Jun 16, 2021
3857677
Missed instances
TomBurch Jun 16, 2021
442e22c
Remove nonMembers
TomBurch Jun 17, 2021
c1a3d0b
Add avatarSync
TomBurch Jun 17, 2021
abcd87d
Update User.php
TomBurch Jun 17, 2021
775aded
Remove brackets
TomBurch Jun 17, 2021
3e1e7a7
Update Mission.php
TomBurch Jun 17, 2021
5d0ff46
Update PageController.php
TomBurch Jun 17, 2021
4e4e418
Merge branch 'ARCOMM:master' into master
TomBurch Jun 19, 2021
999dc4d
Merge branch 'ARCOMM:master' into master
TomBurch Jun 19, 2021
1226e7b
Merge branch 'master' into authenticate-with-discord
TomBurch Jun 19, 2021
1425adb
Merge conflict
TomBurch Jun 19, 2021
61ece02
Merge branch 'ARCOMM:master' into master
TomBurch Jun 19, 2021
7d840c5
Merge branch 'master' into authenticate-with-discord
TomBurch Jun 19, 2021
5626cfc
Merge branch 'ARCOMM:master' into master
TomBurch Jun 20, 2021
c79dc5e
Upgrade laravel/framework to v8 (latest)
TomBurch Jun 20, 2021
724ca76
Merge branch 'master' into authenticate-with-discord
TomBurch Jun 20, 2021
327ff07
Merge branch '55-upgrade_laravel_framework_to_v8' into authenticate-w…
TomBurch Jun 20, 2021
87b1040
Use HTTP facade
TomBurch Jun 20, 2021
7c2f283
Add method for migration
TomBurch Jun 21, 2021
60e11ad
Restore migration files
TomBurch Jun 21, 2021
c88719a
Remove line
TomBurch Jun 21, 2021
d68906e
Update PageController.php
TomBurch Jun 21, 2021
d768816
Redirect on failed getRoles
TomBurch Jun 21, 2021
ceaf662
Fix exception, change verify-missions to Tester
TomBurch Jun 21, 2021
3ce755a
Merge branch 'ARCOMM:master' into master
TomBurch Jun 21, 2021
1858bb7
Merge remote-tracking branch 'origin/master' into authenticate-with-d…
TomBurch Jun 21, 2021
1f64185
Update AuthServiceProvider.php
TomBurch Jun 21, 2021
9dd6fb8
Update AuthServiceProvider.php
TomBurch Jun 21, 2021
2b6c527
Fix missionUpdate ping
TomBurch Jun 21, 2021
b6ef1bc
Update AuthServiceProvider.php
TomBurch Jun 22, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/ChannelEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App;

class ChannelEnum
{
const Archub = 0;
const Staff = 1;
}
63 changes: 0 additions & 63 deletions app/Console/Commands/PurgeNonMembers.php

This file was deleted.

1 change: 0 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class Kernel extends ConsoleKernel
protected $commands = [
Commands\MigrateOldApps::class,
Commands\ConvertIdToRevisions::class,
Commands\PurgeNonMembers::class,
];

/**
Expand Down
128 changes: 128 additions & 0 deletions app/Discord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace App;

use App\ChannelEnum;
use App\RoleEnum;
use App\Models\Portal\User;
use App\Models\Missions\Mission;
use Illuminate\Auth\Access\AuthorizationException;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

class Discord
{
public static function missionUpdate(string $content, Mission $mission, bool $tagAuthor = false, string $url = null)
{
if ($tagAuthor && ($mission->user->id != auth()->user()->id)) {
$content = "{$content} <@{$mission->user->discord_id}>";
}

if (!is_null($url)) {
$content = "{$content}\n{$url}";
}

self::notifyChannel(ChannelEnum::Archub, $content);
}

public static function notifyChannel(string $channel, string $content)
{
$webhook = self::getWebhookFromChannel($channel);
$response = HTTP::post($webhook, [
'content' => $content,
]);

return $response;
}

private static function getWebhookFromChannel(int $channel)
{
if ($channel == ChannelEnum::Archub)
{
return config('services.discord.archub_webhook');
}
else if ($channel == ChannelEnum::Staff)
{
return config('services.discord.staff_webhook');
}
else
{
throw new Exception("Webhook not found");
}
}

private static function getUser(int $discord_id)
{
return Cache::remember($discord_id, 10, function() use ($discord_id) {
$url = "https://discord.com/api/v8/guilds/".config('services.discord.server_id')."/members/{$discord_id}";
$response = HTTP::withHeaders([
'Authorization' => "Bot ".config('services.discord.token')
])->get($url);

if ($response->successful()) {
return (array)$response->json();
}

throw new AuthorizationException("Error getting user from discord ". $response->status());
});
}

public static function getAvatar(int $discord_id)
{
$avatarHash = ((array)self::getUser($discord_id)["user"])["avatar"];
return "https://cdn.discordapp.com/avatars/{$discord_id}/{$avatarHash}.jpg";
}

private static function getRoles(int $discord_id)
{
return self::getUser($discord_id)["roles"];
}

public static function hasRole(User $user, int $role)
{
if (!auth()->guest() && is_null(auth()->user()->discord_id)) {
throw new AuthorizationException;
}
$roleId = self::getRoleIdFromRole($role);
$roles = self::getRoles($user->discord_id);

return in_array($roleId, $roles);
}

public static function isMember(int $discord_id)
{
$roleId = self::getRoleIdFromRole(RoleEnum::Member);
$roles = self::getRoles($discord_id);

return in_array($roleId, $roles);
}

private static function getRoleIdFromRole(int $role)
{
if ($role == RoleEnum::Member)
{
return config('services.discord.member_role');
}
else if ($role == RoleEnum::Tester)
{
return config('services.discord.tester_role');
}
else if ($role == RoleEnum::SeniorTester)
{
return config('services.discord.senior_tester_role');
}
else if ($role == RoleEnum::Staff)
{
return config('services.discord.staff_role');
}
else if ($role == RoleEnum::Admin)
{
return config('services.discord.admin_role');
}
else
{
throw new Exception("RoleId not found");
}
}
}
24 changes: 0 additions & 24 deletions app/DiscordWebhook.php

This file was deleted.

3 changes: 3 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public function report(Throwable $exception)
*/
public function render($request, Throwable $exception)
{
if ($exception instanceof \Illuminate\Auth\Access\AuthorizationException && !auth()->guest() && is_null(auth()->user()->discord_id)) {
return redirect('/auth/redirect');
}
return parent::render($request, $exception);
}

Expand Down
78 changes: 78 additions & 0 deletions app/Http/Controllers/Auth/DiscordController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace App\Http\Controllers\Auth;

use App\Discord;
use App\RoleEnum;
use App\Models\Portal\User;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Auth\Events\Registered;
use Laravel\Socialite\Facades\Socialite;

class DiscordController extends Controller
{
/**
* Redirect the user to the Discord login page.
*
* @return \Illuminate\Http\Response
*/
public function redirect()
{
return Socialite::driver('discord')->redirect();
}

/**
* Handles the response from Discord.
*
* @return \Illuminate\Http\Response
*/
public function callback(Request $request)
{
$user = Socialite::driver('discord')->user();

if (!auth()->guest()) {
if (is_null(auth()->user()->discord_id)) {
auth()->user()->discord_id = $user->id;
auth()->user()->save();
return redirect('/hub');
}
}
Comment on lines +36 to +42
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For migrating existing users


if (Discord::isMember($user->id)) {
return $this->create($user);
}

return [
'error' => 'You are not a member.'
];
}

/**
* Creates the user account and redirects with the access token.
*
* @return \Illuminate\Http\Response
*/
public function create($data)
{
$user = User::where('discord_id', $data->id)->first();

if (is_null($user)) {
$user = User::create([
'discord_id' => $data->id,
'username' => $data->name,
'email' => $data->email,
'avatar' => $data->avatar,
]);
}

auth()->login($user, true);

if ($user->can('access-hub')) {
return redirect('/hub');
}
return redirect('/');
}
}
32 changes: 0 additions & 32 deletions app/Http/Controllers/Auth/ForgotPasswordController.php

This file was deleted.

39 changes: 0 additions & 39 deletions app/Http/Controllers/Auth/LoginController.php

This file was deleted.

Loading