{"id":22109673,"url":"https://github.com/jazcarate/cloudflare-worker-google-oauth","last_synced_at":"2025-09-03T19:32:33.926Z","repository":{"id":74110361,"uuid":"383839595","full_name":"jazcarate/cloudflare-worker-google-oauth","owner":"jazcarate","description":"a companion repository for a blogpost on how to OAuth using Google and Cloudflare Workers","archived":false,"fork":false,"pushed_at":"2024-08-07T07:30:41.000Z","size":188,"stargazers_count":30,"open_issues_count":0,"forks_count":9,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-28T11:32:25.011Z","etag":null,"topics":["cloudflare","google","oauth2"],"latest_commit_sha":null,"homepage":"https://apiumhub.com/tech-blog-barcelona/implementing-google-oauth-google-api-cloudflare-workers/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jazcarate.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-07T15:11:06.000Z","updated_at":"2024-12-14T13:32:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"04e409cf-b015-4eab-9d3c-4100a583dfe0","html_url":"https://github.com/jazcarate/cloudflare-worker-google-oauth","commit_stats":null,"previous_names":[],"tags_count":0,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jazcarate%2Fcloudflare-worker-google-oauth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jazcarate%2Fcloudflare-worker-google-oauth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jazcarate%2Fcloudflare-worker-google-oauth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jazcarate%2Fcloudflare-worker-google-oauth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jazcarate","download_url":"https://codeload.github.com/jazcarate/cloudflare-worker-google-oauth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231913890,"owners_count":18445009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloudflare","google","oauth2"],"created_at":"2024-12-01T09:36:34.128Z","updated_at":"2024-12-30T20:57:57.321Z","avatar_url":"https://github.com/jazcarate.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ( ◕◡◕)っ Cloudflare Workers Google OAuth\n\nThis is a companion repository for the blog post at [Apiumhub :: Tech blog](https://apiumhub.com/tech-blog-barcelona/implementing-google-oauth-google-api-cloudflare-workers/).\n\nIn the blog post, we go though the code that OAuth-s a user and uses a Google API for that user.\nWe'll be developing this app that lists, based on a query, the files in that user's Google Drive. Like so:\n\n![Result — Design is my passion](./docs/result.png)\n\n## One picture summary\n![Sequence of requests](./docs/sequence_of_requests.svg)\n\n\u003cdetails\u003e\n  \u003csummary\u003eGenerated with \u003ca href=\"https://sequencediagram.org/\" target=\"_blank\" rel=\"noopener noreferrer\"\u003esequencediagram.org\u003c/a\u003e\u003c/summary\u003e\n  \u003cpre\u003e\u003ccode\u003etitle Sequence of requests\n\nClient-\u003eCloudflare Worker: GET /\nnote left of Cloudflare Worker: The client is unauthenticated\nCloudflare Worker--\u003eClient: Redirect to Google Sign in\nClient-\u003eGoogle API: Ask for permission\nGoogle API--\u003eClient: Prompt to sign in\nClient-\u003eGoogle API: Grant permissions\nactivate Google API\nnote left of Google API: Google now\\nhas a session\\nfor the user\nGoogle API--\u003eClient: Go back to the Cloudflare Worker with a `token`\nClient-\u003eCloudflare Worker: GET /auth with a `code`\nCloudflare Worker-\u003eGoogle API: Exchange `code` for a `token`\nGoogle API--\u003eCloudflare Worker: a token\nactivate Cloudflare Worker\nnote left of Cloudflare Worker: An auth is stored in the KV with the code\nCloudflare Worker--\u003eClient: Go back to the original request with the auth cookie\nClient-\u003eCloudflare Worker: GET /\nnote left of Cloudflare Worker: Now the client is autenticated\nCloudflare Worker-\u003eGoogle API: Get files\nGoogle API--\u003eCloudflare Worker: A list of files\nCloudflare Worker--\u003eClient: An HTML with a list of files\nexpandable− logout\nClient-\u003eCloudflare Worker: GET /logout\nCloudflare Worker-\u003eGoogle API: Logout\ndeactivate Google API\nGoogle API--\u003eCloudflare Worker: OK\ndeactivate Cloudflare Worker\nCloudflare Worker--\u003eClient: OK\nend\n  \u003c/code\u003e\u003c/pre\u003e\n\u003c/details\u003e\n\nA more detail explanation of how Google Sign in should behave can be found in Google's docs: [Using OAuth 2.0 for Web Server Applications](https://developers.google.com/identity/protocols/oauth2/web-server).\n\n## Requirements\n\n- [wrangler](https://developers.cloudflare.com/workers/cli-wrangler/install-update) 1.17 or newer.\n- A [Cloudflare account](https://dash.cloudflare.com).\n- A Google Services account\n  - With the API you wish to consume enabled. e.g: https://console.developers.google.com/apis/api/drive.googleapis.com/overview\n  - A Google OAuth Client ID and Secret, from the [Credentials](https://console.cloud.google.com/apis/credentials) \u003e + Create credentials \u003e Oauh client ID. _Note: \"Authorized redirect URIs\" should have your cloudflare worker url with `/auth` at the end, and optionaly `http://127.0.0.1:8787/auth` to work locally.\n\n### Setup wrangler\n\n1. Clone this template: `wrangler generate [a name] https://github.com/jazcarate/cloudflare-worker-google-oauth`.\n1. Update the worker's secrets with the ones generated by creating the `Credentials` in the Google Service Account:\n   - `wrangler secret put CLIENT_ID [your client id]`\n   - `wrangler secret put CLIENT_SECRET [your client secret]`\n1. Create a `KV` namespace: `wrangler kv:namespace create \"authTokens\"` and update the `wrangler.toml` accordingly.\n\n## Ideas to grow this project\nIf you would like to use this setups as a starting point to develop interesting things; I recommend trying out one (or all!) of this improvements:\n\n- Create a middleware pattern to deaal with authenticated and unauthenticated endpoints\n- Serve static content, either with Cloudflare Sites, or reading local files in a Worker. A default path could be implemented to serve files in `public/` folder.\n- Improve the rendered HTML with a template library, or roll up your own!\n- Use another Google API from [the list](https://developers.google.com/workspace/products).\n\n\n## Glossary\nThroughout this project there are two somewhat similar, but distincts concepts:\n - Google's `code`: Is a one time code that is quickly exchanged with Google for a `token` 👇.\n - `token`: Is the authentication method for Google.\n - `auth`: Is the authentication method for this Worker.\n\nThe app deals with both, in very different ways; and it is the `KV` that provides a one way transformation between `auth` to `token`.\n \n## Contributing\nIf you think that you can improve the code, or the explanation; feel free to drop a pull request or a comment; but please keep in mind that the purpose of this is educational.\n\n## Developing\nOnce the requirements are fulfilled, simply run `npm run dev`.\n\nThe server will default to running at [127.0.0.1:8787](http://127.0.0.1:8787/).\n\n## Testing\nRn `npm test` for jest tests.\n\n## Publishing\nRun `npm run publish` to lint, build and publish to your Cloudflare Worker!\n\n#### ⚠️ Caveats\n\nSome 🪄 wizardry was implemented to know if the code was running locally (with `wrangler dev`) or in the cloud; as `wrangler dev` reweites the `request.url` to always match the environment.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjazcarate%2Fcloudflare-worker-google-oauth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjazcarate%2Fcloudflare-worker-google-oauth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjazcarate%2Fcloudflare-worker-google-oauth/lists"}