# What npm access tokens the library redacts, and what it leaves alone.
#
# Every token here is built from the ordered run
# 0123456789abcdefghijklmnopqrstuvwxyz, which no real credential is. That run is
# thirty-six characters and so is a whole token body — the shortest one the
# pattern reads, since the count is a floor. It is spelled in lowercase where
# the case does not matter and in uppercase where the case is what a case is
# about: base62 holds the letters of both, so either spelling is a body.

patterns: npm-access-token

# The one prefix npm has issued a token of this format with. There is a single
# kind of string here rather than a table of them, as the GitLab and Slack files
# have: a granular access token and the classic tokens created before them are
# written the same way, so nothing in the value says what it is allowed to do,
# and the cases below are that one shape in the places a token is written.

case:     a token in an environment assignment
in:       NPM_TOKEN=npm_0123456789abcdefghijklmnopqrstuvwxyz
out:      NPM_TOKEN=«npm-access-token»

case:     a token in an npmrc
in:       //registry.npmjs.org/:_authToken=npm_0123456789abcdefghijklmnopqrstuvwxyz
out:      //registry.npmjs.org/:_authToken=«npm-access-token»

case:     a token in the authorization header a request to the registry carries
in:       Authorization: Bearer npm_0123456789abcdefghijklmnopqrstuvwxyz
out:      Authorization: Bearer «npm-access-token»

case:     a token on a command line
in:       npm config set //registry.npmjs.org/:_authToken npm_0123456789abcdefghijklmnopqrstuvwxyz
out:      npm config set //registry.npmjs.org/:_authToken «npm-access-token»

case:     a token in a workflow environment block
in:       env:\n  NODE_AUTH_TOKEN: npm_0123456789abcdefghijklmnopqrstuvwxyz
out:      env:\n  NODE_AUTH_TOKEN: «npm-access-token»

# The alphabet the thirty-six characters are written in. It is base62 — the
# letters of both cases and the digits — which is what both rulesets stating a
# shape admit behind the prefix and what npm's own account of the format names
# the checksum's encoding for. The two characters base64url adds, the hyphen and
# the underscore, are not admitted, and either of them ends a body.

case:     a body written in capitals
in:       npm_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
out:      «npm-access-token»

case:     a body carrying a hyphen
in:       npm_0123456789abcdef-ghijklmnopqrstuvwxyz
out:      npm_0123456789abcdef-ghijklmnopqrstuvwxyz

case:     a body carrying an underscore
in:       npm_0123456789abcdef_ghijklmnopqrstuvwxyz
out:      npm_0123456789abcdef_ghijklmnopqrstuvwxyz

# The lengths either side of thirty-six. npm states no length for a token; what
# it states is that the random part carries a hundred and seventy-eight bits and
# that six characters of checksum close the token, which comes to thirty-six
# behind the prefix. Like the GitHub bodies and unlike the AWS access key ID and
# the Google API key, it is read as a floor rather than a count: thirty-five
# characters are not a token, and thirty-seven are one token to the end of its
# run rather than a token and a character left over. What the floor costs is the
# middle case below — a token cut short of thirty-six keeps every random
# character written in front of the cut, and none of them is redacted.

case:     a body of thirty-six characters is a token
in:       npm_0123456789abcdefghijklmnopqrstuvwxyz
out:      «npm-access-token»

case:     a body of thirty-five characters is not
in:       npm_0123456789abcdefghijklmnopqrstuvwxy
out:      npm_0123456789abcdefghijklmnopqrstuvwxy

case:     an alphabet run longer than the floor is a token to the end of it
in:       npm_0123456789abcdefghijklmnopqrstuvwxyz0
out:      «npm-access-token»

# What a token may be written against. The prefix is the whole of the anchor and
# there is no boundary on either side of it, so a token keeps its span whatever
# word character it abuts — and where the run of its alphabet carries on past
# the floor, what carries it on is redacted with the token. A hyphen and an
# underscore end the run instead, which is what tells this pattern apart from
# the OpenAI and Anthropic ones: their bodies are base64url and would draw a
# hyphenated word in.

case:     a token after an underscore
in:       NPM_TOKEN_npm_0123456789abcdefghijklmnopqrstuvwxyz
out:      NPM_TOKEN_«npm-access-token»

case:     a token after a letter
in:       xnpm_0123456789abcdefghijklmnopqrstuvwxyz
out:      x«npm-access-token»

case:     a word written against a token
in:       npm_0123456789abcdefghijklmnopqrstuvwxyzsuffix
out:      «npm-access-token»

case:     a hyphenated word written against a token
in:       npm_0123456789abcdefghijklmnopqrstuvwxyz-suffix
out:      «npm-access-token»-suffix

case:     an underscored word written against a token
in:       npm_0123456789abcdefghijklmnopqrstuvwxyz_suffix
out:      «npm-access-token»_suffix

case:     a token at the end of a sentence
in:       the token is npm_0123456789abcdefghijklmnopqrstuvwxyz.
out:      the token is «npm-access-token».

# Two tokens written together. The three letters the prefix opens with belong to
# the alphabet a body is written in, so a body may close with npm and the
# underscore of the next token stand directly behind it; the second token begins
# three characters before the first one ends, the two spans overlap and are
# redacted as one. Written without that overlap the two are separate values, and
# both go.

