Skip to content

Commit

Permalink
fix: update token split method
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jul 30, 2021
1 parent 3f7201c commit c43ecee
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
11 changes: 4 additions & 7 deletions packages/app-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as express from 'express'
import { parseToken } from './lib/utils/token'
import { parseToken, splitBearerToken } from './lib/utils/token'
import { v4 as uuidv4 } from 'uuid'
import Config from './config'
import { router } from './router/index'
Expand All @@ -13,23 +13,20 @@ const server = express()
server.use(express.json())

// 服务端开放跨域
server.all('*', function (req, res, next) {
server.all('*', function (_req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Authorization, Content-Type')
res.header('Access-Control-Allow-Methods', '*')
req['requestId'] = uuidv4()
next()
})

// 解析 Bearer Token
server.use(function (req, _res, next) {
const bearer = req.headers['authorization'] ?? ''
const splitted = bearer.split(' ')
const token = splitted.length === 2 ? splitted[1] : ''
const token = splitBearerToken(req.headers['authorization'] ?? '')
const auth = parseToken(token) || null
req['auth'] = auth

const requestId = req['requestId'] || uuidv4()
const requestId = req['requestId'] = uuidv4()
logger.info(`[${requestId}] ${req.path} start request`)
logger.debug(`[${requestId}] auth: ` + JSON.stringify(auth))
next()
Expand Down
19 changes: 18 additions & 1 deletion packages/app-server/src/lib/utils/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ export function getToken(payload: any): string {
return jwt.sign(payload, secret)
}

// 解析 token
/**
* 解析 token
* @param token
* @returns
*/
export function parseToken(token: string): any | null {
if (!token) return null
try {
Expand All @@ -22,3 +26,16 @@ export function parseToken(token: string): any | null {
return null
}
}

/**
* 将 bearer 形式的授权数据中把 token 分出来
* @param bearer "Bearer xxxxx"
* @returns
*/
export function splitBearerToken(bearer: string): string | null {
if(!bearer) return null

const splitted = bearer?.split(' ')
const token = splitted?.length === 2 ? splitted[1] : null
return token
}
34 changes: 17 additions & 17 deletions packages/devops-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ server.use(function (req, _res, next) {
const auth = parseToken(token) || null
req['auth'] = auth

const requestId = req['requestId'] = uuidv4()
const requestId = req['requestId'] = uuidv4()
logger.info(`[${requestId}] ${req.path} start request`)
logger.debug(`[${requestId}] auth: ` + JSON.stringify(auth))
next()
Expand All @@ -33,21 +33,21 @@ server.listen(Config.PORT, () => console.log(`listened on ${Config.PORT}`))
/**
* 监听云函数、触发器变化 & 部署
*/
{
{
const acc = Globals.sys_accessor
acc.ready.then(() => {
acc.db
.watch([], { fullDocument: 'updateLookup' })
.on('change', doc => {
const coll = doc.ns.coll
if(coll === '__functions') {
deployFunctions()
}

if(coll === '__triggers') {
deployTriggers()
}
})
})
acc.ready.then(() => {
acc.db
.watch()
.on('change', doc => {
const coll = doc.ns.coll
if (coll === '__functions') {
// TODO 此处调试时有问题,客户端保存后,函数被清除,无法获取
deployFunctions().then(() => logger.debug('__functions changed: deployed'))
}

if (coll === '__triggers') {
deployTriggers().then(() => logger.debug('__triggers changed: deployed'))
}
})
})
}

0 comments on commit c43ecee

Please sign in to comment.