Skip to content
Open dashboard (opens in a new tab)

Authentication

Authenticate an API in a pipeline

A recorded login signs in through a browser, and an API has no login page to record. Most APIs hand out a bearer token from an endpoint instead. For a pipeline, the pattern is to request a fresh token just before the scan, store it in a header authentication, and scan with that authentication.

Every recipe on this page ends the same way:

Terminal window
nightvision auth headers update my-api-token -H "Authorization: Bearer $ACCESS_TOKEN"
nightvision scan my-api --project my-project --auth my-api-token

auth headers update replaces the stored header values, so each run starts with a token that is still valid.

Replace my-project with the name of the application’s project. The account behind NIGHTVISION_TOKEN needs a role on that project. The CLI splits a -H value at commas, so a header value that contains a comma does not work here.

  • A header authentication named my-api-token, created once. Create it in the dashboard, or from your own terminal with any placeholder value:

    Terminal window
    nightvision auth headers create my-api-token -H "Authorization: Bearer placeholder"

    See Headers and cookies for the dashboard steps and for assigning it to an application.

  • An application in NightVision for the API, named my-api here. See Applications.

  • An API token for the pipeline, stored as the secret NIGHTVISION_TOKEN. It updates an authentication, so the account behind it needs the Project Manager role on the project. See API tokens.

  • The credentials your API issues tokens for (a client id and secret, or a service account’s username and password), stored as CI secrets. Use an account made for scanning, with the least access that still reaches the routes you want tested.

Request a token from the provider’s token endpoint with the client credentials grant.

Terminal window
ACCESS_TOKEN=$(curl -sS --fail -X POST "$TOKEN_URL" \
--data-urlencode grant_type=client_credentials \
--data-urlencode client_id="$CLIENT_ID" \
--data-urlencode client_secret="$CLIENT_SECRET" \
--data-urlencode scope="$SCOPE" | jq -er .access_token)
nightvision auth headers update my-api-token -H "Authorization: Bearer $ACCESS_TOKEN"
nightvision scan my-api --project my-project --auth my-api-token

--data-urlencode keeps a secret that contains +, &, % or = intact. Leave out the scope line if your provider does not use scopes. jq -e makes the step fail if the response has no access_token, so a wrong secret stops the job before the scan. Some providers expect the client id and secret in an Authorization: Basic header instead of the body. Use whichever your provider documents.

Post the scan account’s username and password to the API’s own login route and read the token from the response.

Terminal window
ACCESS_TOKEN=$(curl -sS --fail -X POST "$API_URL/auth/login" \
-H "Content-Type: application/json" \
-d "$(jq -n --arg u "$SCAN_USER" --arg p "$SCAN_PASSWORD" '{username: $u, password: $p}')" \
| jq -er .token)
nightvision auth headers update my-api-token -H "Authorization: Bearer $ACCESS_TOKEN"
nightvision scan my-api --project my-project --auth my-api-token

Change the path, the field names in the request and the field read from the response (.token here) to match your API. jq -n --arg builds the body, so a password with quotes or other special characters stays valid JSON.

If the API takes a fixed key, there is nothing to request. Store the key as a CI secret and write it to the authentication on each run. That keeps the key in one place, so rotating it is a secret change and not an edit in the dashboard.

Terminal window
nightvision auth headers update my-api-token -H "X-API-Key: $API_KEY"
nightvision scan my-api --project my-project --auth my-api-token

If the key never changes, you can instead put it in the authentication once and run only the scan command.

Mask the token so it never appears in the job log, then run the same two commands, auth headers update and scan. The job installs the CLI first. See Install the CLI.

.github/workflows/api-scan.yml
name: API scan
on:
workflow_dispatch:
jobs:
scan:
runs-on: ubuntu-latest
env:
NIGHTVISION_TOKEN: ${{ secrets.NIGHTVISION_TOKEN }}
steps:
- name: Install the NightVision CLI
run: |
curl -L https://downloads.nightvision.net/binaries/latest/nightvision_latest_linux_amd64.tar.gz -q | tar -xz
sudo mv nightvision /usr/local/bin/
- name: Request an access token
run: |
ACCESS_TOKEN=$(curl -sS --fail -X POST "$TOKEN_URL" \
--data-urlencode grant_type=client_credentials \
--data-urlencode client_id="$CLIENT_ID" \
--data-urlencode client_secret="$CLIENT_SECRET" | jq -er .access_token)
echo "::add-mask::$ACCESS_TOKEN"
echo "ACCESS_TOKEN=$ACCESS_TOKEN" >> "$GITHUB_ENV"
env:
TOKEN_URL: ${{ secrets.API_TOKEN_URL }}
CLIENT_ID: ${{ secrets.API_CLIENT_ID }}
CLIENT_SECRET: ${{ secrets.API_CLIENT_SECRET }}
- name: Store the token and scan
run: |
nightvision auth headers update my-api-token -H "Authorization: Bearer $ACCESS_TOKEN"
nightvision scan my-api --project my-project --auth my-api-token

A header authentication has no login to replay, so --login-check tells you little about it. To fail fast on a bad token, call one authenticated route yourself before the scan.

Terminal window
curl -sS --fail -o /dev/null -H "Authorization: Bearer $ACCESS_TOKEN" "$API_URL/me"

After the scan, check the scan’s coverage. A scan that reached far fewer routes than usual is the sign that the token stopped working part way.