Modules@robinpath/auth
auth

@robinpath/auth

0.1.2Node.jsPublic

API authentication helpers (Basic, Bearer, API key, HMAC) for RobinPath

Auth

API authentication helpers: Basic, Bearer, API key, HMAC signing, and password hashing

Package: @robinpath/auth | Category: Web | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the auth module when you need to:

  • Create a Basic authentication header from username and password -- Use auth.basic to perform this operation
  • Parse a Basic auth header to extract username and password -- Use auth.parseBasic to perform this operation
  • Create a Bearer authentication header from a token -- Use auth.bearer to perform this operation
  • Extract the token from a Bearer auth header -- Use auth.parseBearer to perform this operation
  • Create an API key configuration for header or query parameter placement -- Use auth.apiKey to perform this operation

Quick Reference

FunctionDescriptionReturns
basicCreate a Basic authentication header from username and passwordBasic auth header string (e.g. 'Basic dXNlcjpwYXNz')
parseBasicParse a Basic auth header to extract username and password{username, password}
bearerCreate a Bearer authentication header from a tokenBearer auth header string
parseBearerExtract the token from a Bearer auth headerThe extracted token string
apiKeyCreate an API key configuration for header or query parameter placement{type, name, value} object for use in HTTP requests
hmacSignCreate an HMAC signature for a payloadHex-encoded HMAC signature
hmacVerifyVerify an HMAC signature using timing-safe comparisonTrue if the signature is valid
generateApiKeyGenerate a cryptographically secure random API keyRandom hex API key, optionally prefixed
hashPasswordHash a password using PBKDF2 with a random saltHash string in format salt:iterations:hash
verifyPasswordVerify a password against a PBKDF2 hash (timing-safe)True if the password matches the hash
buildAuthHeaderBuild an Authorization header from a type and credentialsComplete Authorization header value
parseAuthHeaderParse any Authorization header into its scheme and credentialsObject with scheme and decoded credentials

Functions

basic

Create a Basic authentication header from username and password

Module: auth | Returns: string -- Basic auth header string (e.g. 'Basic dXNlcjpwYXNz')

auth.basic "user" "pass"
ParameterTypeRequiredDescription
usernamestringYesUsername
passwordstringYesPassword

parseBasic

Parse a Basic auth header to extract username and password

Module: auth | Returns: object -- {username, password}

auth.parseBasic "Basic dXNlcjpwYXNz"
ParameterTypeRequiredDescription
headerstringYesThe Authorization header value

bearer

Create a Bearer authentication header from a token

Module: auth | Returns: string -- Bearer auth header string

auth.bearer "eyJhbGciOi..."
ParameterTypeRequiredDescription
tokenstringYesThe bearer token

parseBearer

Extract the token from a Bearer auth header

Module: auth | Returns: string -- The extracted token string

auth.parseBearer "Bearer eyJhbGciOi..."
ParameterTypeRequiredDescription
headerstringYesThe Authorization header value

apiKey

Create an API key configuration for header or query parameter placement

Module: auth | Returns: object -- {type, name, value} object for use in HTTP requests

auth.apiKey "sk-abc123" "header" "Authorization"
ParameterTypeRequiredDescription
keystringYesThe API key value
placementstringNo'header' or 'query' (default: header)
namestringNoHeader or query param name (default: X-API-Key)

hmacSign

Create an HMAC signature for a payload

Module: auth | Returns: string -- Hex-encoded HMAC signature

auth.hmacSign "payload" "secret" "sha256"
ParameterTypeRequiredDescription
payloadstringYesThe payload to sign
secretstringYesThe secret key
algorithmstringNoHash algorithm (default: sha256)

hmacVerify

Verify an HMAC signature using timing-safe comparison

Module: auth | Returns: boolean -- True if the signature is valid

auth.hmacVerify "payload" "secret" "abc123def..."
ParameterTypeRequiredDescription
payloadstringYesThe original payload
secretstringYesThe secret key
signaturestringYesThe hex signature to verify
algorithmstringNoHash algorithm (default: sha256)

generateApiKey

Generate a cryptographically secure random API key

Module: auth | Returns: string -- Random hex API key, optionally prefixed

auth.generateApiKey 32 "sk"
ParameterTypeRequiredDescription
lengthnumberNoKey length in bytes (default 32)
prefixstringNoOptional prefix (e.g. 'sk', 'pk')

hashPassword

Hash a password using PBKDF2 with a random salt

Module: auth | Returns: string -- Hash string in format salt:iterations:hash

auth.hashPassword "my-secret-password"
ParameterTypeRequiredDescription
passwordstringYesThe password to hash
iterationsnumberNoPBKDF2 iterations (default 100000)

verifyPassword

Verify a password against a PBKDF2 hash (timing-safe)

Module: auth | Returns: boolean -- True if the password matches the hash

auth.verifyPassword "my-secret-password" $storedHash
ParameterTypeRequiredDescription
passwordstringYesThe password to verify
hashstringYesThe stored hash (salt:iterations:hash)

buildAuthHeader

Build an Authorization header from a type and credentials

Module: auth | Returns: string -- Complete Authorization header value

auth.buildAuthHeader "bearer" $token
ParameterTypeRequiredDescription
typestringYesAuth type: basic, bearer, apikey
valueanyYesToken string or {username, password} for basic

parseAuthHeader

Parse any Authorization header into its scheme and credentials

Module: auth | Returns: object -- Object with scheme and decoded credentials

auth.parseAuthHeader $header
ParameterTypeRequiredDescription
headerstringYesThe Authorization header value

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Invalid Basic auth headerCheck the error message for details
Invalid Basic auth credentialsCheck the error message for details
Invalid Bearer auth headerCheck the error message for details
Invalid hash format. Expected salt:iterations:hashCheck the error message for details
Invalid placement: "...". Use "header" or "query".Check the error message for details
@desc "Basic and validate result"
do
  set $result as auth.basic "user" "pass"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Multi-step Auth workflow

Chain multiple auth operations together.

@desc "Basic, parse basic, and more"
do
  set $r_basic as auth.basic "user" "pass"
  set $r_parseBasic as auth.parseBasic "Basic dXNlcjpwYXNz"
  set $r_bearer as auth.bearer "eyJhbGciOi..."
  print "All operations complete"
enddo

2. Safe basic with validation

Check results before proceeding.

@desc "Basic and validate result"
do
  set $result as auth.basic "user" "pass"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.2latest1 months ago
Install
$ robinpath add @robinpath/auth

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.2
LicenseMIT
Unpacked Size5.7 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

web