npmhttp

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 39 Imported by: 0

Documentation

Overview

Package npmhttp serves the npm registry of npm repositories at /artifactory/api/npm/{repoKey}/ (decision 0034), mounted through the router kit (decision 0032). Packuments and version manifests are built on each request from what the npm format keeps in the package index, and tarballs download through the item routes' download, so redirects and download counts apply to them as to any file. A publish stores the tarball through the item service, whose npm format indexes it as it indexes a REST deploy. Beside the registry, the npm partial strict server (NPMAPI) answers the JFrog operations that hand npm its .npmrc lines and queue an npm local repository's reindex and strict-layout conversion.

Role

artifacts module, ecosystem layer: the native npm registry protocol of the npm slice, and its HTTP layer for the JFrog operations beside it. It is named npmhttp so that it does not shadow net/http. Outside tests, only its own http subtree, the module's http adapters and app may import it (decision 0044, dependency rule 9).

Inputs

  • Driving: the server, which mounts Protocol through the router kit (pkgroute) for repositories of package type npm: packuments, version manifests and tarballs; publish, dist-tags, deprecation and unpublish; search, ping and whoami; and npm's legacy login and logout. The router also embeds NPMAPI in the artifactory spec's strict server, calls NPMOperation and IsNPMReindex in its error-profile rules, and gives the authentication middleware NPMUserRequest as its first skip and KeepNPMCredential as its credential keeper, and the access log NPMLoggedPath after the LuaRocks redactor.
  • Driven: the router kit (pkgroute.Kit), which resolves the repository and the caller, checks permissions, reads and writes items and writes errors; the format package (artifacts/ecosystem/npm), whose index and rules the answers are built from; the accounts (Options.Accounts), whose password check and token service the access module provides (access/api); and the remote source service (Options.RemoteSources), which fetches upstream metadata and tarballs into the remote cache. NPMAPI's operations run over the item kit (artifacts/adapters/http/itemkit), whose store (itemkit.Store) holds the parked conversion that a resume reads, and which resolves the repository, checks Manage and queues the reindex every format shares, and over access/api's Tokens, whose encrypted password stands in for a password caller's own in the .npmrc lines (DV-004).

Outputs

  • Protocol and Options: the routes, the source of each repository class and the npm error profile; DefaultMaxPublishSize and DefaultLoginTokenExpiry, decision 0034's defaults.
  • Accounts, Passwords and Tokens: what login and logout need from the server.
  • NPMAPI: the npm partial strict server, whose five operations answer the .npmrc lines (GetNpmAuth, GetNpmScopeAuth) and queue an npm local repository's reindex and strict-layout conversion (CalculateNpmRepositoryMetadata, MigrateNpmRepositoryToStrictLayout, ResumeNpmRepositoryMigration).
  • NPMOperation and IsNPMReindex, the paths that answer errors in Artifactory's envelope; NPMUserRequest, the login and logout paths the middleware reads no credentials on; KeepNPMCredential, which keeps the token a request authenticated with for the .npmrc lines; and NPMLoggedPath, which redacts the token of a logout path in the access log.
  • Credential, AuthLines, ScopeAuthLines and ValidScope: the .npmrc lines that GetNpmAuth and GetNpmScopeAuth answer.
  • Answers: for a local or federated repository, packuments and manifests built from the package index, with what the caller may not read left out; for a remote repository, the upstream metadata with only the tarball URLs rewritten and its signatures kept, and checked tarballs.

Invariants

  • A packument, manifest, search result or tag list shows only the versions the caller may read. A search without text by a caller who may read every path of the repository sees every package with a version, so its from is the package index's offset and only the page is read (#1425); any other search counts from over the candidates the caller may read.
  • The download policy, after read permission: the packument, a version's manifest and the dist-tags are checked at .npm/<name>/package.json, where 7.x keeps the packument, so the blackout or a pattern on that path refuses them with 404, locally and for a remote. A pattern on one version's tarball refuses only the tarball and leaves the version listed, and a remote tarball's read of its packument checks the blackout alone. A local search keeps its answers, as 6.20's searchers do not check the blackout (decision 0032).
  • Remote metadata is cached as the upstream sent it; an answer rewrites only its tarball URLs and keeps its signature fields, and a tarball's integrity is checked before it is published or served from the cache.
  • A write to a remote repository answers 405, and statuses and error texts stay byte-identical: npm reads them.
  • A caller's raw token reaches no handler but the .npmrc operations: only KeepNPMCredential stores it in the request's context, under a key private to this package, and only NPMAPI reads it back.

