Skip to content

Commit

Permalink
feat: add config setting for server port #315
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Jul 29, 2022
1 parent dfec307 commit 52c3e7f
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/models/openIdInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface OpenIdConfiguration {
useDeviceCodeClientSecret?: boolean;
usePkce: boolean;
redirectUri: URL;
serverPort?: number;
}

export interface OpenIdInformation extends UserSession {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/oauth2/flow/authorizationCodeFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class AuthorizationCodeFlow implements OpenIdFlow {

registerListener({
id: state,
url: config.redirectUri,
port: config.serverPort || Number(config.redirectUri.port),
path: config.redirectUri.pathname,
name: `authorization for ${config.clientId}: ${config.authorizationEndpoint}`,
resolve: params => {
if (params.code && params.state === state) {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/oauth2/flow/implicitFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class ImplicitFlow implements OpenIdFlow {

registerListener({
id: state,
url: config.redirectUri,
port: config.serverPort || Number(config.redirectUri.port),
path: config.redirectUri.pathname,
name: `authorization for ${config.clientId}: ${config.authorizationEndpoint}`,
resolve: params => {
if (params.state === state) {
Expand Down
13 changes: 9 additions & 4 deletions src/plugins/oauth2/flow/openIdHttpServer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { log } from '../../../io';
import { createServer, Server } from 'http';
import { URL } from 'url';

interface RequestListener {
url: URL;
path: string;
port: number;
id: string;
name: string;
resolve: (params: Record<string, string>) => { valid: boolean; message: string; statusMessage: string };
Expand All @@ -18,7 +18,7 @@ let serverTimeoutTime = 0;

export function registerListener(listener: RequestListener): void {
listeners.push(listener);
initServer(Number(listener.url.port), listener.url.pathname);
initServer(listener.port, listener.path);
}

export function unregisterListener(id: string): void {
Expand Down Expand Up @@ -67,7 +67,6 @@ function closeServer() {
function initServer(port: number, callbackPath: string) {
resetServer(600);
if (!server) {
log.debug(`open http server on port ${port}`);
server = createServer((req, res) => {
try {
let statusMessage = 'invalid';
Expand Down Expand Up @@ -134,6 +133,12 @@ function initServer(port: number, callbackPath: string) {
}
});
server.listen(port);
const address = server.address();
if (address) {
log.debug(`open http server on port ${typeof address === 'string' ? address : address.port}`);
} else {
log.debug(`open http server on port ${port}`);
}
}
}

Expand Down
15 changes: 14 additions & 1 deletion src/plugins/oauth2/openIdConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ export const DEFAULT_CALLBACK_URI = 'http://localhost:3000/callback';
const DefaultOAuthVariablePrefix = 'oauth2';

function getVariableRaw(variables: models.Variables, name: string, variablePrefix: string): unknown {
let varValue = variables[`${variablePrefix}_${name}`];
const variableName = `${variablePrefix}_${name}`;
let varValue = variables[variableName];

const caseInsensitiveVariables = Object.entries(variables)
.filter(([key]) => key.toLowerCase() === variableName.toLowerCase())
.map(([, value]) => value);
if (caseInsensitiveVariables.length === 1) {
return caseInsensitiveVariables.pop();
}
if (typeof varValue === 'undefined' || varValue === null) {
varValue = get(variables, `${variablePrefix}.${name}`);
}
Expand Down Expand Up @@ -50,6 +58,10 @@ export function getOpenIdConfiguration(
const expandedValue = getVariableUnknown(variables, variablePrefix, name);
return utils.toBoolean(expandedValue, defaultValue);
};
const getNumber = (name: string, defaultValue = undefined) => {
const expandedValue = getVariableUnknown(variables, variablePrefix, name);
return utils.toNumber(expandedValue, defaultValue);
};

const config: models.OpenIdConfiguration = {
variablePrefix: variablePrefix || DefaultOAuthVariablePrefix,
Expand All @@ -71,6 +83,7 @@ export function getOpenIdConfiguration(
useAuthorizationHeader: getBoolean('useAuthorizationHeader', true),
useDeviceCodeClientSecret: getBoolean('useDeviceCodeClientSecret'),
usePkce: getBoolean('usePkce'),
serverPort: getNumber('serverPort'),
};
return config;
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './logUtils';
export * from './markdownUtils';
export * from './mimeTypeUtils';
export * from './modelUtils';
export * from './numberUtils';
export * from './parserUtils';
export * from './promiseUtils';
export * from './repeatUtils';
Expand Down
15 changes: 15 additions & 0 deletions src/utils/numberUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ensureString } from './stringUtils';

export function toNumber<T>(value: unknown, defaultValue: T | undefined = undefined): number | T | undefined {
if (typeof value === 'number') {
return value;
}
const stringValue = ensureString(value);
if (stringValue) {
const numberValue = Number.parseInt(stringValue.trim(), 10);
if (!isNaN(numberValue)) {
return numberValue;
}
}
return defaultValue;
}
10 changes: 0 additions & 10 deletions src/utils/stringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ export function isString(text: unknown): text is string {
return typeof text === 'string';
}

export function toNumber<T>(text: string | undefined, defaultVal?: T | undefined): number | T | undefined {
if (text) {
const number = Number.parseInt(text, 10);
if (!Number.isNaN(number)) {
return number;
}
}
return defaultVal;
}

export function isStringEmpty(text: unknown): boolean {
return typeof text === 'string' && /^(\s*)?$/u.test(text);
}
Expand Down

0 comments on commit 52c3e7f

Please sign in to comment.