diff --git a/extend.php b/extend.php index d2e6144..293c788 100644 --- a/extend.php +++ b/extend.php @@ -12,13 +12,21 @@ namespace FoskyM\OAuthCenter; use Flarum\Extend; +use FoskyM\OAuthCenter\Middlewares\ResourceScopeMiddleware; return [ (new Extend\Frontend('forum')) ->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')) ->js(__DIR__.'/js/dist/admin.js') ->css(__DIR__.'/less/admin.less'), 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), ]; diff --git a/js/dist/forum.js b/js/dist/forum.js index 501e3ef..d1604e5 100644 Binary files a/js/dist/forum.js and b/js/dist/forum.js differ diff --git a/js/dist/forum.js.map b/js/dist/forum.js.map index adc717a..00df114 100644 Binary files a/js/dist/forum.js.map and b/js/dist/forum.js.map differ diff --git a/js/src/forum/components/oauth/AuthorizePage.js b/js/src/forum/components/oauth/AuthorizePage.js new file mode 100644 index 0000000..e853bd6 --- /dev/null +++ b/js/src/forum/components/oauth/AuthorizePage.js @@ -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 ( +
+
+
+
+ ); + } +} diff --git a/js/src/forum/components/user/AuthorizePage.js b/js/src/forum/components/user/AuthorizedPage.js similarity index 68% rename from js/src/forum/components/user/AuthorizePage.js rename to js/src/forum/components/user/AuthorizedPage.js index 4bd4482..e55e596 100644 --- a/js/src/forum/components/user/AuthorizePage.js +++ b/js/src/forum/components/user/AuthorizedPage.js @@ -1,12 +1,12 @@ import UserPage from 'flarum/forum/components/UserPage'; -export default class AuthorizePage extends UserPage { +export default class AuthorizedPage extends UserPage { oninit(vnode) { super.oninit(vnode); this.loadUser(m.route.param('username')); } content() { return ( -
+
); } diff --git a/js/src/forum/index.js b/js/src/forum/index.js index 207d915..3ba3f21 100644 --- a/js/src/forum/index.js +++ b/js/src/forum/index.js @@ -2,23 +2,29 @@ import app from 'flarum/forum/app'; import {extend} from 'flarum/common/extend'; import UserPage from 'flarum/forum/components/UserPage'; 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.routes['user.authorize'] = { - path: '/u/:username/authorize', + app.routes['oauth.authorize'] = { + path: '/oauth/authorize', component: AuthorizePage }; + + app.routes['user.authorized'] = { + path: '/u/:username/authorized', + component: AuthorizedPage + }; extend(UserPage.prototype, 'navItems', function (items) { if (app.session.user && app.session.user.id() === this.user.id()) { items.add( - 'authorize', + 'authorized', 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', }, [ - app.translator.trans('foskym-oauth-center.forum.page.label.authorize'), + app.translator.trans('foskym-oauth-center.forum.page.label.authorized'), // this.user.moderatorNoteCount() > 0 ? {this.user.moderatorNoteCount()} : '', ] ), diff --git a/locale/zh-Hans.yml b/locale/zh-Hans.yml index a95be51..b32d92d 100644 --- a/locale/zh-Hans.yml +++ b/locale/zh-Hans.yml @@ -3,5 +3,7 @@ foskym-oauth-center: forum: page: - label: + title: authorize: 授权 + label: + authorized: 授权记录 diff --git a/src/Controllers/AuthorizeController.php b/src/Controllers/AuthorizeController.php new file mode 100644 index 0000000..56f3eeb --- /dev/null +++ b/src/Controllers/AuthorizeController.php @@ -0,0 +1,49 @@ +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(); + } +}