Coverage for polar/auth/scope.py: 99%

81 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-12-05 17:15 +0000

1from enum import StrEnum 1ab

2 

3from pydantic import GetJsonSchemaHandler 1ab

4from pydantic.json_schema import JsonSchemaValue 1ab

5from pydantic_core import core_schema as cs 1ab

6 

7 

8class Scope(StrEnum): 1ab

9 openid = "openid" 1ab

10 profile = "profile" 1ab

11 email = "email" 1ab

12 user_read = "user:read" 1ab

13 

14 web_read = "web:read" # Read-only web access 1ab

15 web_write = "web:write" # Write web access 1ab

16 

17 organizations_read = "organizations:read" 1ab

18 organizations_write = "organizations:write" 1ab

19 

20 custom_fields_read = "custom_fields:read" 1ab

21 custom_fields_write = "custom_fields:write" 1ab

22 

23 discounts_read = "discounts:read" 1ab

24 discounts_write = "discounts:write" 1ab

25 

26 checkout_links_read = "checkout_links:read" 1ab

27 checkout_links_write = "checkout_links:write" 1ab

28 

29 checkouts_read = "checkouts:read" 1ab

30 checkouts_write = "checkouts:write" 1ab

31 

32 transactions_read = "transactions:read" 1ab

33 transactions_write = "transactions:write" 1ab

34 

35 payouts_read = "payouts:read" 1ab

36 payouts_write = "payouts:write" 1ab

37 

38 products_read = "products:read" 1ab

39 products_write = "products:write" 1ab

40 

41 benefits_read = "benefits:read" 1ab

42 benefits_write = "benefits:write" 1ab

43 

44 events_read = "events:read" 1ab

45 events_write = "events:write" 1ab

46 

47 meters_read = "meters:read" 1ab

48 meters_write = "meters:write" 1ab

49 

50 files_read = "files:read" 1ab

51 files_write = "files:write" 1ab

52 

53 subscriptions_read = "subscriptions:read" 1ab

54 subscriptions_write = "subscriptions:write" 1ab

55 

56 customers_read = "customers:read" 1ab

57 customers_write = "customers:write" 1ab

58 

59 members_read = "members:read" 1ab

60 members_write = "members:write" 1ab

61 

62 wallets_read = "wallets:read" 1ab

63 wallets_write = "wallets:write" 1ab

64 

65 customer_meters_read = "customer_meters:read" 1ab

66 

67 customer_sessions_write = "customer_sessions:write" 1ab

68 

69 customer_seats_read = "customer_seats:read" 1ab

70 customer_seats_write = "customer_seats:write" 1ab

71 

72 orders_read = "orders:read" 1ab

73 orders_write = "orders:write" 1ab

74 

75 refunds_read = "refunds:read" 1ab

76 refunds_write = "refunds:write" 1ab

77 payments_read = "payments:read" 1ab

78 

79 metrics_read = "metrics:read" 1ab

80 

81 webhooks_read = "webhooks:read" 1ab

82 webhooks_write = "webhooks:write" 1ab

83 

84 external_organizations_read = "external_organizations:read" 1ab

85 

86 license_keys_read = "license_keys:read" 1ab

87 license_keys_write = "license_keys:write" 1ab

88 

89 repositories_read = "repositories:read" 1ab

90 repositories_write = "repositories:write" 1ab

91 

92 issues_read = "issues:read" 1ab

93 issues_write = "issues:write" 1ab

94 

95 customer_portal_read = "customer_portal:read" 1ab

96 customer_portal_write = "customer_portal:write" 1ab

97 

98 notifications_read = "notifications:read" 1ab

99 notifications_write = "notifications:write" 1ab

100 

101 notification_recipients_read = "notification_recipients:read" 1ab

102 notification_recipients_write = "notification_recipients:write" 1ab

103 

104 @classmethod 1ab

105 def __get_pydantic_json_schema__( 1ab

106 cls, core_schema: cs.CoreSchema, handler: GetJsonSchemaHandler 

107 ) -> JsonSchemaValue: 

108 json_schema = handler(core_schema) 1c

109 json_schema = handler.resolve_ref_schema(json_schema) 1c

110 json_schema["enumNames"] = SCOPES_SUPPORTED_DISPLAY_NAMES 1c

111 return json_schema 1c

112 

113 

114RESERVED_SCOPES = {Scope.web_read, Scope.web_write} 1ab

115SCOPES_SUPPORTED = [s.value for s in Scope if s not in RESERVED_SCOPES] 1ab

116SCOPES_SUPPORTED_DISPLAY_NAMES: dict[Scope, str] = { 1ab

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} 

179 

180 

181def scope_to_set(scope: str) -> set[Scope]: 1ab

182 return {Scope(x) for x in scope.strip().split()} 1c

183 

184 

185def scope_to_list(scope: str) -> list[Scope]: 1ab

186 return list(scope_to_set(scope))