TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: How do you do authorization for background jobs?

3 pointsby ratpikover 3 years ago
When a flow is directly triggered by a users action, we have the Cookie&#x2F;Token of the user to do AuthN&#x2F;AuthZ.<p>However when we need to run background jobs or some asynchronous workflow on behalf of the user, we do not have the Cookie&#x2F;Token of the user. We do have the user id but not the session. How do you solve for AuthZ specifically for your background jobs and dependent microservices?

2 comments

gregjorover 3 years ago
Generate a one-time or short-lifespan token, pass that with the user ID and simulate a login&#x2F;session.<p>For a web app I work on that does this we generate auth tokens in the database, with an associated lifetime. Tokens are long-ish hashes from random bytes. The token is passed in the URL with the user ID. The app logs in as that user, deletes the token. Expired tokens that weren&#x27;t used for some reason get purged when they expire (scheduled event running in the database).<p>You could associate the token with a specific user ID in the database, use a secret to base the hash on so you don&#x27;t need the database... multiple possible variations depending on your needs and security requirements.
akajlaover 3 years ago
For this type of background job use-case (ex. some backfill, processing etc), I would think the main reason for authn&#x2F;authz would be to ensure that only the background job process can read and edit the appropriate user data. In that case, you would need some token-based machine-to-machine authz with scoped tokens that limit what data the background process can access.