References

Status

Implemented P7, with remote retrieval in P9. Moved here in P9d, which also moved the server root's npm operations here (WEB-17).

Testing

  • npm_test.go, publish_test.go, lifecycle_test.go, latest_test.go and search_test.go: the registry routes through a server, their permissions, refusals and store faults.
  • search_page_test.go: a late search page of 5,000 packages reads the index a page of candidates at a time, and only its page for a caller who may read the whole repository.
  • user_test.go: login and logout with the real authenticator and token service, built by the access test kit (access/accesstest).
  • remote_test.go, remote_dist_test.go and remote_integrity_test.go: remote metadata, tarball integrity, URL policy and outages.
  • policy_test.go: the download policy on local and remote packuments, manifests, dist-tags, tarballs and search.
  • npmrc_test.go: the .npmrc lines and scopes.
  • login_test.go: the login and logout path predicates, the configuration operations' paths and the logged-path redactor.
  • operations_test.go: NPMAPI's operations without the router, with no store, no caller and a store that fails under them; fixture_test.go holds their fixtures.
  • served_copy_fault_test.go: a cached package document whose copy the server fails to read answers 500, logged once, and stays cached (#1488).

Index

Constants

View Source
const DefaultLoginTokenExpiry = 30 * 24 * time.Hour

DefaultLoginTokenExpiry is decision 0034's default npm.login_token_expiry: the month a token from npm login lasts on 7.x (KB 000006713).

View Source
const DefaultMaxPublishSize = 1 << 30

DefaultMaxPublishSize is decision 0034's default npm.max_publish_size.

Variables

This section is empty.

Functions

func AuthLines

func AuthLines(c Credential) string

AuthLines is GET /api/npm/auth's answer: _auth with the base64 of user:<encrypted password> for a Basic caller, as 7.x's documentation shows it, or _authToken for a token caller. 7.161.26 stopped sending the always-auth and email lines (RTDEV-96526).

func IsNPMReindex

func IsNPMReindex(path string) bool

IsNPMReindex reports whether path is the JFrog reindex operation beside an npm registry, which answers in Artifactory's envelope, as the JFrog operations there do on 7.x (RTFACT-30181), where the registry answers in npm's. Only POST is the operation: to a GET, "reindex" is a package, and a refusal the server answers before routing gives its npm client Artifactory's envelope.

func KeepNPMCredential

func KeepNPMCredential(ctx context.Context, r *http.Request, p kernel.Principal) context.Context

KeepNPMCredential keeps, for the npm configuration operations alone, the token a request authenticated with, which they write into .npmrc as it came. Nothing else needs the raw token, so no other request carries it further than the middleware. It reads the credential as the middleware does: an API key first (accessapi.APIKey), which authenticates a request that sends one whatever its Authorization header says, as 7.x's props filter does, so a Basic or Bearer value beside it, which nothing verified, is never written in its place. Then Basic and Bearer (accessapi.BasicCredentials, accessapi.BearerToken), not Request.BasicAuth: a lower-case "basic" value, a blank "Bearer" or another scheme authenticates nothing in 7.x. Nor do Basic credentials naming another user, such as the anonymous name the middleware declines while anonymous access is off; an empty name is the token's subject (authn.Authenticator.Token).

func NPMLoggedPath

func NPMLoggedPath(path string) string

NPMLoggedPath is path as the access log records it: the token a logout's path names is a credential, which must not reach the log. A token that cannot be revoked outlives the logout, and the log outlives both. Any other path comes back unchanged.

func NPMOperation

func NPMOperation(path string) bool

NPMOperation reports whether path is one of the JFrog operations that answer npm's configuration, which answer errors in the Artifactory envelope, {"errors":[...]}, as 7.x does (RTFACT-30181), where the registry beside them answers npm's.

func NPMUserRequest

func NPMUserRequest(r *http.Request) bool

NPMUserRequest is npmUserPath of r's escaped path, the skip accesshttp.Authenticate asks first.

func Protocol

func Protocol(o Options) pkgroute.Protocol

Protocol is npm's registry protocol.

func ScopeAuthLines

func ScopeAuthLines(scope, registry string, c Credential) string

ScopeAuthLines is GET /api/npm/{repoKey}/auth/{scope}'s answer: the scope mapped to registry, the repository's registry URL ending in a slash, then the credential keyed to that URL less its scheme, so npm sends it to that registry alone. A Basic caller gets _password, the base64 of the encrypted password, and username, as 7.x's documentation shows them; the email and always-auth lines went in 7.161.26.

func ValidScope

func ValidScope(scope string) bool

ValidScope reports whether scope, without its @, can name an npm scope: npm accepts it in a scoped name. A scope is written into .npmrc, so anything else, a line break among it, is refused.

Types

type Accounts

type Accounts struct {
	// Passwords verifies the user name and password a login sends. Nil
	// answers login 501.
	Passwords Passwords
	// Tokens issues a login's token and revokes the one a logout names.
	// Nil answers login 501, and logout revokes nothing.
	Tokens Tokens
	// TokenExpiry is how long a login's token lasts
	// (npm.login_token_expiry); zero means DefaultLoginTokenExpiry.
	TokenExpiry time.Duration
}

Accounts is what npm's legacy login and logout need from the server: the authenticator that checks every other password login, and the token service (decision 0005).

type Credential

type Credential struct {
	// User and Password are set for a Basic caller. Password is the
	// caller's encrypted password (DV-004) when it sent its password, and
	// the token it sent when it sent one as the password: never a password.
	User, Password string
	// Token is set for a token caller, to the token it sent.
	Token string
}

Credential is what GET /api/npm/auth and its scoped form write into .npmrc for a caller (decision 0034): a user name and the secret that stands for the password, for a caller that sent Basic credentials, or the token a caller sent as Bearer or as an API key (X-JFrog-Art-Api or X-Api-Key).

type NPMAPI

type NPMAPI struct{ *itemkit.Kit }

NPMAPI is the npm ecosystem's partial strict server: the repository metadata and strict-layout operations and the npm auth documents, over the item kit.

func (NPMAPI) CalculateNpmRepositoryMetadata

CalculateNpmRepositoryMetadata queues the reindex of an npm local repository's index, the item kit's Reindex that every format shares, and answers 7.x's text once the work is queued. It refuses as npmOperation does.

func (NPMAPI) GetNpmAuth

GetNpmAuth answers .npmrc lines that authenticate npm as the caller.

func (NPMAPI) GetNpmScopeAuth

GetNpmScopeAuth answers .npmrc lines that point an npm scope at a repository and authenticate npm to it as the caller. Any npm repository may be named, whatever its class. The scope comes without its @ (R25 4); one sent with it is taken as meant.

func (NPMAPI) MigrateNpmRepositoryToStrictLayout

MigrateNpmRepositoryToStrictLayout queues the npm format's conversion of a local repository to the strict layout (npmfmt.KindConvert), keyed by the repository, and answers 202 once it is queued. It refuses as npmOperation does.

func (NPMAPI) ResumeNpmRepositoryMigration

ResumeNpmRepositoryMigration queues again a conversion the work queue parked once its retries ran out. A conversion cannot be cancelled, so that is the only one to resume; any other answers 400. [I] 7.x's answer then is unknown (#216).

type Options

type Options struct {
	// RemoteSources opts this adapter into remote retrieval.
	RemoteSources *source.Service
	// Accounts verifies the passwords npm login sends and keeps the tokens
	// it issues.
	Accounts Accounts
	// MaxPublishSize is the largest publish request accepted
	// (npm.max_publish_size); zero means DefaultMaxPublishSize.
	MaxPublishSize int64
	// Format are the npm format's settings, which a read applies when it
	// resolves latest itself, as the format would settle it.
	Format npmfmt.Settings
	// KeyPairs reads the key pairs whose public keys the keys endpoint
	// lists (decision 0034, "Signatures"). Without it no repository lists
	// any, and the endpoint answers 404 as before signing existed.
	KeyPairs accessapi.Keys
}

Options are the npm registry's settings.

type Passwords

type Passwords interface {
	Basic(ctx context.Context, name, password string) (kernel.Principal, error)
	Token(ctx context.Context, raw, user string) (kernel.Principal, error)
}

Passwords verifies a login's credentials as the server's middleware does. Basic and Token are accessapi.Authenticator's, which satisfies it, with that contract's answers and errors: login answers a refusal as it comes.

type Tokens

type Tokens interface {
	Issue(ctx context.Context, r accessapi.TokenRequest) (accessapi.IssuedToken, error)
	RevokeValue(ctx context.Context, raw string) error
}

Tokens issues a login's token and revokes the one a logout names. Issue and RevokeValue are accessapi.Tokens', which satisfies it, with that contract's errors: logout tests them with errors.Is.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL