Skip to content

Latest commit

 

History

History
39 lines (28 loc) · 1.77 KB

File metadata and controls

39 lines (28 loc) · 1.77 KB

Based on the codebase, particularly the test mocks implementation, here's how Grip handles GitHub API authentication:

When making requests to GitHub's API, Grip uses HTTP Basic Authentication with the following flow:

  1. Authentication headers are checked first:
def _authenticate(self, request):
    if 'Authorization' not in request.headers:
        return None
    dummy = requests.Request()
    requests.auth.HTTPBasicAuth(*self.auth)(dummy)
    if request.headers['Authorization'] != dummy.headers['Authorization']:
        return (401, {'content-type': 'application/json; charset=utf-8'},
                '{"message":"Bad credentials"}')
    return None

Key aspects of the authentication handling:

  1. If no Authorization header is present, requests are treated as unauthenticated (subject to stricter rate limits)
  2. When credentials are provided, they're validated using HTTP Basic Auth
  3. Invalid credentials result in a 401 response with a JSON error message: {"message":"Bad credentials"}

The authentication is important because GitHub's API has rate limiting:

  • Unauthenticated requests: 60 requests per hour
  • Authenticated requests: 5,000 requests per hour

This means that for heavy usage or in environments where you need to render many markdown files, proper authentication is crucial to avoid hitting rate limits.

When using Grip, you can provide GitHub credentials either through:

  • Environment variables
  • Command line arguments
  • API configuration

If invalid credentials are provided, Grip will receive the 401 error from GitHub and fall back to unauthenticated requests, but with the lower rate limit.

This implementation ensures secure handling of credentials while maintaining compatibility with GitHub's API requirements and rate limiting policies.