Skip to content

Commit

Permalink
Apply code formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Mar 1, 2022
1 parent 5710ad9 commit 0ad74b2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
18 changes: 9 additions & 9 deletions packages/oauth/src/state-stores/clear-state-store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sign, verify } from "jsonwebtoken";
import { InstallURLOptions } from "../install-url-options";
import { StateStore, StateObj } from "./interface";
import { InvalidStateError } from "../errors";
import { sign, verify } from 'jsonwebtoken';
import { InstallURLOptions } from '../install-url-options';
import { StateStore, StateObj } from './interface';
import { InvalidStateError } from '../errors';

// default implementation of StateStore
export default class ClearStateStore implements StateStore {
Expand All @@ -11,15 +11,15 @@ export default class ClearStateStore implements StateStore {

public constructor(
stateSecret: string,
stateExpirationSeconds: number = 600
stateExpirationSeconds: number = 600,
) {
this.stateSecret = stateSecret;
this.stateExpirationSeconds = stateExpirationSeconds;
}

public async generateStateParam(
installOptions: InstallURLOptions,
now: Date
now: Date,
): Promise<string> {
const source = {
installOptions,
Expand All @@ -31,7 +31,7 @@ export default class ClearStateStore implements StateStore {

public async verifyStateParam(
now: Date,
state: string
state: string,
): Promise<InstallURLOptions> {
// decode the state using the secret
let decoded: StateObj;
Expand All @@ -44,10 +44,10 @@ export default class ClearStateStore implements StateStore {
// Check if the state value is not too old
const generatedAt = new Date(decoded.now);
const passedSeconds = Math.floor(
(now.getTime() - generatedAt.getTime()) / 1000
(now.getTime() - generatedAt.getTime()) / 1000,
);
if (passedSeconds > this.stateExpirationSeconds) {
throw new InvalidStateError("The state value is already expired");
throw new InvalidStateError('The state value is already expired');
}
// return installOptions
return decoded.installOptions;
Expand Down
42 changes: 20 additions & 22 deletions packages/oauth/src/state-stores/file-state-store.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { homedir } from "os";
import fs from "fs";
import path from "path";
import { ConsoleLogger, Logger } from "@slack/logger";
import { StateStore, StateObj } from "./interface";
import { InstallURLOptions } from "../install-url-options";
import { InvalidStateError } from "../errors";
import { homedir } from 'os';
import fs from 'fs';
import path from 'path';
import { ConsoleLogger, Logger } from '@slack/logger';
import { StateStore, StateObj } from './interface';
import { InstallURLOptions } from '../install-url-options';
import { InvalidStateError } from '../errors';

export interface FileStateStoreArgs {
stateExpirationSeconds?: number;
Expand All @@ -20,20 +20,18 @@ export class FileStateStore implements StateStore {
private logger: Logger;

public constructor(args: FileStateStoreArgs) {
this.baseDir =
args.baseDir !== undefined
? args.baseDir
: `${homedir()}/.bolt-js-oauth-states`;
this.stateExpirationSeconds =
args.stateExpirationSeconds !== undefined
? args.stateExpirationSeconds
: 600;
this.baseDir = args.baseDir !== undefined ?
args.baseDir :
`${homedir()}/.bolt-js-oauth-states`;
this.stateExpirationSeconds = args.stateExpirationSeconds !== undefined ?
args.stateExpirationSeconds :
600;
this.logger = args.logger !== undefined ? args.logger : new ConsoleLogger();
}

public async generateStateParam(
installOptions: InstallURLOptions,
now: Date
now: Date,
): Promise<string> {
let state = generateRandomString();
while (this.alreadyExists(state)) {
Expand All @@ -47,7 +45,7 @@ export class FileStateStore implements StateStore {

public async verifyStateParam(
now: Date,
state: string
state: string,
): Promise<InstallURLOptions> {
try {
// decode the state using the secret
Expand All @@ -62,18 +60,18 @@ export class FileStateStore implements StateStore {
// Check if the state value is not too old
const generatedAt = new Date(decoded.now);
const passedSeconds = Math.floor(
(now.getTime() - generatedAt.getTime()) / 1000
(now.getTime() - generatedAt.getTime()) / 1000,
);
if (passedSeconds > this.stateExpirationSeconds) {
throw new InvalidStateError("The state value is already expired");
throw new InvalidStateError('The state value is already expired');
}
// return installOptions
return decoded.installOptions;
}
} finally {
this.deleteFile(state);
}
throw new InvalidStateError("The state value is already expired");
throw new InvalidStateError('The state value is already expired');
}

// -------------------------------------------
Expand Down Expand Up @@ -113,10 +111,10 @@ export class FileStateStore implements StateStore {
}
}

const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

function generateRandomString(length: number = 10): string {
let generated = "";
let generated = '';
for (let i = 0; i < length; i += 1) {
const position = Math.floor(Math.random() * chars.length);
generated += chars.charAt(position);
Expand Down

0 comments on commit 0ad74b2

Please sign in to comment.