-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpie_croit.py
executable file
·43 lines (37 loc) · 1.13 KB
/
httpie_croit.py
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
"""
croit auth plugin for HTTPie
"""
from httpie.plugins import AuthPlugin
from urllib.parse import urlsplit, urlunsplit
import requests
__version__ = '1.0.0'
__author__ = 'Paul Emmerich'
__licence__ = 'BSD'
"""
Implements croit authentication which is basically just a
OAuth2 Bearer token acquired via client_credentials.
"""
class CroitAuth:
def __init__(self, username, password):
self.username = username
self.password = password
def __call__(self, req):
token = self.get_token(req)
req.headers['Authorization'] = 'Bearer ' + token
return req
def get_token(self, orig_req):
url = list(urlsplit(orig_req.url))
url[2] = '/api/auth/login'
url = urlunsplit(url)
req = requests.post(
url,
data={'grant_type': 'client_credentials'},
auth=(self.username, self.password)
)
return req.json()['access_token']
class CroitAuthPlugin(AuthPlugin):
name = 'croit auth'
auth_type = 'croit'
description = 'croit API authentication'
def get_auth(self, username, password):
return CroitAuth(username, password)