Git Product home page Git Product logo

openid-client-server's Introduction

openid-client-server

An OpenId Relying Party (RP, Client) application server.

This module leverages the openid-client module to implement a web server that secures any Web UI framework that can be hosted by Node.js with Authorization Code Flow (optional Proof Key), Implicit Flow or Hybrid Flow. The module also provides configurable proxy endpoints that include the user token automatically in requests to API endpoints, as well a session management making it easier to create Web UI's that are "secure by default".

Install

with npm

$ npm install @optum/openid-client-server

with yarn

$ yarn add @optum/openid-client-server

Usage

Options

The resolveOptions function will leverage environmental variables to auto-build all options with defaults. It can be required in the server setup module via import {resolveOptions} from '@optum/openid-client-server.

For more info see the .env.example file

clientServer

Use the clientServer function to create a http server with an integrated openid-client and all features in @optum/openid-client-server.

With a Promise

import {IncomingMessage, ServerResponse} from 'http'
import {clientServer} from '@optum/openid-client-server'

import handle from 'serve-handler'

const port = parseInt(process.env.NEXT_SERVER_PORT ?? '8080', 10)

const serveHandler = async (
    req: IncomingMessage,
    res: ServerResponse
): Promise<void> => {
    handle(req, res, {
        headers: [
            {
                source: '**/*.*',
                headers: [
                    {
                        key: 'Cache-Control',
                        value: 'max-age=0'
                    }
                ]
            }
        ]
    })
}

clientServer({
    contentHandler: serveHandler
})
    .then(server =>
        server.listen(port, () => {
            console.log(`> Ready on http://localhost:${port}`)
        })
    )
    .catch(error => {
        console.log('Static content server failed to start')
        console.error(error)
    })

With a Async Await

import {IncomingMessage, ServerResponse} from 'http'
import {clientServer} from '@optum/openid-client-server'

import handle from 'serve-handler'

const port = parseInt(process.env.NEXT_SERVER_PORT ?? '8080', 10)

;(async (): Promise<void> => {
    try {
        const serveHandler = async (
            req: IncomingMessage,
            res: ServerResponse
        ): Promise<void> => {
            handle(req, res, {
                headers: [
                    {
                        source: '**/*.*',
                        headers: [
                            {
                                key: 'Cache-Control',
                                value: 'max-age=0'
                            }
                        ]
                    }
                ]
            })
        }

        const server = await clientServer({contentHandler: serveHandler})

        server.listen(port, () => {
            console.log(`> Ready on http://localhost:${port}`)
        })
    } catch (error) {
        console.log('Static content server failed to start')
        console.error(error)
    }
})()

For a Next.js example, see: examples/nextjs file

Background

The original goal of this module was to provide as easy way to implement OpenID flows with Next.js applications via a custom Next.js server. There were issues leveraging frameworks like Koa.js for "easy wins" in session management and out-of-the-box middleware, so tides turned to using Node's core http module. The result ended up working for any Web UI that could be served by Node.js, so here we are.

Development

Environment

  • Node.js is required to develop this module. Please install the latest LTS version if you haven't already.
  • Module dependencies are managed with Yarn. Please install it if you haven't already.
    $ npm i -g yarn

Editors

VS Code

IntelliJ

openid-client-server's People

Contributors

ryantrappy avatar dependabot[bot] avatar lxghtless avatar amyschoen avatar

Stargazers

Jose F. Fernandez avatar  avatar Eric Carboni avatar  avatar Michael Helfer avatar

Watchers

Justin Chase avatar James Cloos avatar Michael Helfer avatar Ryan Sites avatar Alex Aavang avatar Michael R Blevins avatar  avatar Chris Lantz avatar  avatar JD DeFauw avatar Phil Lodwick avatar

Forkers

mtintes

openid-client-server's Issues

Headers not being set on response

I work within EFWAE in Optum and was having an issue with the openid-client-server and was referred here by Alex Aavang.

While investigating why our react front end was seeing calls that should be JSON as XML I came across a possible bug within the server. The headers from node-fetch are key value pairs where the value is an array with a single value. The node response header method, res.writeHead expects these headers to be set as strings rather than arrays. This is not the case when performing a getUserInfo request to the OAuth provider but only happens with the response from our koa api.

Screen Shot 2020-12-01 at 4 36 54 PM

Adding the following code at the beginning of the sendResponse method in /middleware/util.ts resolves the issue as it converts the arrays into strings. Without these changes none of the headers seen in the response from the koa API are forwarded to the client.

if(headers.raw){ headers = JSON.parse(JSON.stringify(headers.raw())) }

This can also been seen in the network tab of the page using the server. As you can see the response does not have a content-type and is therefore seen as xml.

Screen Shot 2020-12-02 at 8 16 08 AM

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.