case:     a token beginning inside the token before it
in:       npm_0123456789abcdefghijklmnopqrstuvwnpm_0123456789abcdefghijklmnopqrstuvwxyz
out:      «npm-access-token»

case:     two tokens with nothing between them
in:       npm_0123456789abcdefghijklmnopqrstuvwxyznpm_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
out:      «npm-access-token»

case:     two tokens separated by a space
in:       npm_0123456789abcdefghijklmnopqrstuvwxyz npm_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
out:      «npm-access-token» «npm-access-token»

# What this pattern redacts that nobody issued. The prefix carries an
# underscore, which standard base64 writes nowhere, so only a base64url encoding
# can hold one — and there four characters of an alphabet of sixty-four stand
# where the prefix stands about once in seventeen million characters. Where
# thirty-six base62 characters follow, the run from the prefix to the end of
# them is redacted whether or not npm issued it. That is the answer rather than
# a fault in it, because such a run is a token's format exactly and nothing in
# the text tells the two apart: sparing these would spare a real token written
# the same way. What is taken is a stretch of a value that was already opaque to
# a reader.

case:     the prefix inside a longer run of base64url
in:       payload=zzzznpm_0123456789abcdefghijklmnopqrstuvwxyzzzzz
out:      payload=zzzz«npm-access-token»

case:     the prefix where a signature stands
in:       eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhYmMifQ.zzzznpm_0123456789abcdefghijklmnopqrstuvwxyzzzzz
out:      eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhYmMifQ.zzzz«npm-access-token»

# A digest written behind the prefix, which is the one over-match here that
# takes a value a reader had a use for. Hexadecimal digits are base62 and a
# digest carries nothing that ends a run, so npm_ and the forty characters of a
# SHA-1 are a token's format exactly, as npm_ and the sixty-four of a SHA-256
# are — the shape a cache key takes where it is built from the prefix and the
# hash of a lock file. Both are redacted. Declining them would mean declining
# every token npm happened to write in the digits alone, which is a whole
# credential against a cache key. What holds either side of it is the floor and
# the prefix: an MD5 is thirty-two characters, four short of a body, and a
# hyphen is no character the prefix carries.

case:     a sha1 behind the prefix
in:       npm_0123456789abcdef0123456789abcdef01234567
out:      «npm-access-token»

case:     a sha256 in a cache key
in:       key: npm_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
out:      key: «npm-access-token»

case:     an md5 behind the prefix, four characters short of a body
in:       npm_0123456789abcdef0123456789abcdef
out:      npm_0123456789abcdef0123456789abcdef

case:     a sha1 behind a hyphen rather than the prefix
in:       npm-0123456789abcdef0123456789abcdef01234567
out:      npm-0123456789abcdef0123456789abcdef01234567

# And what this pattern leaves alone. The first three are the prefix, read in
# the case npm writes it and in all four of its characters. The two after them
# are the alphabet and the anchor: a body a space has broken, and a run of the
# right length with no prefix in front of it. Of the three after those, a digest
# and a sentence hold no underscore where the prefix wants one and so hold no
# position to be found at however long they run; the environment npm exports to
# a lifecycle script is written in snake_case and does carry the prefix, and
# what turns it away is the next underscore of the name, which ends the run long
# before thirty-six characters of it have been read.

case:     an uppercase prefix
in:       NPM_0123456789abcdefghijklmnopqrstuvwxyz
out:      NPM_0123456789abcdefghijklmnopqrstuvwxyz

case:     a hyphen where the prefix carries an underscore
in:       npm-0123456789abcdefghijklmnopqrstuvwxyz
out:      npm-0123456789abcdefghijklmnopqrstuvwxyz

case:     two characters of the prefix
in:       nxm_0123456789abcdefghijklmnopqrstuvwxyz
out:      nxm_0123456789abcdefghijklmnopqrstuvwxyz

case:     a body broken by a space
in:       npm_0123456789abcdef ghijklmnopqrstuvwxyz
out:      npm_0123456789abcdef ghijklmnopqrstuvwxyz

case:     a run of the right length opening with no prefix
in:       0123456789abcdefghijklmnopqrstuvwxyz
out:      0123456789abcdefghijklmnopqrstuvwxyz

case:     a hexadecimal digest
in:       sha1=0123456789abcdef0123456789abcdef01234567
out:      sha1=0123456789abcdef0123456789abcdef01234567

case:     prose
in:       there is no credential in this sentence
out:      there is no credential in this sentence

case:     the environment npm exports to a lifecycle script
in:       npm_package_version=1.0.0 npm_lifecycle_event=prepublishOnly
out:      npm_package_version=1.0.0 npm_lifecycle_event=prepublishOnly

# And an npm credential this pattern has nothing to say about. The format this
# one replaced was a bare UUID: hyphens where a body carries none, and no prefix
# in front of it, so there is no run shaped like a token to locate. npm has
# revoked the last of those, and one written into a file today is a string
# nothing here reads.

case:     a token of the uuid format npm issued before this one
in:       0123456789ab-cdef-0123-4567-89abcdef0123
out:      0123456789ab-cdef-0123-4567-89abcdef0123
