资讯专栏INFORMATION COLUMN

nextcloud与oauth2集成教程

seasonley / 1610人阅读

摘要:基础环境安装在官方应用商店下载解压并拷贝至目录打开并在应用中启用配置使用管理员账号打开找到设置,选择并添加信息,如下设置只使用登录,取消系统登录编辑添加以下配置使用注销使用中发现点击登出后,只是注销了本身的,并没有注销的,因此会登出失败打开

基础环境

Nextcloud 15.0.5

oauth2

安装 sociallogin

在官方app应用商店下载 sociallogin

解压并拷贝 sociallogin 至 app 目录

打开 Nextcloud 并在 应用中启用

配置oauth2

使用管理员账号打开 Nextcloud 找到 sociallogin设置,选择 Custom OAuth2 并添加oauth2信息,如下

设置只使用oauth2登录,取消系统登录
编辑 /var/www/html/config/config.php 添加以下配置:

"social_login_auto_redirect" => true

使用Oauth2注销 Nextcloud

使用中发现点击登出后,Nextcloud只是注销了本身的session,并没有注销oauth2的session,因此会登出失败.
打开 core/Controller/LoginController.php 找到 logout 方法,修改 $response 值如下:

$response = new RedirectResponse("http://10.0.4.3/logout?redirect_uri=http://10.0.4.33:8088");

http://10.0.4.3/logout 为oauth2认证系统登出地址

使用用户信息创建组

在使用oauth2登录成功后,为了与原有Nextcloud用户区分,Nextcloud会在数据库中创建一个用户名为 oauth2 Internal name + 登录名称的用户,这样使用起来及其不方便,我们可以通过修改以下代码,保证用户名正常不带前缀:

根据返回用户信息中其他信息创建组

sociallogin/lib/Controller/LoginController.php

private function login($uid, Profile $profile)
{
    $user = $this->userManager->get($uid);
    if (null === $user) {
        $connectedUid = $this->socialConnect->findUID($uid);
        $user = $this->userManager->get($connectedUid);
    }
    if ($this->userSession->isLoggedIn()) {
        if (!$this->config->getAppValue($this->appName, "allow_login_connect")) {
            throw new LoginException($this->l->t("Social login connect is disabled"));
        }
        if (null !== $user) {
            throw new LoginException($this->l->t("This account already connected"));
        }
        $currentUid = $this->userSession->getUser()->getUID();
        $this->socialConnect->connectLogin($currentUid, $uid);
        return new RedirectResponse($this->urlGenerator->linkToRoute("settings.PersonalSettings.index", ["section" => "additional"]));
    }
    if (null === $user) {
        if ($this->config->getAppValue($this->appName, "disable_registration")) {
            throw new LoginException($this->l->t("Auto creating new users is disabled"));
        }
        if (
            $profile->email && $this->config->getAppValue($this->appName, "prevent_create_email_exists")
            && count($this->userManager->getByEmail($profile->email)) !== 0
        ) {
            throw new LoginException($this->l->t("Email already registered"));
        }
        $password = substr(base64_encode(random_bytes(64)), 0, 30);
        $user = $this->userManager->createUser($uid, $password);
        $user->setDisplayName($profile->displayName ?: $profile->identifier);
        $user->setEMailAddress((string)$profile->email);

        $newUserGroup = $this->config->getAppValue($this->appName, "new_user_group");
        if ($newUserGroup) {
            try {
                $group = $this->groupManager->get($newUserGroup);
                $group->addUser($user);
            } catch (Exception $e) {
            }
        }

        if ($profile->photoURL) {
            $curl = new Curl();
            try {
                $photo = $curl->request($profile->photoURL);
                $avatar = $this->avatarManager->getAvatar($uid);
                $avatar->set($photo);
            } catch (Exception $e) {
            }
        }
        $this->config->setUserValue($uid, $this->appName, "disable_password_confirmation", 1);
        if ($profile->data["departmentName"] !== null) {
            $existGroup = $this->groupManager->get($profile->data["departmentName"]);
            if ($existGroup === null) {
                $newGroup = $this->groupManager->createGroup($profile->data["departmentName"]);
                $newGroup->addUser($user);
            } else {
                $existGroup->addUser($user);
            }
        }
    }

    $this->userSession->completeLogin($user, ["loginName" => $user->getUID(), "password" => null]);
    $this->userSession->createSessionToken($this->request, $user->getUID(), $user->getUID());

    if ($redirectUrl = $this->session->get("login_redirect_url")) {
        return new RedirectResponse($redirectUrl);
    }

    $this->session->set("last-password-confirm", time());

    return new RedirectResponse($this->urlGenerator->getAbsoluteURL("/"));
}

