Comparing version 2.0.6 to 2.0.7-alpha.0
@@ -47,3 +47,2 @@ import { Context } from 'egg'; | ||
private parseState; | ||
private generateId; | ||
} |
@@ -198,3 +198,3 @@ "use strict"; | ||
this.authHandler = kauthConfig.authHandler; | ||
const router = new KauthRouter(); | ||
const router = new KauthRouter(kauthConfig.apiInjected ? kauthApi : undefined, kauthConfig.tracerInjected); | ||
router.route(kauthConfig.logoutUri, this.logout); | ||
@@ -214,10 +214,2 @@ router.route(kauthConfig.moziAuthorizeUri, this.redirectMoziAuthorizeUri); | ||
this.permissionHandler = async (ctx, userId) => { | ||
// 根据 egg 规范,如果存在 tracer,那么将会保持在 ctx.tracer 中 | ||
if (ctx.tracer) { | ||
const tracerId = ctx.tracer.id; | ||
// 某些情况下,可能会出现 spanId 不存在的情况,这时候需要自己设置 spanId | ||
const spanId = ctx.tracer.spanId || this.generateId(8); | ||
kauthApi.setTraceId(tracerId); | ||
kauthApi.setSpanId(spanId); | ||
} | ||
const permissions = await kauthApi.listPermissionsByUserId(userId); | ||
@@ -246,25 +238,9 @@ ctx.permissions = permissions; | ||
} | ||
generateId(bytes) { | ||
const spanBuffer = Buffer.allocUnsafe(bytes); | ||
for (let i = 0; i < bytes / 4; i++) { | ||
// unsigned right shift drops decimal part of the number | ||
// it is required because if a number between 2**32 and 2**32 - 1 is generated, an out of range error is thrown by writeUInt32BE | ||
spanBuffer.writeUInt32BE((Math.random() * 2 ** 32) >>> 0, i * 4); | ||
} | ||
// If buffer is all 0, set the last byte to 1 to guarantee a valid w3c id is generated | ||
for (let i = 0; i < bytes; i++) { | ||
if (spanBuffer[i] > 0) { | ||
break; | ||
} | ||
else if (i === bytes - 1) { | ||
spanBuffer[bytes - 1] = 1; | ||
} | ||
} | ||
return spanBuffer.toString('hex', 0, bytes); | ||
} | ||
} | ||
exports.KauthProvider = KauthProvider; | ||
class KauthRouter { | ||
constructor() { | ||
constructor(kauthApi, tracerInjected) { | ||
this._routes = {}; | ||
this._kauthApi = kauthApi; | ||
this._tracerInjected = tracerInjected; | ||
} | ||
@@ -277,3 +253,17 @@ route(path, callback) { | ||
} | ||
prepare(ctx) { | ||
if (this._kauthApi) { | ||
ctx.kauthApi = this._kauthApi; | ||
if (this._tracerInjected && ctx.tracer) { | ||
// 根据 egg 规范,如果存在 tracer,那么将会保持在 ctx.tracer 中 | ||
const tracerId = ctx.tracer.id; | ||
// 某些情况下,可能会出现 spanId 不存在的情况,这时候需要自己设置 spanId | ||
const spanId = ctx.tracer.spanId || this.generateId(8); | ||
this._kauthApi.setTraceId(tracerId); | ||
this._kauthApi.setSpanId(spanId); | ||
} | ||
} | ||
} | ||
async dispatch(ctx, next) { | ||
this.prepare(ctx); | ||
const callback = this._routes[ctx.request.path]; | ||
@@ -286,2 +276,20 @@ if (callback) { | ||
} | ||
generateId(bytes) { | ||
const spanBuffer = Buffer.allocUnsafe(bytes); | ||
for (let i = 0; i < bytes / 4; i++) { | ||
// unsigned right shift drops decimal part of the number | ||
// it is required because if a number between 2**32 and 2**32 - 1 is generated, an out of range error is thrown by writeUInt32BE | ||
spanBuffer.writeUInt32BE((Math.random() * 2 ** 32) >>> 0, i * 4); | ||
} | ||
// If buffer is all 0, set the last byte to 1 to guarantee a valid w3c id is generated | ||
for (let i = 0; i < bytes; i++) { | ||
if (spanBuffer[i] > 0) { | ||
break; | ||
} | ||
else if (i === bytes - 1) { | ||
spanBuffer[bytes - 1] = 1; | ||
} | ||
} | ||
return spanBuffer.toString('hex', 0, bytes); | ||
} | ||
} |
{ | ||
"name": "egg-kauth", | ||
"version": "2.0.6", | ||
"version": "2.0.7-alpha.0", | ||
"description": "egg kauth plugin", | ||
@@ -5,0 +5,0 @@ "eggPlugin": { |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
24456
539
2