|
| 1 | +<p align="center"> |
| 2 | + <a href="https://www.propelauth.com?ref=github" target="_blank" align="center"> |
| 3 | + <img src="https://www.propelauth.com/imgs/lockup.svg" width="200"> |
| 4 | + </a> |
| 5 | +</p> |
| 6 | + |
| 7 | +# PropelAuth Cloudflare Library |
| 8 | + |
| 9 | +A Javascript library for managing authentication, backed by [PropelAuth](https://www.propelauth.com?ref=github). While this library was built specifically with Cloudflare Workers in mind, it can also be used in edge/serverless contexts. |
| 10 | + |
| 11 | +[PropelAuth](https://www.propelauth.com?ref=github) makes it easy to add authentication and authorization to your B2B/multi-tenant application. |
| 12 | + |
| 13 | +Your frontend gets a beautiful, safe, and customizable login screen. Your backend gets easy authorization with just a few lines of code. You get an easy-to-use dashboard to config and manage everything. |
| 14 | + |
| 15 | +## Documentation |
| 16 | + |
| 17 | +- Full reference this library is [here](https://docs.propelauth.com/reference/backend-apis/cloudflare-workers) |
| 18 | +- Getting started guides for PropelAuth are [here](https://docs.propelauth.com/) |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +```shell |
| 23 | +npm install @propelauth/cloudflare-worker |
| 24 | +``` |
| 25 | + |
| 26 | +## Initialize |
| 27 | + |
| 28 | +`initAuth` performs a one-time initialization of the library. |
| 29 | +It will verify your `apiKey` is correct and fetch the metadata needed to verify access tokens in [validateAccessTokenAndGetUserClass](#protect-api-routes). |
| 30 | + |
| 31 | +You can find the `authUrl`, `apiKey`, and `verifierKey` in the **Backend Integration** section in your PropelAuth dashboard. |
| 32 | + |
| 33 | +```typescript |
| 34 | +import { initAuth } from '@propelauth/cloudflare-worker' |
| 35 | + |
| 36 | +const { |
| 37 | + validateAccessTokenAndGetUserClass, |
| 38 | + // ... |
| 39 | +} = initAuth({ |
| 40 | + authUrl: 'REPLACE_ME', |
| 41 | + apiKey: 'REPLACE_ME', |
| 42 | + verifierKey: 'REPLACE_ME', |
| 43 | +}) |
| 44 | +``` |
| 45 | + |
| 46 | +## Protect API Routes |
| 47 | + |
| 48 | +After initializing auth, you can verify access tokens by passing it in the Authorization header (formatted `Bearer TOKEN`) to `validateAccessTokenAndGetUserClass`. |
| 49 | +You can see more information about the User Class [here](https://docs.propelauth.com/reference/backend-apis/cloudflare-workers#user-class). |
| 50 | + |
| 51 | +```ts |
| 52 | +const authorizationHeader = // Get the Authorization header from an HTTP request |
| 53 | +try { |
| 54 | + const user = await validateAccessTokenAndGetUserClass(authorizationHeader) |
| 55 | + console.log(`Got request from user ${user.userId}`); |
| 56 | +} catch (err) { |
| 57 | + // You can return a 401, or continue the request knowing it wasn't sent from a logged-in user |
| 58 | + console.log(`Unauthorized request ${err}`); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +## Authorization / Organizations |
| 63 | + |
| 64 | +You can also verify which organizations the user is in, and which roles and permissions they have in each organization all through the [User Class](https://docs.propelauth.com/reference/backend-apis/cloudflare-workers#user-class). |
| 65 | + |
| 66 | +### Check Org Membership |
| 67 | + |
| 68 | +Verify that the request was made by a valid user **and** that the user is a member of the specified organization. |
| 69 | + |
| 70 | +```ts |
| 71 | +const authorizationHeader = // Get the Authorization header from an HTTP request |
| 72 | +const orgId = // get the orgId from somewhere, such as the request URL |
| 73 | +try { |
| 74 | + const user = await validateAccessTokenAndGetUserClass(authorizationHeader) |
| 75 | + const org = user.getOrg(orgId) |
| 76 | + if (!org) { |
| 77 | + // return a 403 |
| 78 | + } |
| 79 | + console.log(`Got request from user ${user.userId} for org ${org.orgName}`); |
| 80 | +} catch (err) { |
| 81 | + // You can return a 401, or continue the request knowing it wasn't sent from a logged-in user |
| 82 | + console.log(`Unauthorized request ${err}`); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### Check Org Membership and Role |
| 87 | + |
| 88 | +Similar to checking org membership, but will also verify that the user has a specific Role in the organization. |
| 89 | + |
| 90 | +A user has a Role within an organization. By default, the available roles are Owner, Admin, or Member, but these can be configured. These roles are also hierarchical, so Owner > Admin > Member. |
| 91 | + |
| 92 | +```ts |
| 93 | +const authorizationHeader = // Get the Authorization header from an HTTP request |
| 94 | +const orgId = // get the orgId from somewhere, such as the request URL |
| 95 | +try { |
| 96 | + const user = await validateAccessTokenAndGetUserClass(authorizationHeader) |
| 97 | + const org = user.getOrg(orgId) |
| 98 | + if (!org || !org.isRole("Admin")) { |
| 99 | + // return a 403 |
| 100 | + } |
| 101 | + console.log(`Got request from Admin user ${user.userId} for org ${org.orgName}`); |
| 102 | +} catch (err) { |
| 103 | + // You can return a 401, or continue the request knowing it wasn't sent from a logged-in user |
| 104 | + console.log(`Unauthorized request ${err}`); |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Check Org Membership and Permission |
| 109 | + |
| 110 | +Similar to checking org membership, but will also verify that the user has the specified permission in the organization. |
| 111 | + |
| 112 | +Permissions are arbitrary strings associated with a role. For example, `can_view_billing`, `ProductA::CanCreate`, and `ReadOnly` are all valid permissions. You can create these permissions in the PropelAuth dashboard. |
| 113 | + |
| 114 | +```ts |
| 115 | +const authorizationHeader = // Get the Authorization header from an HTTP request |
| 116 | +const orgId = // get the orgId from somewhere, such as the request URL |
| 117 | +try { |
| 118 | + const user = await validateAccessTokenAndGetUserClass(authorizationHeader) |
| 119 | + const org = user.getOrg(orgId) |
| 120 | + if (!org || !org.hasPermission("can_view_billing")) { |
| 121 | + // return a 403 |
| 122 | + } |
| 123 | + console.log(`User ${user.userId} has 'can_view_billing' permissions for org ${org.orgName}`); |
| 124 | +} catch (err) { |
| 125 | + // You can return a 401, or continue the request knowing it wasn't sent from a logged-in user |
| 126 | + console.log(`Unauthorized request ${err}`); |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +## Calling Backend APIs |
| 131 | + |
| 132 | +You can also use the library to call the PropelAuth APIs directly, allowing you to fetch users, create orgs, and a lot more. |
| 133 | + |
| 134 | +```ts |
| 135 | +const auth = initAuth({ |
| 136 | + authUrl: 'REPLACE_ME', |
| 137 | + apiKey: 'REPLACE_ME', |
| 138 | + verifierKey: 'REPLACE_ME', |
| 139 | +}) |
| 140 | + |
| 141 | +const magicLink = await auth.createMagicLink({ |
| 142 | + |
| 143 | +}) |
| 144 | +``` |
| 145 | + |
| 146 | +See the [API Reference](https://docs.propelauth.com/reference) for more information. |
| 147 | + |
| 148 | + |
| 149 | +## Questions? |
| 150 | + |
| 151 | +Feel free to reach out at [email protected] |
| 152 | + |
0 commit comments