GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - johnwatson484/hapi-auth-api-key: API Key authentication strategy for Hapi.js

BranchesTags
Open more actions menu

Latest commit

 

History

33 Commits
33 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
src
src
 
 
test
test
 
 
.gitignore
.gitignore
 
 
.npmignore
.npmignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
SECURITY.md
SECURITY.md
 
 
eslint.config.js
eslint.config.js
 
 
package-lock.json
package-lock.json
 
 
package.json
package.json
 
 
sonar-project.properties
sonar-project.properties
 
 
tsconfig.cjs.json
tsconfig.cjs.json
 
 
tsconfig.json
tsconfig.json
 
 
vitest.config.js
vitest.config.js
 
 

Repository files navigation

Quality Gate Status Bugs Code Smells Duplicated Lines (%) Coverage Known Vulnerabilities

hapi-auth-api-key

API Key authentication strategy for Hapi.js

Installation

npm install hapi-auth-api-key

Usage

import Hapi from '@hapi/hapi'
import HapiAuthApiKey from 'hapi-auth-api-key'

const VALID_API_KEY = process.env.API_KEY || 'your-secret-api-key'

const init = async () => {

  const server = Hapi.server({
    port: 3000,
    host: 'localhost'
  })

  await server.register({
    plugin: HapiAuthApiKey, options: { apiKey: VALID_API_KEY }
  })

  server.auth.strategy('api-key', 'api-key')

  server.route({
    method: 'GET',
    path: '/',
    options: {
      auth: 'api-key'
    },
    handler: (request, h) => {
      console.log('Authenticated request with API key:', request.auth.credentials.apiKey)
      return 'Hello World!'
    }
  })

  await server.start()
  console.log('Server running on %s', server.info.uri)
}

process.on('unhandledRejection', (err) => {

  console.log(err)
  process.exit(1)
})

init()

By default, the plugin expects clients to send the API key in the x-api-key header with each request:

curl -H "x-api-key: your-secret-api-key" http://localhost:3000/

Alternatively, you can configure the plugin to accept the API key as a query parameter by setting the queryParamName option.

Options

The plugin accepts the following options during registration:

apiKey (required)

The API key(s) that are valid for authentication. Can be:

  • String: A single API key

    { apiKey: 'your-secret-api-key' }
  • Array of strings: Multiple valid API keys

    { apiKey: ['key-1', 'key-2', 'key-3'] }
  • Function: A function that receives the request and returns a string or array of strings

    { apiKey: (request) => request.headers['x-tenant-id'] === 'tenant-a' ? 'key-a' : 'key-b' }
  • Promise: A promise that resolves to a string or array of strings

    { apiKey: fetchApiKeysFromDatabase() }

headerName (optional)

The name of the header to check for the API key. Defaults to x-api-key.

{ headerName: 'authorization' }

queryParamName (optional)

The name of the query parameter to check for the API key. Defaults to api-key.

{ queryParamName: 'key' }

Note: At least one of headerName or queryParamName must be specified (or left as default).

About

API Key authentication strategy for Hapi.js

Resources

Readme
MIT license

Security policy

Security policy
Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.