Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltoken committed Oct 3, 2020
1 parent 659bb2b commit 8f04fec
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* limitations under the License.
*/

import { JWKStore } from './lib/jwk-store'
import { OAuth2Issuer } from './lib/oauth2-issuer'
import { OAuth2Server } from './lib/oauth2-server'
import { JWKStore } from './lib/jwk-store';
import { OAuth2Issuer } from './lib/oauth2-issuer';
import { OAuth2Server } from './lib/oauth2-server';

export { JWKStore, OAuth2Issuer, OAuth2Server }
export { JWKStore, OAuth2Issuer, OAuth2Server };
10 changes: 10 additions & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { AssertionError } from 'assert';

export function assertIsString(
input: unknown,
errorMessage: string
): asserts input is string {
if (typeof input !== 'string') {
throw new AssertionError({ message: errorMessage });
}
}
2 changes: 1 addition & 1 deletion src/lib/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
* @module lib/http-server
*/

import { AssertionError } from 'assert';
import { Server, RequestListener, createServer } from 'http';
import { AddressInfo } from 'net';
import { AssertionError } from 'assert';

/**
* Provides a restartable wrapper for http.CreateServer().
Expand Down
13 changes: 3 additions & 10 deletions src/lib/oauth2-issuer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@

import jwt from 'jsonwebtoken';
import { EventEmitter } from 'events';
import { JWKStore } from './jwk-store';
import { JWK } from 'node-jose';
import { AssertionError } from 'assert';

import { JWKStore } from './jwk-store';
import { assertIsString } from './helpers';

interface Header {
kid: string;
[key: string]: unknown;
Expand Down Expand Up @@ -157,15 +159,6 @@ const supportedAlgs = [
'none',
];

export function assertIsString(
input: unknown,
errorMessage: string
): asserts input is string {
if (typeof input !== 'string') {
throw new AssertionError({ message: errorMessage });
}
}

function assertIsAlgorithm(input: string): asserts input is jwt.Algorithm {
if (!supportedAlgs.includes(input)) {
throw new AssertionError({ message: `Unssuported algorithm '${input}'` });
Expand Down
5 changes: 3 additions & 2 deletions src/lib/oauth2-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
*/

import { URL } from 'url';
import net, { AddressInfo } from 'net';
import { isIP, AddressInfo } from 'net';
import { Server } from 'http';

import { HttpServer } from './http-server';
import { OAuth2Issuer } from './oauth2-issuer';
import { OAuth2Service } from './oauth2-service';
Expand Down Expand Up @@ -123,7 +124,7 @@ function buildIssuerUrl(host: string | undefined, port: number) {
}

function coversLocalhost(address: string) {
switch (net.isIP(address)) {
switch (isIP(address)) {
case 4:
return address === '0.0.0.0' || address.startsWith('127.');
case 6:
Expand Down
24 changes: 10 additions & 14 deletions src/lib/oauth2-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
* @module lib/oauth2-service
*/

import http from 'http';
import { IncomingMessage } from 'http';
import { AssertionError } from 'assert';
import express, { RequestHandler, Express } from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
Expand All @@ -27,13 +28,8 @@ import { EventEmitter } from 'events';
import { v4 as uuidv4 } from 'uuid';
import isPlainObject from 'lodash.isplainobject';

import {
assertIsString,
OAuth2Issuer,
jwtTransform,
scopesOrTransform,
} from './oauth2-issuer';
import { AssertionError } from 'assert';
import { OAuth2Issuer, jwtTransform, scopesOrTransform } from './oauth2-issuer';
import { assertIsString } from './helpers';

const OPENID_CONFIGURATION_PATH = '/.well-known/openid-configuration';
const TOKEN_ENDPOINT_PATH = '/token';
Expand Down Expand Up @@ -87,15 +83,15 @@ export class OAuth2Service extends EventEmitter {
* @param {(String|Array<String>|jwtTransform)} [scopesOrTransform] A scope, array of scopes,
* or JWT transformation callback.
* @param {Number} [expiresIn] Time in seconds for the JWT to expire. Default: 3600 seconds.
* @param {http.IncomingMessage} req The incoming HTTP request.
* @param {IncomingMessage} req The incoming HTTP request.
* @returns {String} The produced JWT.
* @fires OAuth2Service#beforeTokenSigning
*/
buildToken(
signed: boolean,
scopesOrTransform: scopesOrTransform | undefined,
expiresIn: number,
req: http.IncomingMessage
req: IncomingMessage
): string {
this.issuer.once('beforeSigning', (token) => {
/**
Expand All @@ -104,7 +100,7 @@ export class OAuth2Service extends EventEmitter {
* @param {object} token The unsigned JWT header and payload.
* @param {object} token.header The JWT header.
* @param {object} token.payload The JWT payload.
* @param {http.IncomingMessage} req The incoming HTTP request.
* @param {IncomingMessage} req The incoming HTTP request.
*/
this.emit('beforeTokenSigning', token, req);
});
Expand Down Expand Up @@ -265,7 +261,7 @@ export class OAuth2Service extends EventEmitter {
* @param {object} response The response body and status code.
* @param {object} response.body The body of the response.
* @param {Number} response.statusCode The HTTP status code of the response.
* @param {http.IncomingMessage} req The incoming HTTP request.
* @param {IncomingMessage} req The incoming HTTP request.
*/
this.emit('beforeResponse', tokenEndpointResponse, req);

Expand Down Expand Up @@ -321,7 +317,7 @@ export class OAuth2Service extends EventEmitter {
* @param {object} response The response body and status code.
* @param {object} response.body The body of the response.
* @param {Number} response.statusCode The HTTP status code of the response.
* @param {http.IncomingMessage} req The incoming HTTP request.
* @param {IncomingMessage} req The incoming HTTP request.
*/
this.emit('beforeUserinfo', userInfoResponse, req);

Expand All @@ -340,7 +336,7 @@ export class OAuth2Service extends EventEmitter {
* @param {object} response The response body and status code.
* @param {object} response.body The body of the response.
* @param {Number} response.statusCode The HTTP status code of the response.
* @param {http.IncomingMessage} req The incoming HTTP request.
* @param {IncomingMessage} req The incoming HTTP request.
*/
this.emit('beforeRevoke', revokeResponse, req);

Expand Down
2 changes: 1 addition & 1 deletion src/oauth2-mock-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import fs from 'fs';
import { JWK } from 'node-jose';
import path from 'path';
import { OAuth2Server } from './index';
import { assertIsString } from './lib/oauth2-issuer';
import { assertIsString } from './lib/helpers';

/* eslint no-console: off */

Expand Down

0 comments on commit 8f04fec

Please sign in to comment.