basic-auth

WebJar for basic-auth

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

basic-auth
Last Version

Last Version

2.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

basic-auth
WebJar for basic-auth
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/jshttp/basic-auth

Download basic-auth

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/basic-auth/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>basic-auth</artifactId>
    <version>2.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/basic-auth/
implementation 'org.webjars.npm:basic-auth:2.0.1'
// https://jarcasting.com/artifacts/org.webjars.npm/basic-auth/
implementation ("org.webjars.npm:basic-auth:2.0.1")
'org.webjars.npm:basic-auth:jar:2.0.1'
<dependency org="org.webjars.npm" name="basic-auth" rev="2.0.1">
  <artifact name="basic-auth" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='basic-auth', version='2.0.1')
)
libraryDependencies += "org.webjars.npm" % "basic-auth" % "2.0.1"
[org.webjars.npm/basic-auth "2.0.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.webjars.npm : safe-buffer jar [5.1.2]

Project Modules

There are no modules declared in this project.

basic-auth

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Generic basic auth Authorization header field parser for whatever.

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install basic-auth

API

var auth = require('basic-auth')

auth(req)

Get the basic auth credentials from the given request. The Authorization header is parsed and if the header is invalid, undefined is returned, otherwise an object with name and pass properties.

auth.parse(string)

Parse a basic auth authorization header string. This will return an object with name and pass properties, or undefined if the string is invalid.

Example

Pass a Node.js request object to the module export. If parsing fails undefined is returned, otherwise an object with .name and .pass.

var auth = require('basic-auth')
var user = auth(req)
// => { name: 'something', pass: 'whatever' }

A header string from any other location can also be parsed with auth.parse, for example a Proxy-Authorization header:

var auth = require('basic-auth')
var user = auth.parse(req.getHeader('Proxy-Authorization'))

With vanilla node.js http server

var http = require('http')
var auth = require('basic-auth')
var compare = require('tsscmp')

// Create server
var server = http.createServer(function (req, res) {
  var credentials = auth(req)

  // Check credentials
  // The "check" function will typically be against your user store
  if (!credentials || !check(credentials.name, credentials.pass)) {
    res.statusCode = 401
    res.setHeader('WWW-Authenticate', 'Basic realm="example"')
    res.end('Access denied')
  } else {
    res.end('Access granted')
  }
})

// Basic function to validate credentials for example
function check (name, pass) {
  var valid = true

  // Simple method to prevent short-circut and use timing-safe compare
  valid = compare(name, 'john') && valid
  valid = compare(pass, 'secret') && valid

  return valid
}

// Listen
server.listen(3000)

License

MIT

org.webjars.npm
Low-Level JavaScript HTTP-related Modules

Versions

Version
2.0.1
2.0.0
1.1.0
1.0.1