Skip to content

Commit

Permalink
Preparing release 1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
queue-it committed Jun 26, 2023
1 parent 588ab34 commit d46f7f3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@queue-it/queue-token",
"version": "1.0.3",
"version": "1.0.4",
"description": "Ensures that end users cannot enter the queue without a valid token and to be a container which can carry sensitive user information from integrating system into the queue",
"repository": "https://github.com/queueit/QueueToken.V1.JavaScript",
"main": "dist/index.js",
Expand Down
40 changes: 33 additions & 7 deletions src/Payload.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {PayloadDto} from "./model/PayloadDto";
import {Utils} from "./QueueITHelpers";
import {TokenSerializationException} from "./Token";
import {Base64} from "./helpers/Base64";
import {ModeOfOperationCBC} from "./helpers/Aes";
import { PayloadDto } from "./model/PayloadDto";
import { Utils } from "./QueueITHelpers";
import { TokenSerializationException } from "./Token";
import { Base64 } from "./helpers/Base64";
import { ModeOfOperationCBC } from "./helpers/Aes";
import { TokenOrigin } from "./model/TokenOrigin";

export class Payload {
public static Enqueue(): EnqueueTokenPayloadGenerator {
Expand Down Expand Up @@ -33,6 +34,12 @@ export class EnqueueTokenPayloadGenerator {
return this;
}

public WithOrigin(origin: TokenOrigin): EnqueueTokenPayloadGenerator {
this._payload = EnqueueTokenPayload.create(this._payload, null);
this._payload.AddTokenOrigin(origin);
return this;
}

public Generate(): IEnqueueTokenPayload {
return this._payload;
}
Expand All @@ -42,6 +49,7 @@ export interface IEnqueueTokenPayload {
readonly Key: string
readonly RelativeQuality?: number
readonly CustomData: Object
readonly TokenOrigin: TokenOrigin

EncryptAndEncode(secretKey: string, tokenIdentifier: string): string

Expand All @@ -52,6 +60,7 @@ export class EnqueueTokenPayload implements IEnqueueTokenPayload {
private _customData: Object;
private _key: string;
private _relativeQuality: number
private _origin: TokenOrigin

public get Key(): string {
return this._key;
Expand All @@ -73,11 +82,16 @@ export class EnqueueTokenPayload implements IEnqueueTokenPayload {
this._relativeQuality = value;
}

public get TokenOrigin(): TokenOrigin {
return this._origin;
}

constructor() {
this._customData = {};
this._origin = TokenOrigin.Connector;
}

static create(payload?: EnqueueTokenPayload, key?: string, relativeQuality?: number, customData?: object): EnqueueTokenPayload {
static create(payload?: EnqueueTokenPayload, key?: string, relativeQuality?: number, customData?: object, origin?: TokenOrigin): EnqueueTokenPayload {
const newPayload = new EnqueueTokenPayload();
newPayload.Key = key;
if (payload) {
Expand All @@ -87,13 +101,19 @@ export class EnqueueTokenPayload implements IEnqueueTokenPayload {
newPayload.Key = payload.Key;
}
}

if (relativeQuality != null) {
newPayload.RelativeQuality = relativeQuality
}

if (customData) {
newPayload._customData = customData;
}

if (origin) {
newPayload._origin = origin;
}

return newPayload;
}

Expand All @@ -105,18 +125,24 @@ export class EnqueueTokenPayload implements IEnqueueTokenPayload {
return this;
}

public AddTokenOrigin(origin: TokenOrigin): EnqueueTokenPayload {
this._origin = origin ?? TokenOrigin.Connector;
return this;
}

public Serialize(): Uint8Array {
const dto = new PayloadDto();
dto.Key = this.Key;
dto.RelativeQuality = this.RelativeQuality;
dto.CustomData = this.CustomData;
dto.Origin = this.TokenOrigin;

return dto.Serialize();
}

static Deserialize(input: string, secretKey: string, tokenIdentifier: string): EnqueueTokenPayload {
const dto = PayloadDto.DeserializePayload(input, secretKey, tokenIdentifier);
return EnqueueTokenPayload.create(null, dto.Key, dto.RelativeQuality, dto.CustomData)
return EnqueueTokenPayload.create(null, dto.Key, dto.RelativeQuality, dto.CustomData, dto.Origin);
}

EncryptAndEncode(secretKey: string, tokenIdentifier: string): string {
Expand Down
26 changes: 13 additions & 13 deletions src/Token.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {TokenVersion} from "./model/TokenVersion";
import {EnqueueTokenPayload, IEnqueueTokenPayload} from "./Payload";
import {EncryptionType} from "./model/EncryptionType";
import {ShaHashing, Utils} from "./QueueITHelpers";
import {HeaderDto} from "./model/HeaderDto";
import {Base64} from "./helpers/Base64";
import { TokenVersion } from "./model/TokenVersion";
import { EnqueueTokenPayload, IEnqueueTokenPayload } from "./Payload";
import { EncryptionType } from "./model/EncryptionType";
import { ShaHashing, Utils } from "./QueueITHelpers";
import { HeaderDto } from "./model/HeaderDto";
import { Base64 } from "./helpers/Base64";

const InvalidTokenExceptionMessage = "Invalid token";

Expand Down Expand Up @@ -116,13 +116,13 @@ export class EnqueueToken {
}

public static Create(tokenIdentifier: string,
customerId: string,
eventId: string,
issued: Date,
expires: Date,
ipAddress: string,
xForwardedFor: string,
payload: IEnqueueTokenPayload) {
customerId: string,
eventId: string,
issued: Date,
expires: Date,
ipAddress: string,
xForwardedFor: string,
payload: IEnqueueTokenPayload) {
const token = new EnqueueToken(customerId, "");
token.TokenIdentifier = tokenIdentifier;
token.CustomerId = customerId;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/Aes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class Utf8Converter {
}

// Number of rounds by keysize
let numberOfRounds = {16: 10, 24: 12, 32: 14}
let numberOfRounds = { 16: 10, 24: 12, 32: 14 }

// Round constant words
let rcon = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91];
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export {Payload} from "./Payload";
export {Token} from "./Token"
export { Payload } from "./Payload";
export { Token } from "./Token"
26 changes: 19 additions & 7 deletions src/model/PayloadDto.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import {AESEncryption, Utils} from "../QueueITHelpers";
import {Base64} from "../helpers/Base64";
import { AESEncryption, Utils } from "../QueueITHelpers";
import { Base64 } from "../helpers/Base64";
import { TokenOrigin } from "./TokenOrigin";

export class PayloadDto {
public RelativeQuality?: number
public Key: string
public CustomData?: object
public Origin?: TokenOrigin

Serialize(): Uint8Array {
const obj = {
r: this.RelativeQuality,
k: this.Key
r: this.RelativeQuality,
k: this.Key
};
if(this.CustomData && Object.keys(this.CustomData).length>0){

if (this.CustomData && Object.keys(this.CustomData).length > 0) {
obj['cd'] = this.CustomData
}

if (this.Origin) {
obj['o'] = this.Origin
}

let jsonData = JSON.stringify(obj);

return Utils.stringToUint8Array(jsonData);
Expand All @@ -23,14 +31,18 @@ export class PayloadDto {
const headerEncrypted = Base64.decode(input);
const decryptedBytes: Uint8Array = AESEncryption.DecryptPayload(secretKey, tokenIdentifier, headerEncrypted);
const jsonData = JSON.parse(Utils.uint8ArrayToString(decryptedBytes));
if(jsonData==null) return null;
if (jsonData == null) return null;

const payload = new PayloadDto();
payload.RelativeQuality = jsonData['r'];
payload.Key = jsonData['k'];
if(jsonData['cd']){

if (jsonData['cd']) {
payload.CustomData = jsonData['cd'];
}

jsonData['o'] = payload.Origin ?? jsonData['o'];

return payload;
}
}
4 changes: 4 additions & 0 deletions src/model/TokenOrigin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum TokenOrigin {
Connector,
InviteOnly
}

0 comments on commit d46f7f3

Please sign in to comment.