Coverage for polar/auth/scope.py: 99%
81 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 12:42 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 12:42 +0000
1from enum import StrEnum
3from pydantic import GetJsonSchemaHandler
4from pydantic.json_schema import JsonSchemaValue
5from pydantic_core import core_schema as cs
8class Scope(StrEnum):
9 openid = "openid"
10 profile = "profile"
11 email = "email"
12 user_read = "user:read"
14 web_read = "web:read" # Read-only web access
15 web_write = "web:write" # Write web access
17 organizations_read = "organizations:read"
18 organizations_write = "organizations:write"
20 custom_fields_read = "custom_fields:read"
21 custom_fields_write = "custom_fields:write"
23 discounts_read = "discounts:read"
24 discounts_write = "discounts:write"
26 checkout_links_read = "checkout_links:read"
27 checkout_links_write = "checkout_links:write"
29 checkouts_read = "checkouts:read"
30 checkouts_write = "checkouts:write"
32 transactions_read = "transactions:read"
33 transactions_write = "transactions:write"
35 payouts_read = "payouts:read"
36 payouts_write = "payouts:write"
38 products_read = "products:read"
39 products_write = "products:write"
41 benefits_read = "benefits:read"
42 benefits_write = "benefits:write"
44 events_read = "events:read"
45 events_write = "events:write"
47 meters_read = "meters:read"
48 meters_write = "meters:write"
50 files_read = "files:read"
51 files_write = "files:write"
53 subscriptions_read = "subscriptions:read"
54 subscriptions_write = "subscriptions:write"
56 customers_read = "customers:read"
57 customers_write = "customers:write"
59 members_read = "members:read"
60 members_write = "members:write"
62 wallets_read = "wallets:read"
63 wallets_write = "wallets:write"
65 customer_meters_read = "customer_meters:read"
67 customer_sessions_write = "customer_sessions:write"
69 customer_seats_read = "customer_seats:read"
70 customer_seats_write = "customer_seats:write"
72 orders_read = "orders:read"
73 orders_write = "orders:write"
75 refunds_read = "refunds:read"
76 refunds_write = "refunds:write"
77 payments_read = "payments:read"
79 metrics_read = "metrics:read"
81 webhooks_read = "webhooks:read"
82 webhooks_write = "webhooks:write"
84 external_organizations_read = "external_organizations:read"
86 license_keys_read = "license_keys:read"
87 license_keys_write = "license_keys:write"
89 repositories_read = "repositories:read"
90 repositories_write = "repositories:write"
92 issues_read = "issues:read"
93 issues_write = "issues:write"
95 customer_portal_read = "customer_portal:read"
96 customer_portal_write = "customer_portal:write"
98 notifications_read = "notifications:read"
99 notifications_write = "notifications:write"
101 notification_recipients_read = "notification_recipients:read"
102 notification_recipients_write = "notification_recipients:write"
104 @classmethod
105 def __get_pydantic_json_schema__(
106 cls, core_schema: cs.CoreSchema, handler: GetJsonSchemaHandler
107 ) -> JsonSchemaValue:
108 json_schema = handler(core_schema)
109 json_schema = handler.resolve_ref_schema(json_schema)
110 json_schema["enumNames"] = SCOPES_SUPPORTED_DISPLAY_NAMES
111 return json_schema
114RESERVED_SCOPES = {Scope.web_read, Scope.web_write}
115SCOPES_SUPPORTED = [s.value for s in Scope if s not in RESERVED_SCOPES]
116SCOPES_SUPPORTED_DISPLAY_NAMES: dict[Scope, str] = {
117 Scope.openid: "OpenID",
118 Scope.profile: "Read your profile",
119 Scope.email: "Read your email address",
120 Scope.web_read: "Web Read Access",
121 Scope.web_write: "Web Write Access",
122 Scope.user_read: "User Read",
123 Scope.organizations_read: "Read your organizations",
124 Scope.organizations_write: "Create or modify organizations",
125 Scope.custom_fields_read: "Read custom fields",
126 Scope.custom_fields_write: "Create or modify custom fields",
127 Scope.discounts_read: "Read discounts",
128 Scope.discounts_write: "Create or modify discounts",
129 Scope.checkout_links_read: "Read checkout links",
130 Scope.checkout_links_write: "Create or modify checkout links",
131 Scope.checkouts_read: "Read checkout sessions",
132 Scope.checkouts_write: "Create or modify checkout sessions",
133 Scope.transactions_read: "Read transactions",
134 Scope.transactions_write: "Create or modify transactions",
135 Scope.payouts_read: "Read payouts",
136 Scope.payouts_write: "Create or modify payouts",
137 Scope.products_read: "Read products",
138 Scope.products_write: "Create or modify products",
139 Scope.benefits_read: "Read benefits",
140 Scope.benefits_write: "Create or modify benefits",
141 Scope.events_read: "Read events",
142 Scope.events_write: "Create events",
143 Scope.meters_read: "Read meters",
144 Scope.meters_write: "Create or modify meters",
145 Scope.files_read: "Read file uploads",
146 Scope.files_write: "Create or modify file uploads",
147 Scope.subscriptions_read: "Read subscriptions made on your organizations",
148 Scope.subscriptions_write: (
149 "Create or modify subscriptions made on your organizations"
150 ),
151 Scope.customers_read: "Read customers",
152 Scope.customers_write: "Create or modify customers",
153 Scope.members_read: "Read members",
154 Scope.members_write: "Create or modify members",
155 Scope.wallets_read: "Read wallets",
156 Scope.wallets_write: "Create or modify wallets",
157 Scope.customer_meters_read: "Read customer meters",
158 Scope.customer_sessions_write: "Create or modify customer sessions",
159 Scope.customer_seats_read: "Read customer seats",
160 Scope.customer_seats_write: "Create or modify customer seats",
161 Scope.orders_read: "Read orders made on your organizations",
162 Scope.orders_write: "Modify orders made on your organizations",
163 Scope.refunds_read: "Read refunds made on your organizations",
164 Scope.refunds_write: "Create or modify refunds",
165 Scope.payments_read: "Read payments made on your organizations",
166 Scope.metrics_read: "Read metrics",
167 Scope.webhooks_read: "Read webhooks",
168 Scope.webhooks_write: "Create or modify webhooks",
169 Scope.external_organizations_read: "Read external organizations",
170 Scope.license_keys_read: "Read license keys",
171 Scope.license_keys_write: "Modify license keys",
172 Scope.customer_portal_read: "Read your orders, subscriptions and benefits",
173 Scope.customer_portal_write: "Create or modify your orders, subscriptions and benefits",
174 Scope.notifications_read: "Read notifications",
175 Scope.notifications_write: "Mark notifications as read",
176 Scope.notification_recipients_read: "Read notification recipients",
177 Scope.notification_recipients_write: "Create or modify notification recipients",
178}
181def scope_to_set(scope: str) -> set[Scope]:
182 return {Scope(x) for x in scope.strip().split()}
185def scope_to_list(scope: str) -> list[Scope]:
186 return list(scope_to_set(scope))