NL最近开始提供免费虚拟主机,之前一直在想使用哪个系统,本来是使用WHMCS和DA破解版,但多少有点不好意思。
最后先用了 Paymenter 和 CyberPanel。
Paymenter是近两年出来的,类似于WHMCS,HostBill的主机类销售管理系统。 他是免费开源的系统。
CyberPanel其实本站之前已经介绍过多次,国外的一款主机管理系统,是基于 LiteSpeed(Ent) 或者 OpenLiteSpeed(OpenSource)的。
为了方便NL用户使用虚拟主机,我在NL启用了Oauth Center,但是Paymenter默认只支持3种Oauth(discord,google,github),所以要让Paymenter支持使用NL账户登录,就需要做一些改动。因为我也是第一次接触Paymenter,不是太熟悉,所以直接源码动手改造了,最好的方式当然还是应该以插件的形式来提供。
话不多说,下面是具体的一些改动。
Step 1:
增加一个 FlarumProvider.php 类
<?php
namespace App\Services\OAuth;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\User;
class FlarumProvider extends AbstractProvider
{
protected $baseUrl = 'https://www.nodeloc.com';
protected $authorizeUrl = 'https://www.nodeloc.com/oauth/authorize';
protected $tokenUrl = 'https://www.nodeloc.com/oauth/token';
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase($this->authorizeUrl, $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return $this->tokenUrl;
}
public function getAccessToken($code)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
'body' => $this->getTokenFields($code),
]);
return $this->parseAccessToken($response->getBody());
}
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get(
$this->baseUrl.'/api/user',
[
'headers' => [
'Accept' => 'application/json',
'access_token' => 'Bearer'.$token,
'Authorization' => 'Bearer '.$token,
],
]
);
return json_decode($response->getBody(), true);
}
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => $user['id'],
'name' => $user['username'],
'email' => $user['email'],
]);
}
}
这是一个Provider类,告诉程序遇到 Flarum 的oauth到哪里处理。
Step 2 修改 AppServiceProvider.php 类
在boot 中加入以下代码
if (config('settings::flarum_enabled')) {
// 扩展 Socialite 以支持 Flarum 提供的定义 OAuth 认证服务
Socialite::extend('flarum', function ($app) {
$config = $app['config']['services.flarum'];
return Socialite::buildProvider(FlarumProvider::class, $config);
});
config(['services.flarum.client_id' => config('settings::flarum_client_id')]);
config(['services.flarum.client_secret' => config('settings::flarum_client_secret')]);
config(['services.flarum.redirect' => url('/login/flarum/callback')]);
}
也就是如此启用了 flarum 的 oauth,那么初始化FlarumProvider来处理。
Step 3 修改SocialLoginController.php 控制器,处理Oauth登录时的跳转
<?php
namespace App\Http\Controllers\Auth;
use App\Models\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
class SocialLoginController extends Controller
{
public function redirectToProvider($provider)
{
if ($provider == 'discord') {
return Socialite::driver($provider)->scopes(['email'])->redirect();
}elseif ($provider == 'flarum') {
return Socialite::driver($provider)->scopes(['user.read'])->redirect();
}elseif ($provider == 'github') {
return Socialite::driver($provider)->scopes(['user:email'])->redirect();
} elseif ($provider == 'google') {
return Socialite::driver($provider)->scopes(['email'])->redirect();
} else {
return redirect()->route('login');
}
}
public function handleProviderCallback($provider)
{
if ($provider == 'discord') {
$user = Socialite::driver($provider)->user();
if($user->user["verified"] == false) {
return redirect()->route('login')->with('error', 'Your Discord account is not verified.');
}
$user = User::where('email', $user->email)->first();
if (!$user) {
return redirect()->route('register')->with('error', 'You are not registered on this site.');
} else {
Auth::login($user, true);
return redirect()->route('index');
}
}
elseif ($provider == 'flarum') {
$user = Socialite::driver($provider)->user();
$user = User::where('email', $user->email)->first();
if (!$user) {
return redirect()->route('register')->with('error', 'You are not registered on this site.');
} else {
Auth::login($user, true);
return redirect()->route('index');
}
}
elseif ($provider == 'google') {
$user = Socialite::driver($provider)->user();
$user = User::where('email', $user->email)->first();
if (!$user) {
return redirect()->route('register')->with('error', 'You are not registered on this site.');
} else {
Auth::login($user, true);
return redirect()->route('index');
}
} elseif ($provider == 'github') {
$user = Socialite::driver($provider)->user();
$user = User::where('email', $user->email)->first();
if (!$user) {
return redirect()->route('register')->with('error', 'You are not registered on this site.');
} else {
Auth::login($user, true);
return redirect()->route('index');
}
} else {
return redirect()->route('login');
}
}
}
Step 4 修改前台界面 ,增加 Flarum 登录按钮
@if (config('settings::flarum_enabled'))
<a href="{{ route('social.login', 'flarum') }}"
class="button button-secondary !flex gap-x-2 items-center justify-center">
<svg class="w-5 h-5 mr-2 text-secondary-900 dark:text-secondary" viewBox="0 0 127.14 96.36"
fill="none" xmlns="http://www.w3.org/2000/svg">
<defs>
<style>
.cls-1 {
fill: var(--secondary-900);
}
</style>
</defs>
<g id="Flarum_Logo_-_Large_-_White" data-name="Flarum Logo - Large - White">
<path d="M0 0 C3.0625 0.1875 3.0625 0.1875 5.0625 2.1875 C5.2501103 5.04464464 5.31807318 7.80315986 5.29296875 10.66015625 C5.29251053 11.49840439 5.29205231 12.33665253 5.2915802 13.20030212 C5.28855724 14.97314875 5.28066963 16.74599271 5.26831055 18.51879883 C5.25005065 21.24244522 5.24780085 23.96575192 5.24804688 26.68945312 C5.24313192 28.40885713 5.2373037 30.12825881 5.23046875 31.84765625 C5.22935593 32.66742447 5.2282431 33.48719269 5.22709656 34.33180237 C5.17854801 40.07145199 5.17854801 40.07145199 4.0625 41.1875 C0.890625 41.76953125 0.890625 41.76953125 -2.9375 41.1875 C-5.52863592 38.75505199 -7.30656121 35.87335217 -9.1875 32.875 C-10.21132381 31.31494847 -11.23728899 29.75629969 -12.265625 28.19921875 C-12.7080957 27.50272217 -13.15056641 26.80622559 -13.60644531 26.08862305 C-15.06954861 23.88175942 -15.06954861 23.88175942 -17.9375 21.1875 C-18.2675 28.1175 -18.5975 35.0475 -18.9375 42.1875 C-21.9075 41.8575 -24.8775 41.5275 -27.9375 41.1875 C-27.9375 28.3175 -27.9375 15.4475 -27.9375 2.1875 C-24.4725 1.6925 -24.4725 1.6925 -20.9375 1.1875 C-16.98993553 4.22010419 -14.49275458 8.0929777 -11.8125 12.25 C-9.61007773 15.64790051 -7.38513554 18.96272073 -4.9375 22.1875 C-4.91244385 21.55658447 -4.8873877 20.92566895 -4.86157227 20.27563477 C-4.74595863 17.43353175 -4.62307684 14.59178917 -4.5 11.75 C-4.46068359 10.75677734 -4.42136719 9.76355469 -4.38085938 8.74023438 C-4.33896484 7.79599609 -4.29707031 6.85175781 -4.25390625 5.87890625 C-4.21724854 5.00435791 -4.18059082 4.12980957 -4.14282227 3.22875977 C-3.8152746 -0.0276327 -3.34670741 0.21361962 0 0 Z M2.0625 10.1875 C3.0625 12.1875 3.0625 12.1875 3.0625 12.1875 Z " fill="#189961" transform="translate(28.9375,11.8125)"/>
<path d="M0 0 C0.02505615 0.69037354 0.0501123 1.38074707 0.07592773 2.09204102 C0.1926566 5.24901409 0.31503178 8.40574457 0.4375 11.5625 C0.47681641 12.64853516 0.51613281 13.73457031 0.55664062 14.85351562 C0.77201355 20.30410746 1.08159222 25.62075441 2 31 C5.42689636 32.14229879 7.61102218 31.90861076 11.1875 31.5625 C12.27417969 31.46066406 13.36085938 31.35882813 14.48046875 31.25390625 C15.72763672 31.12822266 15.72763672 31.12822266 17 31 C17.09765625 37.15234375 17.09765625 37.15234375 17 39 C14.68780525 41.31219475 9.24665588 40.47820014 6.125 40.625 C5.20074219 40.6765625 4.27648437 40.728125 3.32421875 40.78125 C2.43605469 40.82507812 1.54789063 40.86890625 0.6328125 40.9140625 C-0.18300293 40.9548291 -0.99881836 40.9955957 -1.83935547 41.03759766 C-4 41 -4 41 -7 40 C-8.02992723 35.70069524 -8.21399703 31.47331016 -8.31640625 27.07421875 C-8.33718735 26.32662796 -8.35796844 25.57903717 -8.37937927 24.80879211 C-8.44444189 22.43511604 -8.5035571 20.06133513 -8.5625 17.6875 C-8.60572646 16.07225505 -8.64934204 14.45702047 -8.69335938 12.84179688 C-8.80007851 8.89463701 -8.90146619 4.94737233 -9 1 C-5.92677456 0.08941469 -3.19902639 -0.08886184 0 0 Z M5 34 C5.33 34.66 5.66 35.32 6 36 C6.33 35.34 6.66 34.68 7 34 C6.34 34 5.68 34 5 34 Z " fill="#BC9944" transform="translate(46,12)"/>
<path d="M0 0 C3.0625 0.1875 3.0625 0.1875 5.0625 2.1875 C5.2501103 5.04464464 5.31807318 7.80315986 5.29296875 10.66015625 C5.29228142 11.91752846 5.29228142 11.91752846 5.2915802 13.20030212 C5.28855724 14.97314875 5.28066963 16.74599271 5.26831055 18.51879883 C5.25005065 21.24244522 5.24780085 23.96575192 5.24804688 26.68945312 C5.24313192 28.40885713 5.2373037 30.12825881 5.23046875 31.84765625 C5.22935593 32.66742447 5.2282431 33.48719269 5.22709656 34.33180237 C5.17854801 40.07145199 5.17854801 40.07145199 4.0625 41.1875 C0.875 41.8125 0.875 41.8125 -2.9375 41.1875 C-5.96139067 38.62783135 -7.61056342 35.90494827 -8.9375 32.1875 C-9.12612505 27.74984758 -8.6808537 23.55977132 -7.9375 19.1875 C-6.9475 20.1775 -5.9575 21.1675 -4.9375 22.1875 C-4.89991577 21.24112671 -4.89991577 21.24112671 -4.86157227 20.27563477 C-4.74595863 17.43353175 -4.62307684 14.59178917 -4.5 11.75 C-4.44102539 10.26016602 -4.44102539 10.26016602 -4.38085938 8.74023438 C-4.33896484 7.79599609 -4.29707031 6.85175781 -4.25390625 5.87890625 C-4.19891968 4.56708374 -4.19891968 4.56708374 -4.14282227 3.22875977 C-3.8152746 -0.0276327 -3.34670741 0.21361962 0 0 Z M2.0625 10.1875 C3.0625 12.1875 3.0625 12.1875 3.0625 12.1875 Z " fill="#679954" transform="translate(28.9375,11.8125)"/>
<path d="M0 0 C4 1 4 1 5 2 C5.08861992 4.8116338 5.11522355 7.59858255 5.09765625 10.41015625 C5.0962413 11.25219101 5.09482635 12.09422577 5.09336853 12.96177673 C5.08775263 15.66205335 5.07519748 18.36224828 5.0625 21.0625 C5.05748705 22.88867068 5.05292379 24.71484266 5.04882812 26.54101562 C5.03777804 31.02737542 5.02050279 35.51367384 5 40 C2.525 40.495 2.525 40.495 0 41 C0 27.47 0 13.94 0 0 Z M2 10 C3 12 3 12 3 12 Z " fill="#7D9950" transform="translate(29,12)"/>
<path d="M0 0 C1.32 0 2.64 0 4 0 C4.9114645 8.20421859 5.15301589 16.31182124 5.125 24.5625 C5.12886719 25.73619141 5.13273437 26.90988281 5.13671875 28.11914062 C5.13542969 29.23611328 5.13414063 30.35308594 5.1328125 31.50390625 C5.13168457 32.51267822 5.13055664 33.5214502 5.12939453 34.56079102 C5 37 5 37 4 39 C3.34 39 2.68 39 2 39 C0.97007277 34.70069524 0.78600297 30.47331016 0.68359375 26.07421875 C0.66281265 25.32662796 0.64203156 24.57903717 0.62062073 23.80879211 C0.55555811 21.43511604 0.4964429 19.06133513 0.4375 16.6875 C0.39427354 15.07225505 0.35065796 13.45702047 0.30664062 11.84179688 C0.19992149 7.89463701 0.09853381 3.94737233 0 0 Z " fill="#A69948" transform="translate(37,13)"/>
<path d="M0 0 C0.09765625 6.15234375 0.09765625 6.15234375 0 8 C-1 9 -1 9 -4.16015625 9.09765625 C-6.09181641 9.08025391 -6.09181641 9.08025391 -8.0625 9.0625 C-9.35285156 9.05347656 -10.64320312 9.04445313 -11.97265625 9.03515625 C-12.97167969 9.02355469 -13.97070312 9.01195312 -15 9 C-15 8.34 -15 7.68 -15 7 C-14.34 6.525625 -13.68 6.05125 -13 5.5625 C-12.34 5.046875 -11.68 4.53125 -11 4 C-11 3.01 -11 2.02 -11 1 C-3.375 0 -3.375 0 0 0 Z " fill="#EC9938" transform="translate(63,43)"/>
<path d="M0 0 C1.984375 1.515625 1.984375 1.515625 4 4 C4.2911521 6.73682971 4.38322418 9.03940039 4.25 11.75 C4.23195313 12.44738281 4.21390625 13.14476562 4.1953125 13.86328125 C4.14838554 15.57611539 4.07664605 17.28823824 4 19 C1.48828125 16.95703125 1.48828125 16.95703125 -1 14 C-1.26953125 10.44921875 -1.26953125 10.44921875 -0.8125 6.6875 C-0.67457031 5.43324219 -0.53664063 4.17898437 -0.39453125 2.88671875 C-0.26433594 1.93410156 -0.13414063 0.98148438 0 0 Z " fill="#539957" transform="translate(21,31)"/>
</g>
</svg>
{{ __('Sign in with Nodeloc') }}
</a>
@endif
这样整个登录流程就处理完成,现在就可以使用Nodeloc授权登录Free.Nodeloc.com了。