@barry-rocks/sdk/auth
The pieces of Barry’s local authentication a service needs:
isAuthorized for the instance secret, browser sign-in (handleSignIn,
hasBrowserSession, SIGN_IN_PATH), and the session cookie. The
security model explains what the secret
does and doesn’t protect.
import { … } from "@barry-rocks/sdk/auth";Variables
Section titled “Variables”SESSION_COOKIE
Section titled “SESSION_COOKIE”
constSESSION_COOKIE:"barry_session"="barry_session"
SIGN_IN_PATH
Section titled “SIGN_IN_PATH”
constSIGN_IN_PATH:"/sign-in"="/sign-in"
Functions
Section titled “Functions”carriesSecret()
Section titled “carriesSecret()”carriesSecret(
req,secret):boolean
Whether the request carries the secret as a bearer token or x-barry-secret.
Delegates to requestCredential, the single parser the API server already
uses: it rejects duplicate Authorization/x-barry-secret headers (which
Node can otherwise silently collapse) and enforces a token charset. Keeping
two parsers for “does this request carry the secret” is exactly the drift the
auth code warns against.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
req |
IncomingMessage |
secret |
string |
Returns
Section titled “Returns”boolean
handleSignIn()
Section titled “handleSignIn()”handleSignIn(
req,res,secret,title):Promise<boolean>
Serve GET /sign-in (the form) and POST /sign-in (the exchange). Returns
false for any other request, so a server calls it first and carries on.
A right secret sets the cookie and redirects to /; a wrong one re-renders
the form with a 401. The cookie is Secure when the request reached the
provider over https, so a page reached over the tunnel never sends it in
the clear, and plain http on this Mac still works.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
req |
IncomingMessage |
res |
ServerResponse |
secret |
string |
title |
string |
Returns
Section titled “Returns”Promise<boolean>
hasBrowserSession()
Section titled “hasBrowserSession()”hasBrowserSession(
req,secret,now?):boolean
Whether the request carries a session cookie a sign-in with secret issued
that has not yet expired. The embedded timestamps are re-signed and compared,
and the expiry is checked against now.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
req |
IncomingMessage |
secret |
string |
now |
number |
Returns
Section titled “Returns”boolean
isAuthorized()
Section titled “isAuthorized()”isAuthorized(
req,secret):boolean
The secret, from an app, or a signed-in browser’s cookie. Nothing else counts.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
req |
IncomingMessage |
secret |
string |
Returns
Section titled “Returns”boolean
sessionToken()
Section titled “sessionToken()”sessionToken(
secret,now?,lifetimeMs?):string
What the session cookie holds for an instance whose secret is secret.
now/lifetimeMs are injectable for tests and for callers that want a
shorter window; by default a browser stays signed in for 30 days.
Parameters
Section titled “Parameters”| Parameter | Type | Default value |
|---|---|---|
secret |
string |
undefined |
now |
number |
... |
lifetimeMs |
number |
THIRTY_DAYS_MS |
Returns
Section titled “Returns”string