我给开源软件hoppscotch增加了sso单点登陆功能
因为我的服务放到了公网上面,所以默认的系统不登陆也能使用,于是我加了登陆页面,当用户未登陆时,自动跳转到登陆页面,如下
2.系统默认的登陆是email/github之类的,太不方便,于是我增加了SSO登陆的功能(配置我自己搭建的SSO系统authentik),完美实现登陆。
只需要在selfthost-web上,增加Login page,打到后替换原来的/site
3.后端backend api,增加sso callback api
auth.controller.ts增加:
/**
** Route to initiate SSO auth via Microsoft
*/
@Get('oidc')
@UseGuards(OIDCSSOGuard)
async OIDCAuth(@Request() req) {}
/**
** Callback URL for SSO
*/
@Get('oidc/callback')
@SkipThrottle()
//@UseGuards(OIDCSSOGuard)
//@UseInterceptors(UserLastLoginInterceptor)
async OIDCAuthRedirect(@Query('code') code: string, @Query('state') state: string, @Request() req, @Res() res) {
console.log('================oidc/callback==================')
console.log(code);
console.log(state);
/* 1. 换 token */
const redirectUri='https://api.xxxxxx.cn/backend/v1/auth/oidc/callback';
const tokenRes = await this.authService.exchangeCode(code,redirectUri);
console.log(tokenRes);
console.log('res.access_token:'+res.access_token);
/* 2. 拿用户信息 */
const userInfoEndpoint ='https://sso.xxxxxx.cn/application/o/userinfo/'
const userInfo = await this.authService.fetchUserInfo(res.access_token, userInfoEndpoint);
/* 3. 找到 / 创建 Hoppscotch 内部用户
规则:根据 email 或 sub 建立映射
*/
let user = await this.authService.getUserByEmail(userInfo['email']);
console.log('user:'+JSON.stringify(user));
if(user == null){
throwHTTPErr({
message: 'user not found',
statusCode: 404,
});
}
/* 4. 生成 Hoppscotch JWT */
const authTokens = await this.authService.generateAuthTokens(user['value']['uid']);
if (E.isLeft(authTokens)) throwHTTPErr(authTokens.left);
/* 5. 写回浏览器(同现有 Email/OAuth 登录逻辑) */
authCookieHandler(
res,
authTokens.right,
true,
state,
this.configService,
);
}
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · 原创!无插件hack方式实现conflulence open connect sso登陆
- · 修改hoppscotch支持必须登陆指定用户才可以使用
- · 单点登录实现——基于OAuth2.0协议的接入方案
- · 单点登录SSO的实现原理:asp.net Forms身份验证详解(三)
- · 单点登录SSO的实现原理:附源码(二)
- · 单点登录SSO的实现原理(一)
- · 软件安全:OWASP top 10详解
- · Emby破解教程(1):Emby.Server.Implementations.dll 修改
- · navicat premium15中文破解版 v15.0.23 32/64位
- · Unity Pro v2019.2.6f1 for Mac 永久激活版
- · 使用meta绕过referrer防盗链验证
- · vs2013 密钥

