AZ-400: Designing and Implementing Microsoft DevOps Solutions

Design and Implement Authentication and Authorization Methods

Implement and manage GitHub Authentication

Effective GitHub authentication is critical for securing your repositories, workflows, and API interactions. In this guide, we cover three primary methods:

  • GitHub Apps
  • GITHUB_TOKEN
  • Personal Access Tokens (PATs)

Each approach has unique advantages, scope, and security considerations. Read on to determine which fits your DevOps and CI/CD pipelines best.


1. GitHub Apps

GitHub Apps act on behalf of your application—independent from user credentials. They can be installed on organizations or user accounts to interact with repositories, respond to events, and automate tasks like code reviews, CI/CD, and issue management.

The image is a diagram illustrating the capabilities of GitHub Apps, showing they can interact with organizations, access repositories, perform actions, and respond to events.

Benefits

  • Fine-grained permissions scoped to required actions
  • Enhanced security by separating app identity from user credentials
  • Detailed audit logs and install-based access control

Creating a GitHub App

  1. Navigate to GitHub SettingsDeveloper Settings.
  2. Select GitHub Apps and click New GitHub App.
  3. Provide the App name, homepage URL, and callback URL.
  4. Configure the required permissions and subscribe to relevant events.
  5. Save and download the private key for authentication.

The image shows a section of a GitHub interface for creating a GitHub App, with options for GitHub Apps, OAuth Apps, and personal access tokens. It includes a button to create a new GitHub App and links to developer documentation.

Authenticating with a GitHub App

Authentication is a two-step process:

  1. Generate a JSON Web Token (JWT) using your App’s private key.
  2. Exchange the JWT for an installation access token via GitHub’s API.

The image illustrates the process of authenticating with a GitHub app, highlighting the use of a private key and generating a JSON Web Token (JWT).

Example: Generate a JWT in Python (replace APP_ID and PRIVATE_KEY).

import jwt, time

payload = {
  "iat": int(time.time()),
  "exp": int(time.time()) + 600,
  "iss": APP_ID
}

jwt_token = jwt.encode(payload, PRIVATE_KEY, algorithm="RS256")

Use the resulting jwt_token to request an installation access token:

curl -X POST https://api.github.com/app/installations/INSTALLATION_ID/access_tokens \
     -H "Authorization: Bearer jwt_token" \
     -H "Accept: application/vnd.github.v3+json"

Note

After updating permissions, you must regenerate the installation access token for changes to take effect.

Managing Permissions

Regularly audit your App’s permissions in Settings. Use the App’s dashboard or API to adjust scopes and monitor usage.

The image is a flowchart illustrating the management of GitHub app permissions, including steps like managing permissions, maintaining security, and app settings.


2. GITHUB_TOKEN

GITHUB_TOKEN is an automatically generated secret available in GitHub Actions workflows. It provides repository-scoped authentication for checkout, API calls, and publishing packages—without manual secret management.

The image is a flowchart explaining the process of understanding GITHUB_TOKEN, showing that a workflow is initiated, GitHub Actions generates the token, and it is used for authentication.

Benefits

  • Auto-generated for every workflow run
  • Limited to current repository to minimize blast radius
  • No manual rotation or storage needed

The image is a slide titled "GITHUB_TOKEN – Benefits" with a highlighted point about "Automatic generation and scope limitation."

Usage Example

Use GITHUB_TOKEN from the secrets context:

name: CI Workflow
on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Run tests
        run: npm test
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Warning

Do not expose GITHUB_TOKEN to external URLs or untrusted actions—limit usage to internal steps only.


3. Personal Access Tokens (PATs)

Personal Access Tokens (PATs) provide user-level authentication for GitHub APIs and Git operations. You can choose classic or fine-grained tokens to control scope and expiration.

The image is an introduction to Personal Access Tokens (PATs) for GitHub, highlighting their use as alternative passwords for better security in accessing GitHub APIs and repositories.

Use Cases

  • CLI or script-based Git operations
  • REST or GraphQL API integrations
  • Third-party service authentication

Generating a PAT

  1. In GitHub Settings, open Developer SettingsPersonal access tokens.
  2. Click Generate new token.
  3. Choose classic or fine-grained.
  4. Select scopes (e.g., repo, workflow, admin:org).
  5. Generate the token and store it securely.

The image shows a screenshot of a GitHub interface for generating a personal access token (PAT), highlighting the "Generate new token" option.

The image shows a user interface for creating a new personal access token (classic) with options to set expiration and select various scopes for permissions. It includes sections for repository, workflow, and admin controls, among others.

Authenticating with a PAT

Include the token in the Authorization header for API calls:

curl -H "Authorization: token <PAT>" https://api.github.com/user/repos

Note

Store PATs in a secure vault or GitHub Secrets, rotate them regularly, and avoid embedding them in code.


Method Comparison and Best Practices

MethodScopeRotationIdeal Use Case
GitHub AppsOrg & repo, fine-grainedManual/APIIntegrations, bots, automated workflows
GITHUB_TOKENSingle repoAuto-rotatedGitHub Actions workflows
PATsUser-level, broad or narrowManualCLI scripts, local development, third-party tools

The image compares three authentication methods: GitHub Apps, GITHUB_TOKEN, and PATs, highlighting their features and ideal use cases.

Security Best Practices

  • Grant the minimum required permissions.
  • Rotate keys and tokens frequently.
  • Monitor audit logs for unauthorized access.
  • Enforce SAML SSO, OAuth apps, and branch protection rules.

Watch Video

Watch video content

Previous
Choose between Service Principals and Managed Identity