chore: add authorize page

This commit is contained in:
FoskyM 2023-09-29 14:47:36 +08:00
parent 386664042e
commit 6e6055c44a
No known key found for this signature in database
GPG key ID: 42C0ED6994AD7E9C
8 changed files with 105 additions and 10 deletions

View file

@ -12,13 +12,21 @@
namespace FoskyM\OAuthCenter; namespace FoskyM\OAuthCenter;
use Flarum\Extend; use Flarum\Extend;
use FoskyM\OAuthCenter\Middlewares\ResourceScopeMiddleware;
return [ return [
(new Extend\Frontend('forum')) (new Extend\Frontend('forum'))
->js(__DIR__.'/js/dist/forum.js') ->js(__DIR__.'/js/dist/forum.js')
->css(__DIR__.'/less/forum.less'), ->css(__DIR__.'/less/forum.less')
->route('/oauth/authorize', 'oauth.authorize'),
(new Extend\Frontend('admin')) (new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js') ->js(__DIR__.'/js/dist/admin.js')
->css(__DIR__.'/less/admin.less'), ->css(__DIR__.'/less/admin.less'),
new Extend\Locales(__DIR__.'/locale'), new Extend\Locales(__DIR__.'/locale'),
(new Extend\Routes('forum'))
->post('/oauth/authorize', 'oauth.authorize.post', Controllers\AuthorizeController::class),
(new Extend\Middleware('api'))->add(ResourceScopeMiddleware::class),
]; ];

BIN
js/dist/forum.js generated vendored

Binary file not shown.

BIN
js/dist/forum.js.map generated vendored

Binary file not shown.

View file

@ -0,0 +1,30 @@
import app from 'flarum/forum/app';
import {extend} from 'flarum/common/extend';
import Page from 'flarum/common/components/Page';
import IndexPage from 'flarum/forum/components/IndexPage';
import LogInModal from 'flarum/forum/components/LogInModal';
import extractText from 'flarum/common/utils/extractText';
export default class AuthorizePage extends IndexPage {
oninit(vnode) {
super.oninit(vnode);
if (!app.session.user) {
setTimeout(() => app.modal.show(LogInModal), 500);
}
const params = m.route.param();
}
setTitle() {
app.setTitle(extractText(app.translator.trans('foskym-oauth-center.forum.page.title.authorize')));
app.setTitleCount(0);
}
view() {
return (
<div className="AuthorizePage">
<div className="container">
</div>
</div>
);
}
}

View file

@ -1,12 +1,12 @@
import UserPage from 'flarum/forum/components/UserPage'; import UserPage from 'flarum/forum/components/UserPage';
export default class AuthorizePage extends UserPage { export default class AuthorizedPage extends UserPage {
oninit(vnode) { oninit(vnode) {
super.oninit(vnode); super.oninit(vnode);
this.loadUser(m.route.param('username')); this.loadUser(m.route.param('username'));
} }
content() { content() {
return ( return (
<div className="AuthorizePage"> <div className="AuthorizedPage">
</div> </div>
); );
} }

View file

@ -2,23 +2,29 @@ import app from 'flarum/forum/app';
import {extend} from 'flarum/common/extend'; import {extend} from 'flarum/common/extend';
import UserPage from 'flarum/forum/components/UserPage'; import UserPage from 'flarum/forum/components/UserPage';
import LinkButton from 'flarum/common/components/LinkButton'; import LinkButton from 'flarum/common/components/LinkButton';
import AuthorizePage from "./components/user/AuthorizePage"; import AuthorizePage from "./components/oauth/AuthorizePage";
import AuthorizedPage from "./components/user/AuthorizedPage";
app.initializers.add('foskym/flarum-oauth-center', () => { app.initializers.add('foskym/flarum-oauth-center', () => {
app.routes['user.authorize'] = { app.routes['oauth.authorize'] = {
path: '/u/:username/authorize', path: '/oauth/authorize',
component: AuthorizePage component: AuthorizePage
}; };
app.routes['user.authorized'] = {
path: '/u/:username/authorized',
component: AuthorizedPage
};
extend(UserPage.prototype, 'navItems', function (items) { extend(UserPage.prototype, 'navItems', function (items) {
if (app.session.user && app.session.user.id() === this.user.id()) { if (app.session.user && app.session.user.id() === this.user.id()) {
items.add( items.add(
'authorize', 'authorized',
LinkButton.component( LinkButton.component(
{ {
href: app.route('user.authorize', { username: this.user.username() }), href: app.route('user.authorized', { username: this.user.username() }),
icon: 'fas fa-user-friends', icon: 'fas fa-user-friends',
}, },
[ [
app.translator.trans('foskym-oauth-center.forum.page.label.authorize'), app.translator.trans('foskym-oauth-center.forum.page.label.authorized'),
// this.user.moderatorNoteCount() > 0 ? <span className="Button-badge">{this.user.moderatorNoteCount()}</span> : '', // this.user.moderatorNoteCount() > 0 ? <span className="Button-badge">{this.user.moderatorNoteCount()}</span> : '',
] ]
), ),

View file

@ -3,5 +3,7 @@ foskym-oauth-center:
forum: forum:
page: page:
label: title:
authorize: 授权 authorize: 授权
label:
authorized: 授权记录

View file

@ -0,0 +1,49 @@
<?php
namespace FoskyM\OAuthCenter\Controllers;
use Flarum\User\User;
use Flarum\Http\RequestUtil;
use FoskyM\OAuthCenter\OAuth;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Laminas\Diactoros\Response\JsonResponse;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Group\Group;
class AuthorizeController implements RequestHandlerInterface
{
protected $settings;
public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$actor = RequestUtil::getActor($request);
$actor->assertRegistered();
$params = $request->getParsedBody();
$oauth = new OAuth();
$server = $oauth->server();
$request = $oauth->request()::createFromGlobals();
$response = $oauth->response();
if (!$server->validateAuthorizeRequest($request, $response)) {
$response->send();
die;
}
$is_authorized = (Arr::get($params, 'authorized', 'no') === 'yes');
$server->handleAuthorizeRequest($request, $response, $is_authorized, $actor->id);
if ($is_authorized) {
// this is only here so that you get to see your code in the cURL request. Otherwise, we'd redirect back to the client
/* $code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40);
exit("SUCCESS! Authorization Code: $code");*/
}
$response->send();
}
}