/**
 * Copyright (c) AXA Assistance France
 *
 * Licensed under the AXA Assistance France License (the "License"); you
 * may not use this file except in compliance with the License.
 * A copy of the License can be found in the LICENSE.md file distributed
 * together with this file.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ module 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 , OAuth2Options } from './types' ; /**
 * Represents an OAuth2 HTTP server.
 */ export class OAuth2Server extends HttpServer { private _service : OAuth2Service ; private _issuer : OAuth2Issuer ; param { string | undefined } key param { string | undefined } cert param { OAuth2Options | undefined } oauth2Options constructor ( key ? , cert ? , oauth2Options ? ) { if ( ( key && ! cert ) || ( ! key && cert ) ) { throw 'Both key and cert need to be supplied to start the server with https' ; } const iss = new OAuth2Issuer ( ) ; const serv = new OAuth2Service ( iss , oauth2Options ?. endpoints ) ; let options = undefined ; if ( key && cert ) { options = { key : readFileSync ( key ) , cert : readFileSync ( cert ) , } ; } super ( serv . requestHandler , options ) ; this . _issuer = iss ; this . _service = serv ; } type { OAuth2Issuer } get issuer ( ) : OAuth2Issuer { return this . _issuer ; } type { OAuth2Service } get service ( ) : OAuth2Service { return this . _service ; } type { boolean } get listening ( ) : boolean { return super . listening ; } returns { AddressInfo } address ( ) { const address = super . address ( ) ; assertIsAddressInfo ( address ) ; return address ; } param { number } port param { string } host returns { Promise < void > } async start ( port ? , host ? ) { const server = await super . start ( port , host ) ; if ( ! this . issuer . url ) { this . issuer . url = super . buildIssuerUrl ( host , this . address ( ) . port ) ; } return server ; } returns { Promise } async stop ( ) { await super . stop ( ) ; this . _issuer . url = undefined ; } }