-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews_api.py
More file actions
345 lines (299 loc) · 12.9 KB
/
Copy pathviews_api.py
File metadata and controls
345 lines (299 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from http import HTTPStatus
from fastapi import APIRouter, Depends
from fastapi.exceptions import HTTPException
from lnbits.core.crud import get_user_access_control_lists, get_user_extension
from lnbits.core.models import AccessTokenPayload, SimpleStatus, User
from lnbits.core.models.users import AccountId
from lnbits.core.services.extensions import get_valid_extensions
from lnbits.db import Filters, Page
from lnbits.decorators import (
access_token_payload,
check_account_id_exists,
check_user_exists,
parse_filters,
)
from lnbits.helpers import generate_filter_params_openapi
from .crud import (
create_agent_profile,
delete_agent_profile,
get_activity_events_paginated,
get_agent_policy,
get_agent_profile,
get_agent_profiles_paginated,
update_agent_profile,
upsert_agent_policy,
)
from .models import (
ActivityEvent,
ActivityEventFilters,
AgentPolicy,
AgentProfile,
AgentProfileFilters,
CreateAgentPolicy,
CreateAgentProfile,
LnurlpStatus,
RuntimeBalanceResponse,
RuntimeInvoiceRequest,
RuntimeInvoiceResponse,
RuntimePaymentRequest,
RuntimePaymentResponse,
RuntimePaymentStatusResponse,
RuntimePolicyDecision,
RuntimeStatus,
TokenReference,
UpdateAgentProfile,
)
from .services import (
check_runtime_payment,
create_runtime_invoice,
dry_run_payment,
execute_payment,
get_runtime_balance,
get_runtime_status,
)
agent_profile_filters = parse_filters(AgentProfileFilters)
activity_event_filters = parse_filters(ActivityEventFilters)
agent_wallet_api_router = APIRouter()
@agent_wallet_api_router.get("/api/v1/tokens", response_model=list[TokenReference])
async def api_list_token_references(
account_id: AccountId = Depends(check_account_id_exists),
token: AccessTokenPayload = Depends(access_token_payload),
) -> list[TokenReference]:
"""List safe ACL token references for binding to agent profiles.
This endpoint never returns raw API token secrets. It only exposes ACL/token ids,
names, and a short token hint for UI selection.
"""
_reject_api_token(token)
user_acls = await get_user_access_control_lists(account_id.id)
refs: list[TokenReference] = []
for acl in user_acls.access_control_list:
for acl_token in acl.token_id_list:
refs.append(
TokenReference(
acl_id=acl.id,
acl_name=acl.name,
token_id=acl_token.id,
token_name=acl_token.name,
token_hint=acl_token.id[-6:] if acl_token.id else None,
)
)
return refs
@agent_wallet_api_router.get("/api/v1/lnurlp/status", response_model=LnurlpStatus)
async def api_lnurlp_status(
account_id: AccountId = Depends(check_account_id_exists),
token: AccessTokenPayload = Depends(access_token_payload),
) -> LnurlpStatus:
_reject_api_token(token)
valid_extension_ids = [ext.code for ext in await get_valid_extensions()]
installed = "lnurlp" in valid_extension_ids
user_ext = await get_user_extension(account_id.id, "lnurlp") if installed else None
enabled = bool(user_ext and user_ext.active)
message = None
if not installed:
message = "LNURLp extension is not installed on this LNbits instance."
elif not enabled:
message = "Enable LNURLp to use Lightning Address / LNURL-pay features."
return LnurlpStatus(installed=installed, enabled=enabled, message=message)
@agent_wallet_api_router.post("/api/v1/profiles", status_code=HTTPStatus.CREATED, response_model=AgentProfile)
async def api_create_agent_profile(
data: CreateAgentProfile,
user: User = Depends(check_user_exists),
token: AccessTokenPayload = Depends(access_token_payload),
) -> AgentProfile:
_reject_api_token(token)
_check_user_wallet_access(user, data.wallet)
await _check_acl_token(user.id, data.acl_id, data.token_id)
profile = await create_agent_profile(user.id, data)
return profile
@agent_wallet_api_router.get(
"/api/v1/profiles/paginated",
name="Agent Wallet Profiles List",
response_model=Page[AgentProfile],
openapi_extra=generate_filter_params_openapi(AgentProfileFilters),
)
async def api_get_agent_profiles_paginated(
account_id: AccountId = Depends(check_account_id_exists),
token: AccessTokenPayload = Depends(access_token_payload),
filters: Filters = Depends(agent_profile_filters),
) -> Page[AgentProfile]:
_reject_api_token(token)
return await get_agent_profiles_paginated(user_id=account_id.id, filters=filters)
@agent_wallet_api_router.get("/api/v1/profiles/{profile_id}", response_model=AgentProfile)
async def api_get_agent_profile(
profile_id: str,
account_id: AccountId = Depends(check_account_id_exists),
token: AccessTokenPayload = Depends(access_token_payload),
) -> AgentProfile:
_reject_api_token(token)
profile = await get_agent_profile(account_id.id, profile_id)
if not profile:
raise HTTPException(HTTPStatus.NOT_FOUND, "Agent profile not found.")
return profile
@agent_wallet_api_router.put("/api/v1/profiles/{profile_id}", response_model=AgentProfile)
async def api_update_agent_profile(
profile_id: str,
data: UpdateAgentProfile,
user: User = Depends(check_user_exists),
token: AccessTokenPayload = Depends(access_token_payload),
) -> AgentProfile:
_reject_api_token(token)
profile = await get_agent_profile(user.id, profile_id)
if not profile:
raise HTTPException(HTTPStatus.NOT_FOUND, "Agent profile not found.")
_check_user_wallet_access(user, data.wallet)
await _check_acl_token(user.id, data.acl_id, data.token_id)
updated = AgentProfile(**{**profile.dict(), **data.dict(exclude={"policy"})})
return await update_agent_profile(updated)
@agent_wallet_api_router.delete("/api/v1/profiles/{profile_id}", response_model=SimpleStatus)
async def api_delete_agent_profile(
profile_id: str,
account_id: AccountId = Depends(check_account_id_exists),
token: AccessTokenPayload = Depends(access_token_payload),
) -> SimpleStatus:
_reject_api_token(token)
profile = await get_agent_profile(account_id.id, profile_id)
if not profile:
raise HTTPException(HTTPStatus.NOT_FOUND, "Agent profile not found.")
await delete_agent_profile(account_id.id, profile_id)
return SimpleStatus(success=True, message="Agent profile deleted.")
@agent_wallet_api_router.get("/api/v1/profiles/{profile_id}/policy", response_model=AgentPolicy)
async def api_get_agent_policy(
profile_id: str,
account_id: AccountId = Depends(check_account_id_exists),
token: AccessTokenPayload = Depends(access_token_payload),
) -> AgentPolicy:
_reject_api_token(token)
profile = await get_agent_profile(account_id.id, profile_id)
if not profile:
raise HTTPException(HTTPStatus.NOT_FOUND, "Agent profile not found.")
policy = await get_agent_policy(profile_id)
if not policy:
raise HTTPException(HTTPStatus.NOT_FOUND, "Agent policy not found.")
return policy
@agent_wallet_api_router.put("/api/v1/profiles/{profile_id}/policy", response_model=AgentPolicy)
async def api_upsert_agent_policy(
profile_id: str,
data: CreateAgentPolicy,
account_id: AccountId = Depends(check_account_id_exists),
token: AccessTokenPayload = Depends(access_token_payload),
) -> AgentPolicy:
_reject_api_token(token)
profile = await get_agent_profile(account_id.id, profile_id)
if not profile:
raise HTTPException(HTTPStatus.NOT_FOUND, "Agent profile not found.")
return await upsert_agent_policy(profile_id, data)
@agent_wallet_api_router.get("/api/v1/profiles/{profile_id}/runtime/status", response_model=RuntimeStatus)
async def api_get_runtime_status(
profile_id: str,
account_id: AccountId = Depends(check_account_id_exists),
token: AccessTokenPayload = Depends(access_token_payload),
) -> RuntimeStatus:
profile = await get_agent_profile(account_id.id, profile_id)
if not profile:
raise HTTPException(HTTPStatus.NOT_FOUND, "Agent profile not found.")
_require_profile_token(token, profile)
return await get_runtime_status(profile)
@agent_wallet_api_router.get("/api/v1/profiles/{profile_id}/runtime/balance", response_model=RuntimeBalanceResponse)
async def api_get_runtime_balance(
profile_id: str,
account_id: AccountId = Depends(check_account_id_exists),
token: AccessTokenPayload = Depends(access_token_payload),
) -> RuntimeBalanceResponse:
profile = await get_agent_profile(account_id.id, profile_id)
if not profile:
raise HTTPException(HTTPStatus.NOT_FOUND, "Agent profile not found.")
_require_profile_token(token, profile)
return await get_runtime_balance(profile)
@agent_wallet_api_router.get(
"/api/v1/profiles/{profile_id}/runtime/payments/{checking_id}",
response_model=RuntimePaymentStatusResponse,
)
async def api_check_runtime_payment(
profile_id: str,
checking_id: str,
account_id: AccountId = Depends(check_account_id_exists),
token: AccessTokenPayload = Depends(access_token_payload),
) -> RuntimePaymentStatusResponse:
profile = await get_agent_profile(account_id.id, profile_id)
if not profile:
raise HTTPException(HTTPStatus.NOT_FOUND, "Agent profile not found.")
_require_profile_token(token, profile)
return await check_runtime_payment(profile, checking_id)
@agent_wallet_api_router.post(
"/api/v1/profiles/{profile_id}/runtime/invoice",
status_code=HTTPStatus.CREATED,
response_model=RuntimeInvoiceResponse,
)
async def api_create_runtime_invoice(
profile_id: str,
data: RuntimeInvoiceRequest,
account_id: AccountId = Depends(check_account_id_exists),
token: AccessTokenPayload = Depends(access_token_payload),
) -> RuntimeInvoiceResponse:
profile = await get_agent_profile(account_id.id, profile_id)
if not profile:
raise HTTPException(HTTPStatus.NOT_FOUND, "Agent profile not found.")
_require_profile_token(token, profile)
return await create_runtime_invoice(profile, data)
@agent_wallet_api_router.post(
"/api/v1/profiles/{profile_id}/runtime/dry-run",
response_model=RuntimePolicyDecision,
)
async def api_runtime_dry_run(
profile_id: str,
data: RuntimePaymentRequest,
account_id: AccountId = Depends(check_account_id_exists),
token: AccessTokenPayload = Depends(access_token_payload),
) -> RuntimePolicyDecision:
profile = await get_agent_profile(account_id.id, profile_id)
if not profile:
raise HTTPException(HTTPStatus.NOT_FOUND, "Agent profile not found.")
_require_profile_token(token, profile)
return await dry_run_payment(profile, data)
@agent_wallet_api_router.post(
"/api/v1/profiles/{profile_id}/runtime/pay",
response_model=RuntimePaymentResponse,
)
async def api_runtime_pay(
profile_id: str,
data: RuntimePaymentRequest,
account_id: AccountId = Depends(check_account_id_exists),
token: AccessTokenPayload = Depends(access_token_payload),
) -> RuntimePaymentResponse:
profile = await get_agent_profile(account_id.id, profile_id)
if not profile:
raise HTTPException(HTTPStatus.NOT_FOUND, "Agent profile not found.")
_require_profile_token(token, profile)
return await execute_payment(profile, data)
@agent_wallet_api_router.get(
"/api/v1/profiles/{profile_id}/activity",
response_model=Page[ActivityEvent],
openapi_extra=generate_filter_params_openapi(ActivityEventFilters),
)
async def api_get_activity_events(
profile_id: str,
account_id: AccountId = Depends(check_account_id_exists),
token: AccessTokenPayload = Depends(access_token_payload),
filters: Filters = Depends(activity_event_filters),
) -> Page[ActivityEvent]:
_reject_api_token(token)
profile = await get_agent_profile(account_id.id, profile_id)
if not profile:
raise HTTPException(HTTPStatus.NOT_FOUND, "Agent profile not found.")
return await get_activity_events_paginated(profile_id, filters=filters)
def _reject_api_token(token: AccessTokenPayload) -> None:
if token.api_token_id:
raise HTTPException(HTTPStatus.FORBIDDEN, "API token is only allowed on runtime endpoints.")
def _check_user_wallet_access(user: User, wallet_id: str) -> None:
if wallet_id not in [wallet.id for wallet in user.wallets]:
raise HTTPException(HTTPStatus.FORBIDDEN, "Not your wallet.")
async def _check_acl_token(user_id: str, acl_id: str, token_id: str) -> None:
acls = await get_user_access_control_lists(user_id)
acl = acls.get_acl_by_id(acl_id)
if not acl or not acl.get_token_by_id(token_id):
raise HTTPException(HTTPStatus.FORBIDDEN, "Invalid ACL token.")
def _require_profile_token(token: AccessTokenPayload, profile: AgentProfile) -> None:
if not token.api_token_id:
raise HTTPException(HTTPStatus.FORBIDDEN, "API token required.")
if token.api_token_id != profile.token_id:
raise HTTPException(HTTPStatus.FORBIDDEN, "Profile token mismatch.")