Skip to content

Commit

Permalink
simplifu usercontext
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyperstore committed Mar 30, 2018
1 parent 124aa73 commit 761e81f
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 324 deletions.
360 changes: 186 additions & 174 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@types/fast-stats": "0.0.29",
"@types/jsonwebtoken": "^7.2.5",
"@types/mocha": "^2.2.46",
"@types/mongodb": "^2.2.18",
"@types/mongodb": "^3.0.9",
"chai": "^4.1.2",
"mocha": "^5.0.0",
"tslint": "^5.9.1",
Expand All @@ -37,20 +37,20 @@
"dependencies": {
"amqplib": "^0.5.2",
"fast-stats": "0.0.3",
"graphql": "^0.12.3",
"graphql": "^0.13.2",
"jaeger-client": "^3.7.0",
"jsonwebtoken": "^8.1.0",
"moment": "^2.20.1",
"mongodb": "^2.2.34",
"prom-client": "^10.2.2",
"prom-client": "^11.0.0",
"reflect-metadata": "^0.1.3",
"router": "^1.3.2",
"rxjs": "^5.5.6",
"swagger-ui-dist": "^3.9.0",
"unirest": "^0.5.0",
"uuid": "^3.0.1",
"validator": "^9.2.0",
"zipkin": "^0.10.1",
"zipkin-transport-http": "^0.10.1"
"zipkin": "^0.12.0",
"zipkin-transport-http": "^0.12.0"
}
}
1 change: 0 additions & 1 deletion src/commands/commandFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export function Command(config: CommandConfiguration = {}, commandKey?: string,
};
}


