Jun 8, 2022
Authentication with ngrok
Not every app fits the standard auth model of user registration, login forms, and password resets. Maybe you’re running a quick demo, working with a legacy system you can’t modify, or just need to lock something down fast.
ngrok’s Traffic Policy system lets you add authentication to any endpoint in minutes and without touching your application code.
Let’s walk through how to use Traffic Policy for Basic Auth, OAuth, OpenID Connect, and JWT validation.
What is Traffic Policy?
Traffic Policy is ngrok’s configuration language for managing traffic in ngrok’s cloud. You write rules in YAML that filter, validate, and route requests before they reach your upstream service. Each rule has:
- Expressions that match specific conditions (like checking a header or email domain)
- Actions that execute when conditions match (like denying access or validating a JWT)
You can chain multiple rules together. For authentication, this means you can validate tokens, restrict access by email domain, and add rate limiting all in one policy file.
HTTP Basic Auth
The simplest form of authentication prompts visitors for a username and password before granting access.
Create a YAML file (policy.yaml) with the following Traffic Policy rule:
Start an agent endpoint with that policy:
Basic Auth is great for one-off demos or sharing local work with a co-worker. For production, move to a more secure method.
OAuth 2.0
OAuth lets users log in with their existing credentials from providers like Google, GitHub, or Microsoft. They don’t need to use a password, you don’t have to manage them, and ngrok manages the provider infrastructure so you don’t have to set one up yourself.
When a user accesses your endpoint, they’re redirected to Google to authenticate, then back to your app. ngrok passes identity information as headers:
To restrict access to specific email domains, add an expression that denies
requests authenticated with any email that does not end with
your-company.com:
You can add multiple domain checks or allow specific email addresses for contractors.
OpenID Connect
For corporate or internal identity providers (Okta, Auth0, Azure AD), use OpenID Connect (OIDC).
First, configure your IdP with https://idp.ngrok.com/oauth2/callback as a
sign-in redirect URI. Then create your Traffic Policy:
Your endpoint is now protected by your corporate identity provider.
JWT validation
For API-to-API authentication, validate JSON Web Tokens (JWTs) in ngrok’s cloud before requests reach your service.
Invalid tokens get rejected before hitting your backend. You can also combine JWT validation with rate limiting keyed to the token for per-consumer limits.
Get started
To add authentication to your endpoints:
- Create a free ngrok account
- Add your authtoken with
ngrok config add-authtoken <token> - Write a Traffic Policy file with your auth rules
- Start your endpoint with
--traffic-policy-file
Explore the docs for each auth action:
For additional access control, you can also use IP restrictions to allowlist trusted sources or mTLS for certificate-based authentication on TLS endpoints.
Questions? Find us in the
ngrok/ngrok repo on GitHub, or email
support@ngrok.com.
Frequently asked questions
It depends on your use case:
- Basic Auth works well for quick demos, dev sharing, and internal tools.
- OAuth fits consumer-facing apps where users already have a Google, GitHub, or similar account.
- OIDC suits enterprise apps with a corporate identity provider.
- JWT handles API-to-API authentication for machine-to-machine communication.
Yes. Traffic Policy rules execute in order, so you can layer authentication with other actions like rate limiting or IP restrictions.
Auth actions work on HTTP/HTTPS endpoints. For TCP or TLS endpoints, use mTLS or IP restrictions instead.
Authentication happens in ngrok's cloud before traffic reaches your agent or upstream service. Invalid requests never touch your infrastructure.
Check the Traffic Inspector in your ngrok dashboard to see request details, including which policy rules matched and why requests were denied.
Related posts
Introducing the Basic Auth Traffic Policy action
Secure your endpoints with ngrok’s Basic Auth Traffic Policy, compatible with both interactive and programmatic access to your APIs or apps, in a few minutes.
Add Auth0 authentication to ngrok traffic
Securely add production-grade auth with self-service sign-ups, account recovery, and social auth for multiple platforms - integrate ngrok to Auth0.