-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathauth.py
More file actions
57 lines (38 loc) · 1.63 KB
/
auth.py
File metadata and controls
57 lines (38 loc) · 1.63 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
from requests import Request
from requests.auth import AuthBase
class OpenEoApiAuthBase(AuthBase):
"""
Base class for authentication with the OpenEO REST API.
Follows the authentication approach of the requests library:
an auth object is a callable object that can be passed with get/post request
to manipulate this request (typically setting headers).
"""
def __call__(self, req: Request) -> Request:
# Do nothing by default
return req
class NullAuth(OpenEoApiAuthBase):
"""No authentication"""
pass
class BearerAuth(OpenEoApiAuthBase):
"""
Requests are authenticated through a bearer token
https://open-eo.github.io/openeo-api/apireference/#section/Authentication/Bearer
"""
def __init__(self, bearer: str):
self.bearer = bearer
def __call__(self, req: Request) -> Request:
# Add bearer authorization header.
req.headers["Authorization"] = "Bearer {b}".format(b=self.bearer)
return req
class BasicBearerAuth(BearerAuth):
"""Bearer token for Basic Auth (openEO API 1.0.0 style)"""
def __init__(self, access_token: str, jwt_conformance: bool = False):
if not jwt_conformance:
access_token = "basic//{t}".format(t=access_token)
super().__init__(bearer=access_token)
class OidcBearerAuth(BearerAuth):
"""Bearer token for OIDC Auth (openEO API 1.0.0 style)"""
def __init__(self, provider_id: str, access_token: str, jwt_conformance: bool = False):
if not jwt_conformance:
access_token = "oidc/{p}/{t}".format(p=provider_id, t=access_token)
super().__init__(bearer=access_token)