export function CommandEntryPoint(ignore=false) {
return function (command, key: string, pdesc: PropertyDescriptor) {
let endpoints = command.constructor[entryPointSymbol] || {};
Expand Down
2 changes: 1 addition & 1 deletion src/security/authorizationPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class DefaultAuthorizationPolicy {
* x-yz x-* true
*
* @param {string} scope
* @returns {number}
* @returns {boolean}
*/
hasScope(sec: SecurityContext, handlerScope: string): boolean {
if (!handlerScope || handlerScope === "?" || Service.isDevelopment) {
Expand Down
49 changes: 4 additions & 45 deletions src/security/securityContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { DefaultServiceNames, Inject } from "../di/annotations";
import { IContainer } from '../di/resolvers';
import { IRequestContext } from '../pipeline/common';
import { TokenService } from './services/tokenService';
import { ApiKeyService } from './services/apiKeyService';

export interface IAuthenticationStrategy {
name: string;
Expand All @@ -16,38 +15,19 @@ export interface IAuthenticationStrategy {
}

export interface UserContextData {
/**
* User display name
*
* @type {string}
* @memberOf UserContext
*/
displayName?: string;
/**
* User email
*
* @type {string}
* @memberOf UserContext
*/
email?: string;
/**
* User name
*
* @type {string}
* @memberOf UserContext
*/
name: string;
/**
*
*
* @type {string}
* @memberOf UserContext
*/
tenant: string;
tenant?: string;

scopes: string[];

claims: any;
claims?: any;
}

export interface UserContext extends UserContextData {
Expand All @@ -69,14 +49,13 @@ export interface UserToken extends UserContext {
*/
export class SecurityContext implements UserContext {
private static EmptyScopes: string[] = [];
private static UserFields = ["name", "displayName", "email", "scopes", "tenant", "bearer", "claims"];
private static UserFields = ["name", "scopes", "tenant", "bearer", "claims"];

private strategies = new Map<string, IAuthenticationStrategy>();

constructor(container: IContainer, private scopePolicy: IAuthorizationPolicy) {
// Default
this.addOrReplaceStrategy(new TokenService());
this.addOrReplaceStrategy(new ApiKeyService());

let strategies = container.getList<IAuthenticationStrategy>(DefaultServiceNames.AuthenticationStrategy);
for(let strategy of strategies) {
Expand All @@ -88,20 +67,6 @@ export class SecurityContext implements UserContext {
this.strategies.set(strategy.name.toLowerCase(), strategy);
}

/**
* User display name
*
* @type {string}
* @memberOf UserContext
*/
displayName?: string;
/**
* User email
*
* @type {string}
* @memberOf UserContext
*/
email?: string;
/**
* User name
*
Expand Down Expand Up @@ -146,8 +111,6 @@ export class SecurityContext implements UserContext {
else if (tenantOrCtx) {
this.tenant = tenantOrCtx.tenant;
this.name = tenantOrCtx.name;
this.displayName = tenantOrCtx.displayName || tenantOrCtx.name;
this.email = tenantOrCtx.email;
this._scopes = tenantOrCtx.scopes;
this.claims = tenantOrCtx.claims;
}
Expand All @@ -167,7 +130,7 @@ export class SecurityContext implements UserContext {
// Anonymous
this.name = "Anonymous";
this._isAnonymous = true;
this.claims = [];
this.claims = {};
ctx.logInfo(() => `No authentication context: User access is anonymous `);
return;
}
Expand All @@ -186,8 +149,6 @@ export class SecurityContext implements UserContext {
let userContext = await strategy.verifyToken(ctx, token, this.tenant);
if (userContext) {
this.name = userContext.name;
this.displayName = userContext.displayName;
this.email = userContext.email;
this._scopes = userContext.scopes;
this.tenant = userContext.tenant || this.tenant;
this.bearer = (<UserToken>userContext).bearer;
Expand Down Expand Up @@ -224,8 +185,6 @@ export class SecurityContext implements UserContext {
getUserContext(): UserContextData {
return {
tenant: this.tenant,
displayName: this.displayName,
email: this.email,
name: this.name,
scopes: this._scopes,
claims: this.claims
Expand Down
25 changes: 0 additions & 25 deletions src/security/services/apiKeyCommand.ts

This file was deleted.

60 changes: 0 additions & 60 deletions src/security/services/apiKeyService.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/security/services/tokenService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Injectable, LifeTime, DefaultServiceNames, Inject } from '../../di/annotations';
import { Injectable, LifeTime, DefaultServiceNames } from '../../di/annotations';
import { Conventions } from '../../utils/conventions';
import { Service } from '../../globals/system';
import { IDynamicProperty } from '../../configurations/abstractions';
import { ConfigurationProperty } from '../../globals/manifest';
import { IAuthenticationStrategy, UserContextData } from "../securityContext";
Expand Down Expand Up @@ -29,12 +28,13 @@ export class TokenService implements IAuthenticationStrategy {

createToken( user: UserContextData ): Promise<{ expiresIn: number, token: string, renewToken: string }> {

if (!user || !user.name)
throw new Error("Invalid user data. name is required");

return new Promise(async (resolve, reject) => {
const payload = {
value:
{
displayName: user.displayName,
email: user.email,
name: user.name,
tenant: user.tenant,
scopes: user.scopes,
Expand Down
9 changes: 3 additions & 6 deletions test/command/circuitBreaker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import ActualTime from "../../src/utils/actualTime";
beforeEach(function () {
ActualTime.enableVirtualTimer();
DynamicConfiguration.reset();
CommandMetricsFactory.resetCache();
CircuitBreakerFactory.resetCache();
});

function getCBOptions(commandKey) {
Expand All @@ -18,12 +20,7 @@ function getCBOptions(commandKey) {
circuitBreakerRequestVolumeThreshold: 1
}
);
};

beforeEach(function () {
CommandMetricsFactory.resetCache();
CircuitBreakerFactory.resetCache();
});
}

describe("CircuitBreaker", function () {

Expand Down
4 changes: 2 additions & 2 deletions test/command/command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let context = new TestContext();

describe("Command", function () {
it("should resolve with expected results", async () => {
let command = CommandFactory.createDynamicCommand<TestCommand>(context.context, "TestCommand");
let command = new TestCommand(context.context);
expect(command).not.to.be.undefined;

let result = await command.foo("success");
Expand All @@ -29,7 +29,7 @@ describe("Command", function () {
});

it("should timeout if the function does not resolve within the configured timeout", async () => {
let command = CommandFactory.createDynamicCommand<TestCommandTimeout>(context.context,"TestCommandTimeout");
let command = new TestCommandTimeout(context.context);

expect(command).not.to.be.undefined;
try {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"experimentalDecorators": true,
"declaration": true,
"module": "commonjs",
"target": "es6",
"target": "es2017",
"watch": false,
"strictFunctionTypes": true,
"noUnusedParameters": false,
Expand Down

0 comments on commit 761e81f

Please sign in to comment.