-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1667f4f
commit 09cf828
Showing
10 changed files
with
76 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
/* | ||
* @Author: [email protected] | ||
* @Date: 2024-06-16 17:46:33 | ||
* @LastEditors: [email protected] | ||
* @LastEditTime: 2024-08-02 10:55:54 | ||
* @Description: | ||
*/ | ||
import crypto = require('crypto'); | ||
import { default as Random } from './random'; | ||
import { getSteedosSchema } from '@steedos/objectql'; | ||
const Cookies = require('cookies'); | ||
import { setCookie, clearCookie } from '@steedos/utils'; | ||
|
||
export const hashLoginToken = function (loginToken) { | ||
const hash = crypto.createHash('sha256'); | ||
|
@@ -43,19 +50,17 @@ export const insertHashedLoginToken = async function (userId, hashedToken) { | |
return await userObject.update(userId, data); | ||
} | ||
|
||
|
||
|
||
export const setAuthCookies = function (req, res, userId, authToken, spaceId?) { | ||
let cookies = new Cookies(req, res); | ||
let options = { | ||
maxAge: 90 * 60 * 60 * 24 * 1000, | ||
httpOnly: true, | ||
overwrite: true | ||
} | ||
cookies.set("X-User-Id", userId, options); | ||
cookies.set("X-Auth-Token", authToken, options); | ||
setCookie(req, res, "X-User-Id", userId, options as any); | ||
setCookie(req, res, "X-Auth-Token", authToken, options as any) | ||
|
||
if (spaceId) { | ||
cookies.set("X-Space-Id", spaceId, options); | ||
setCookie(req, res, "X-Space-Id", spaceId, options as any); | ||
// cookies.set("X-Space-Token", spaceId + ',' + authToken, options); | ||
} | ||
|
||
|
@@ -64,16 +69,18 @@ export const setAuthCookies = function (req, res, userId, authToken, spaceId?) { | |
|
||
|
||
export const clearAuthCookies = function (req, res) { | ||
let cookies = new Cookies(req, res); | ||
let options = { | ||
maxAge: 0, | ||
httpOnly: true, | ||
overwrite: true | ||
} | ||
cookies.set("X-User-Id", null, options); | ||
cookies.set("X-Auth-Token", null, options); | ||
cookies.set("X-Access-Token", null, options); | ||
cookies.set("X-Space-Token", null, options); | ||
|
||
clearCookie(req, res, "X-User-Id", options as any) | ||
clearCookie(req, res, "X-Auth-Token", options as any) | ||
|
||
clearCookie(req, res, "X-Access-Token", options as any) | ||
clearCookie(req, res, "X-Space-Token", options as any) | ||
|
||
return; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const Cookies = require('cookies'); | ||
const psl = require('psl'); | ||
|
||
const useSubdomainCookies = process.env.STEEDOS_AUTH_USE_SUBDOMAIN_COOKIES === 'true'; | ||
|
||
// 从请求的 Host 头中提取二级域名部分 | ||
function getSubdomain(host) { | ||
const parsed = psl.parse(host); | ||
if (parsed && parsed.domain) { | ||
return parsed.domain; | ||
} | ||
return host; | ||
} | ||
|
||
export function setCookie(req, res, name, value, options = {domain: null, maxAge: 0, httpOnly: true, overwrite: true}) { | ||
const cookies = new Cookies(req, res); | ||
const host = req.headers.host; | ||
if(host && useSubdomainCookies && psl.isValid(host)){ | ||
const domain = getSubdomain(host); | ||
options.domain = `.${domain}`; // 动态设置二级域名 | ||
} | ||
cookies.set(name, value, options); | ||
} | ||
|
||
export function clearCookie(req, res, name, options = {domain: null, maxAge: 0, httpOnly: true, overwrite: true}) { | ||
const cookies = new Cookies(req, res); | ||
const host = req.headers.host; | ||
if(host && useSubdomainCookies && psl.isValid(host)){ | ||
const domain = getSubdomain(host); | ||
options.domain = `.${domain}`; // 动态设置二级域名 | ||
} | ||
options.maxAge = 0; // 通过将 maxAge 设置为 0 来清除 cookie | ||
cookies.set(name, null, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,16 @@ | |
* @Author: [email protected] | ||
* @Date: 2023-08-06 14:44:51 | ||
* @LastEditors: [email protected] | ||
* @LastEditTime: 2024-04-14 13:58:40 | ||
* @LastEditTime: 2024-08-02 10:22:51 | ||
* @Description: | ||
*/ | ||
|
||
export * from './queryMetadata'; | ||
export * from './defaultsDeep'; | ||
export * from './settings' | ||
|
||
export * from './cookies'; | ||
|
||
export async function sleep(ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters