-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthfi.py
More file actions
321 lines (254 loc) · 10.1 KB
/
authfi.py
File metadata and controls
321 lines (254 loc) · 10.1 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
"""
AuthFI Python SDK
Usage:
from authfi import AuthFI
auth = AuthFI(
tenant="acme",
api_key="sk_live_...",
application_id="client-id", # optional
auto_sync=True,
)
# Flask
@app.route("/api/users")
@auth.require("read:users")
def get_users():
user = auth.current_user() # decoded JWT claims
return jsonify(users)
# FastAPI
@app.get("/api/users")
async def get_users(user=Depends(auth.require("read:users"))):
return users
# On startup
auth.sync()
"""
import json
import time
import base64
import hashlib
import hmac
import functools
from urllib.request import Request, urlopen
from urllib.error import HTTPError
from threading import local
_thread_local = local()
class AuthFIError(Exception):
def __init__(self, message, status=401):
super().__init__(message)
self.status = status
class AuthFI:
def __init__(
self,
tenant,
api_key,
api_url="https://api.authfi.app",
application_id=None,
client_secret=None,
auto_sync=True,
jwks_ttl=300,
):
self.tenant = tenant
self.api_key = api_key
self.api_url = api_url
self.application_id = application_id
self.client_secret = client_secret # required for cloud identity
self.auto_sync = auto_sync
self.jwks_ttl = jwks_ttl
self._jwks = None
self._jwks_fetched = 0
self._registered_permissions = {}
@property
def _manage_url(self):
return f"{self.api_url}/manage/v1/{self.tenant}"
@property
def _auth_url(self):
return f"{self.api_url}/v1/{self.tenant}"
# --- JWKS ---
def _fetch_jwks(self):
now = time.time()
if self._jwks and now - self._jwks_fetched < self.jwks_ttl:
return self._jwks
req = Request(f"{self._auth_url}/.well-known/jwks.json")
with urlopen(req) as res:
self._jwks = json.loads(res.read())
self._jwks_fetched = now
return self._jwks
def verify_token(self, token):
"""Verify JWT and return decoded claims.
Uses PyJWT with JWKS if available, falls back to unverified decode.
Install PyJWT for production: pip install PyJWT[crypto]
"""
parts = token.split(".")
if len(parts) != 3:
raise AuthFIError("Invalid token format")
# Try PyJWT with full RS256 verification
try:
import jwt
from jwt import PyJWKClient
jwks_url = f"{self._auth_url}/.well-known/jwks.json"
jwks_client = PyJWKClient(jwks_url)
signing_key = jwks_client.get_signing_key_from_jwt(token)
payload = jwt.decode(
token,
signing_key.key,
algorithms=["RS256"],
options={"verify_aud": False},
)
return payload
except ImportError:
pass # PyJWT not installed, fall back
# Fallback: decode without signature verification (NOT FOR PRODUCTION)
import warnings
warnings.warn(
"PyJWT not installed — token signature NOT verified. "
"Install with: pip install PyJWT[crypto]",
UserWarning,
stacklevel=2,
)
header = json.loads(_b64decode(parts[0]))
payload = json.loads(_b64decode(parts[1]))
if payload.get("exp", 0) < time.time():
raise AuthFIError("Token expired")
return payload
# --- Permission registration ---
def register_permission(self, name, description=None):
self._registered_permissions[name] = description
def sync(self):
"""Sync registered permissions with AuthFI."""
if not self._registered_permissions:
return
body = {
"permissions": [
{"name": name, "description": desc}
for name, desc in self._registered_permissions.items()
]
}
if self.application_id:
body["application_id"] = self.application_id
try:
data = self._api_call("PUT", "/permissions/sync", body)
print(f"[authfi] Synced {data['synced']} permissions ({data['total']} total)")
except Exception as e:
print(f"[authfi] Permission sync failed: {e}")
# --- Middleware ---
def require(self, *permissions):
"""Decorator that checks required permissions (ALL must match)."""
# Register for auto-sync
for p in permissions:
self.register_permission(p)
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
claims = self._authenticate_request()
user_perms = claims.get("permissions", [])
missing = [p for p in permissions if p not in user_perms]
if missing:
raise AuthFIError(
f"Insufficient permissions. Missing: {missing}", status=403
)
_thread_local.user = claims
return func(*args, **kwargs)
return wrapper
# If called as @auth.require("perm") for FastAPI Depends()
if len(permissions) == 1 and callable(permissions[0]):
fn = permissions[0]
permissions = ()
return decorator(fn)
return decorator
def require_role(self, *roles):
"""Decorator that checks required roles (ANY must match)."""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
claims = self._authenticate_request()
user_roles = claims.get("roles", [])
if not any(r in user_roles for r in roles):
raise AuthFIError(
f"Insufficient role. Required one of: {roles}", status=403
)
_thread_local.user = claims
return func(*args, **kwargs)
return wrapper
return decorator
def current_user(self):
"""Get the current authenticated user claims."""
return getattr(_thread_local, "user", None)
def _authenticate_request(self):
"""Extract and verify token from current request."""
# Flask
try:
from flask import request
auth_header = request.headers.get("Authorization", "")
except (ImportError, RuntimeError):
auth_header = ""
if not auth_header.startswith("Bearer "):
raise AuthFIError("Missing authorization")
token = auth_header[7:]
return self.verify_token(token)
# --- API calls ---
def _api_call(self, method, path, body=None):
url = f"{self._manage_url}{path}"
req = Request(url, method=method)
req.add_header("X-API-Key", self.api_key)
req.add_header("Content-Type", "application/json")
data = json.dumps(body).encode() if body else None
try:
with urlopen(req, data) as res:
return json.loads(res.read())
except HTTPError as e:
error_body = json.loads(e.read()) if e.readable() else {}
raise Exception(error_body.get("error", f"HTTP {e.code}"))
# --- Cloud Identity ---
def cloud_credentials(self, user_token, provider, role_arn=None, project=None, scope=None, ttl=900):
"""Get cloud provider credentials using AuthFI identity.
Requires application_id and client_secret in config. Cloud credentials
are scoped per application — each app can only access resources its IAM role permits.
Args:
user_token: The user's AuthFI JWT
provider: 'aws', 'gcp', or 'azure'
role_arn: AWS IAM role ARN (required for AWS)
project: GCP project ID
scope: Azure scope
ttl: Token TTL in seconds (max 3600)
Returns:
dict with cloud credentials (short-lived)
Usage:
creds = auth.cloud_credentials(token, 'aws', role_arn='arn:aws:iam::123:role/deploy')
# Use creds['access_key_id'], creds['secret_access_key']
"""
if not self.application_id or not self.client_secret:
raise AuthFIError("application_id and client_secret required for cloud credentials")
url = f"{self.api_url}/v1/{self.tenant}/cloud/credentials"
body = {"provider": provider, "ttl": ttl}
if role_arn: body["role_arn"] = role_arn
if project: body["project"] = project
if scope: body["scope"] = scope
req = Request(url, method="POST")
req.add_header("Content-Type", "application/json")
req.add_header("Authorization", f"Bearer {user_token}")
req.add_header("X-Client-ID", self.application_id)
req.add_header("X-Client-Secret", self.client_secret)
try:
with urlopen(req, json.dumps(body).encode()) as res:
return json.loads(res.read())
except HTTPError as e:
error_body = json.loads(e.read()) if e.readable() else {}
raise AuthFIError(error_body.get("error", f"Cloud credentials failed: {e.code}"))
def cloud_token(self, user_token, audience, ttl=900):
"""Get a raw OIDC token for manual federation.
Requires application_id and client_secret in config.
"""
if not self.application_id or not self.client_secret:
raise AuthFIError("application_id and client_secret required for cloud token")
url = f"{self.api_url}/v1/{self.tenant}/cloud/token"
req = Request(url, method="POST")
req.add_header("Content-Type", "application/json")
req.add_header("Authorization", f"Bearer {user_token}")
req.add_header("X-Client-ID", self.application_id)
req.add_header("X-Client-Secret", self.client_secret)
data = json.dumps({"audience": audience, "ttl": ttl}).encode()
with urlopen(req, data) as res:
return json.loads(res.read())
def _b64decode(s):
"""Base64url decode with padding."""
s += "=" * (4 - len(s) % 4)
return base64.urlsafe_b64decode(s)