diff --git a/application/modules/oauth.module.php b/application/modules/oauth.module.php new file mode 100644 index 0000000..b9cc6db --- /dev/null +++ b/application/modules/oauth.module.php @@ -0,0 +1,71 @@ +state(); + return redirect($oauth->authorization_url()); +} + +else if (url_is('/oauth/callback')) { + check_parameters(['code', 'state']); + + $code = get_param('code'); + $state = get_param('state'); + + # Recommended security checks + # https://oauth2-client.thephpleague.com/usage/ + if (isset($_SESSION['oauth_state']) && $state !== $_SESSION['oauth_state']) { + if (isset($_SESSION['oauth_state'])) { + unset($_SESSION['oauth_state']); + } + throw http_error(400, 'Invalid state.'); + } + + # error_log("access_token:$access_token"); + $_SESSION['oauth_access_token'] = $oauth->access_token($code); + + return redirect('/oauth/membership'); +} + +else if (url_is('/oauth/membership')) { + $access_token = $_SESSION['oauth_access_token']; + + $authenticated_user = $oauth->authenticated_user($access_token); + if (empty($authenticated_user)) { + return redirect('/oauth/connect'); + } + + # error_log(json_encode($authenticated_user)); + $_SESSION['oauth_authenticated_user'] = $authenticated_user; + + $member_of = $authenticated_user['memberOf']['nodes']; + # If member of blogmarks on Open Collective + if (!empty($member_of)) { + $user = table('users')->get_one('email', $authenticated_user['email']); + # If user found + if ($user) { + signin($user); + } else { + $user = table('users')->get_one('login', $authenticated_user['slug']); + if ($user) { + throw http_error(400, 'Login is already taken.'); + } + $params = [ + 'name' => $authenticated_user['name'], + 'login' => $authenticated_user['slug'], + 'email' => $authenticated_user['email'], + ]; + $user = table('users')->create($params); + signin($user); + } + return redirect(get_param('redirect_url', '/my/')); + } + + title(_('Membership')); + return render('oauth/membership'); +} + +else { + return unknown_url(); +} diff --git a/application/partials/top.partial.php b/application/partials/top.partial.php index cc93c60..39ec76c 100644 --- a/application/partials/top.partial.php +++ b/application/partials/top.partial.php @@ -20,11 +20,15 @@ / - - - / + + + + + + / + + -