首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在HumHub中使用JWT SSO/自动登录

如何在HumHub中使用JWT SSO/自动登录
EN

Stack Overflow用户
提问于 2017-11-18 01:30:55
回答 1查看 938关注 0票数 1

有没有人知道在HumHub中使用JWTs的好例子?

我在官方文档中找到了一个起点,但它没有清楚地解释应该在JWT的头部或有效负载中发送什么才能使SSO/自动登录正常工作。

是用户id、用户电子邮件...?

另外,它看起来只适用于企业版,有没有免费使用的例子?

有没有其他自动登录HumHub的方法?

EN

回答 1

Stack Overflow用户

发布于 2018-09-25 17:31:32

对于那些不知道的人来说,您需要安装企业版才能让JWT工作。如果您安装了社区版,则可以将企业版作为模块进行安装。它会要求你提供一个许可证密钥,你可以每月花99欧元购买,但如果你友好地询问他们的支持服务台,他们会给你一个试用许可证来玩:)。

humhub/protected/modules/enterprise/modules/jwt/examples/php/index.php中,有一些示例代码可用于对用户进行身份验证。

代码语言:javascript
复制
// Load JWT Libary
require __DIR__ . '/vendor/autoload.php';

use Firebase\JWT\JWT;

// Configuration
$key = 'your shared key';
$domain = "http://humhub.example.com";
$urlRewritingEnabled = false;

// Build token including your user data
$now = time();
$token = array(
    'iss' => 'example',
    'jti' => md5($now . rand()),
    'iat' => $now,
    'exp' => $now + 60,
    'username' => 'j.doe',
    'firstname' => 'John',
    'lastname' => 'Doe',
    'email' => 'john.doe@example.com',
);

// Create JWT token
$jwt = JWT::encode($token, $key);

// Redirect user back to humhub
if ($urlRewritingEnabled) {
    $location = $domain . '/user/auth/external?authclient=jwt';
} else {
    $location = $domain . '/index.php?r=/user/auth/external&authclient=jwt';
}
$location .= "&jwt=" . $jwt;

header("Location: " . $location);

所以我所做的就是将这段代码复制到我自己的PHP文件中,例如https://test.example.com/jwtclient.php,然后在humhub/protected/config/common.php中添加如下内容:

代码语言:javascript
复制
return [
    // ...
    'components' => [
        // ...
        'authClientCollection' => [
            'clients' => [
                // ...
                'jwt' => [
                    'class' => 'humhub\modules\enterprise\modules\jwt\authclient\JWT',
                    'url'       => 'https://test.example.com/jwtclient.php',
                    'sharedKey' => 'your shared key', // same as jwtclient.php
                    // Other configuration options
                    'title' => 'Company SSO Login',
                    // Automatic login, when allowed IP matches
                    'autoLogin' => true,
                    // Limit allowed JWT IPs
                    // 'allowedIPs' => ['localhost'],
                    // Leeway (seconds) for token validation
                    'leeway' => 60
                ]
            ]
        ]
        // ...
    ]
    // ...
];

如果您转到humhub.example.com,它将重定向到test.example.com/jwtclient.php,然后重定向回humhub.example.com,并将jwt附加到URL。这就是它的工作原理。

我还指出,您也不需要使用LDAP来实现这一点。只需确保您的数据库用户在表user_password中没有密码。

我希望这对你有帮助,因为它也在折磨我的大脑!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47356244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档