Skip to content

Commit

Permalink
Merge pull request #140 from axa-group/ntk/fixups
Browse files Browse the repository at this point in the history
Version 4.1.0
  • Loading branch information
nulltoken authored Nov 15, 2021
2 parents 2fecf48 + 8dd986b commit 40bf6bb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 48 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## [4.1.0](https://github.com/axa-group/oauth2-mock-server/compare/v4.0.0...v4.1.0) — 2021-11-15

### Added

- HTTPS support (by [lbestftr](https://github.com/lbestftr))

## [4.0.0](https://github.com/axa-group/oauth2-mock-server/compare/v3.2.0...v4.0.0) — 2021-10-25

### Added

- Add `/endsession` endpoint (thanks [AndTem](https://github.com/AndTem)!)
- Add `/endsession` endpoint (by [AndTem](https://github.com/AndTem))
- Support `EdDSA` algorithm

### Removed
Expand All @@ -26,13 +32,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Added

- Add `subject_types_supported` OpenID Provider Metadata field (thanks [jjbooth74](https://github.com/jjbooth74)!)
- Add `subject_types_supported` OpenID Provider Metadata field (by [jjbooth74](https://github.com/jjbooth74))

## [3.1.0](https://github.com/axa-group/oauth2-mock-server/compare/v3.0.3...v3.1.0) — 2020-11-30

### Added

- Add authorize redirect event (thanks [markwallsgrove](https://github.com/markwallsgrove)!)
- Add authorize redirect event (by [markwallsgrove](https://github.com/markwallsgrove))

## [3.0.3](https://github.com/axa-group/oauth2-mock-server/compare/v3.0.2...v3.0.3) — 2020-11-12

Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,18 @@ It also provides a convenient way, through event emitters, to programmatically c
```

### HTTPS support

It also provides basic HTTPS support, an optional cert and key can be supplied to start the server with SSL/TLS using the in-built NodeJS [HTTPS](https://nodejs.org/api/https.html) module.

We recommend using a package to create a locally trusted certificate, like [mkcert](https://github.com/FiloSottile/mkcert).
```
new OAuth2Server('test-assets/mock-auth/key.pem', 'test-assets/mock-auth/cert.pem')
```

```js
let server = new OAuth2Server(
'test-assets/mock-auth/key.pem',
'test-assets/mock-auth/cert.pem'
);
```

NOTE: Enabling HTTPS will also update the issuer URL to reflect the current protocol.

## Supported endpoints
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oauth2-mock-server",
"version": "4.0.0",
"version": "4.1.0",
"description": "OAuth 2 mock server",
"keywords": [
"oauth",
Expand Down
30 changes: 28 additions & 2 deletions src/lib/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import { Server, RequestListener, createServer } from 'http';
import { createServer as createHttpsServer } from 'https';
import type { AddressInfo } from 'net';
import { AddressInfo, isIP } from 'net';

import { assertIsAddressInfo } from './helpers';
import { HttpServerOptions } from './types';
Expand All @@ -31,7 +31,7 @@ import { HttpServerOptions } from './types';
*/
export class HttpServer {
#server: Server;
protected _isSecured = false;
#isSecured: boolean;

/**
* Creates a new instance of HttpServer.
Expand All @@ -40,8 +40,11 @@ export class HttpServer {
* @param {HttpServerOptions} options Optional HttpServerOptions to start the server with https.
*/
constructor(requestListener: RequestListener, options?: HttpServerOptions) {
this.#isSecured = false;

if (options?.key && options?.cert) {
this.#server = createHttpsServer(options, requestListener);
this.#isSecured = true;
} else {
this.#server = createServer(requestListener);
}
Expand Down Expand Up @@ -114,4 +117,27 @@ export class HttpServer {
});
});
}

protected buildIssuerUrl(host: string | undefined, port: number): string {
const url = new URL(
`${this.#isSecured ? 'https' : 'http'}://localhost:${port}`
);

if (host && !coversLocalhost(host)) {
url.hostname = host.includes(':') ? `[${host}]` : host;
}

return url.origin;
}
}

const coversLocalhost = (address: string) => {
switch (isIP(address)) {
case 4:
return address === '0.0.0.0' || address.startsWith('127.');
case 6:
return address === '::' || address === '::1';
default:
return false;
}
};
46 changes: 7 additions & 39 deletions src/lib/oauth2-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@
* @module lib/oauth2-server
*/

import * as fs from 'fs';
import { ServerOptions } from 'https';
import { URL } from 'url';
import { isIP, AddressInfo } from 'net';
import { readFileSync } from 'fs';
import { AddressInfo } from 'net';
import { Server } from 'http';

import { HttpServer } from './http-server';
import { OAuth2Issuer } from './oauth2-issuer';
import { OAuth2Service } from './oauth2-service';
import { assertIsAddressInfo } from './helpers';
import { HttpServerOptions } from './types';

/**
* Represents an OAuth2 HTTP server.
Expand All @@ -51,18 +50,16 @@ export class OAuth2Server extends HttpServer {
const iss = new OAuth2Issuer();
const serv = new OAuth2Service(iss);

let options: ServerOptions | undefined = undefined;
let options: HttpServerOptions | undefined = undefined;
if (key && cert) {
options = {
key: fs.readFileSync(key),
cert: fs.readFileSync(cert),
key: readFileSync(key),
cert: readFileSync(cert),
};
}

super(serv.requestHandler, options);

this._isSecured = options !== undefined;

this._issuer = iss;
this._service = serv;
}
Expand Down Expand Up @@ -119,11 +116,7 @@ export class OAuth2Server extends HttpServer {
const server = await super.start(port, host);

if (!this.issuer.url) {
this.issuer.url = buildIssuerUrl(
host,
this.address().port,
this._isSecured
);
this.issuer.url = super.buildIssuerUrl(host, this.address().port);
}

return server;
Expand All @@ -139,28 +132,3 @@ export class OAuth2Server extends HttpServer {
this._issuer.url = undefined;
}
}

function buildIssuerUrl(
host: string | undefined,
port: number,
isSecured = false
) {
const url = new URL(`${isSecured ? 'https' : 'http'}://localhost:${port}`);

if (host && !coversLocalhost(host)) {
url.hostname = host.includes(':') ? `[${host}]` : host;
}

return url.origin;
}

function coversLocalhost(address: string) {
switch (isIP(address)) {
case 4:
return address === '0.0.0.0' || address.startsWith('127.');
case 6:
return address === '::' || address === '::1';
default:
return false;
}
}

0 comments on commit 40bf6bb

Please sign in to comment.