-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathauth.py
More file actions
53 lines (35 loc) · 1.5 KB
/
auth.py
File metadata and controls
53 lines (35 loc) · 1.5 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
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, origin: str):
self.bearer = bearer
self.origin = origin
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):
super().__init__(bearer="basic//{t}".format(t=access_token), origin="basic")
class OidcBearerAuth(BearerAuth):
"""Bearer token for OIDC Auth (openEO API 1.0.0 style)"""
def __init__(self, provider_id: str, access_token: str):
super().__init__(bearer="oidc/{p}/{t}".format(p=provider_id, t=access_token), origin="oidc")