sociallogin/lib/Provider/CustomOAuth2.php

public function getUserProfile()
    {
        $profileFields = array_filter(
            array_map("trim", explode(",", $this->config->get("profile_fields"))),
            function ($val) {
                return !empty($val);
            }
        );
        $profileUrl = $this->config->get("endpoints")->get("profile_url");

        if (count($profileFields) > 0) {
            $profileUrl .= (strpos($profileUrl, "?") !== false ? "&" : "?") . "fields=" . implode(",", $profileFields);
        }
        $response = $this->apiRequest($profileUrl);
        if (!isset($response->identifier) && isset($response->id)) {
            $response->identifier = $response->id;
        }
        if (!isset($response->identifier) && isset($response->data->id)) {
            $response->identifier = $response->data->id;
        }
        if (!isset($response->identifier) && isset($response->user_id)) {
            $response->identifier = $response->user_id;
        }
        $data = new DataCollection($response);
        if (!$data->exists("identifier")) {
            throw new UnexpectedApiResponseException("Provider API returned an unexpected response.");
        }
        $userProfile = new UserProfile();
        foreach ($data->toArray() as $key => $value) {
            if (property_exists($userProfile, $key)) {
                $userProfile->$key = $value;
            }
        }
        if (!empty($userProfile->email)) {
            $userProfile->emailVerified = $userProfile->email;
        }
        $attributes = new DataCollection($data->get("attributes"));
        $userProfile->data = [
            "organizationName" => $attributes->get("organizationName"),
            "departmentName" => $attributes->get("departmentName"),
        ];
        if ($attributes->get("name") !== null) {
            $userProfile->displayName = $attributes->get("name");
        }
        return $userProfile;
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/31153.html

相关文章

  • 【Microsoft Azure 的1024种玩法】四. 利用Azure Virtual machi

    摘要:它的客户端覆盖了等各种平台,也提供了网页端以及接口,所以你几乎可以在各种设备上方便地访问你的云盘。【简介】 1.Azure Virtual machines是Azure 提供的多种可缩放按需分配计算资源之一,Nextcloud是一款开源免费的私有云存储网盘项目,可以让你快速便捷地搭建一套属于自己或团队的云同步网盘,从而实现跨平台跨设备文件同步、共享、版本控制、团队协作等功能。它的客...

    Corwien 评论0 收藏0
  • 17k Star!打造私人版的某度网盘,限速也不怕

    摘要:总的来说,适用于个人搭建家庭网盘,也适合大家公司内部私有云网盘。支持显示消息状态指示器举手功能群组对话说明可折叠的视频栏全屏屏幕共享。。参与维护万的开源技术资源库,包括等。 ...

    vincent_xyb 评论0 收藏0
  • Nextcloud个人云存储绝佳选择:一键自动安装方法和云盘使用体验

    搭建个人云存储一般会想到ownCloud,堪称是自建云存储服务的经典。而Nextcloud是ownCloud原开发团队打造的号称是下一代存储。初一看觉得口气不小,刚推出来就重新定义了Cloud,真正试用过后就由衷地赞同这个Nextcloud:它是个人云存储服务的绝佳选择。 与ownCloud相比,Nextcloud的功能丝毫没有减弱,甚至由于可以安装云存储服务应用,自制性更强,也更符合用户的...

    Shisui 评论0 收藏0
  • 开源网盘系统nextcloud容器化部署

    一、Nextcloud简介Nextcloud是一款开源免费的私有云存储网盘项目,可以让你快速便捷地搭建一套属于自己或团队的云同步网盘,从而实现跨平台跨设备文件同步、共享、版本控制、团队协作等功能。它的客户端覆盖了Windows、Mac、Android、iOS、Linux 等各种平台,也提供了网页端以及 WebDAV接口,所以你几乎可以在各种设备上方便地访问你的云盘。官网地址:https://nex...

    社区管理员 评论0 收藏0

发表评论

1条评论

wbh849875651

评论于2022-09-01 15:42

你好,对于sociallogin的一些配置有些问题想请教你,请问可以留个邮箱或者其他联系方式吗?
回复0 赞同0 删除
最新活动
阅读需要支付1元查看
<