Skip to content

Commit

Permalink
implementation hawkauth (#151)
Browse files Browse the repository at this point in the history
* implementation hawkauth

* version bump
  • Loading branch information
cedric05 authored Oct 8, 2022
1 parent 4445648 commit b97a23a
Show file tree
Hide file tree
Showing 17 changed files with 292 additions and 131 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Js2Py = "==0.71"
requests_ntlm = "==1.1.0"
RestrictedPython = "==5.2"
Faker = "==13.11.1"
requests-hawk = "==1.1.1"

[dev-packages]
pyinstaller = "*"
Expand Down
263 changes: 141 additions & 122 deletions Pipfile.lock

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions dotextensions/server/handlers/http2postman.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Dict

from requests.auth import HTTPBasicAuth, HTTPDigestAuth
from requests_hawk import HawkAuth as RequestsHawkAuth
from dothttp import HttpNtlmAuth

from dotextensions.server.postman2_1 import FormParameterType, File, Mode, AuthType, Variable
Expand Down Expand Up @@ -219,6 +220,20 @@ def get_http_to_postman_request(http: HttpDef, description="") -> RequestClass:
value=auth.username,
type="string"
), ApikeyElement(key="password", value=auth.password, type="string")]
if isinstance(auth, RequestsHawkAuth):
request_auth = []
request.auth.hawk = request_auth
request.auth.type = AuthType.HAWK
hawk_id = auth.credentials.get("id", "")
hawk_key = auth.credentials.get("key", "")
hawk_algorithm = auth.credentials.get("algorithm", "")
request_auth += [ApikeyElement(
key="hawkId",
value=hawk_id,
type="string"
),
ApikeyElement(key="authKey", value=hawk_key, type="string"),
ApikeyElement(key="algorithm", value=hawk_algorithm, type="string")]
elif isinstance(auth, AWS4Auth):
request.auth.type = AuthType.AWSV4
request.auth.awsv4 = [
Expand Down
9 changes: 8 additions & 1 deletion dotextensions/server/handlers/postman2http.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from dothttp import Allhttp, Http, NameWrap, UrlWrap, Line, Query, Header, AuthWrap, BasicAuth, DigestAuth, \
MultiPartFile, FilesWrap, TripleOrDouble, Certificate
from dothttp.parse_models import NtlmAuthWrap, Payload, AwsAuthWrap, HttpFileType
from dothttp.parse_models import NtlmAuthWrap, Payload, AwsAuthWrap, HttpFileType, HawkAuth
from dothttp.request_base import HttpFileFormatter
from dothttp.utils import APPLICATION_JSON
from . import logger
Expand Down Expand Up @@ -209,6 +209,13 @@ def get_auth_wrap(request_auth):
auth_wrap = AuthWrap(
ntlm_auth=NtlmAuthWrap(username=ntlm_auth.get('username', ''),
password=ntlm_auth.get('password', '')))
elif hawk_auth := request_auth.hawk:
hawk_auth = ImportPostmanCollection.api_key_element_to_dict(hawk_auth)
# postman 2.0
auth_wrap = AuthWrap(
hawk_auth=HawkAuth(hawk_id=hawk_auth.get('authId', ''),
key=hawk_auth.get('authKey', ''),
algorithm=hawk_auth.get('algorithm', '')))
elif request_auth.apikey:
d = ImportPostmanCollection.api_key_element_to_dict(request_auth.apikey)
key = d.get('key', '<key>')
Expand Down
17 changes: 16 additions & 1 deletion dothttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from requests import PreparedRequest
from requests.auth import HTTPBasicAuth, HTTPDigestAuth, AuthBase
from requests.structures import CaseInsensitiveDict
from requests_hawk import HawkAuth as RequestsHawkAuth

from .utils import get_real_file_path, triple_or_double_tostring, APPLICATION_JSON, json_to_urlencoded_array

Expand All @@ -38,7 +39,7 @@
from .exceptions import *
from .parse_models import Allhttp, AuthWrap, DigestAuth, BasicAuth, Line, NtlmAuthWrap, Query, Http, NameWrap, UrlWrap, Header, \
MultiPartFile, FilesWrap, TripleOrDouble, Payload as ParsePayload, Certificate, P12Certificate, ExtraArg, \
AWS_REGION_LIST, AWS_SERVICES_LIST, AwsAuthWrap, TestScript, ScriptType
AWS_REGION_LIST, AWS_SERVICES_LIST, AwsAuthWrap, TestScript, ScriptType, HawkAuth
from .property_schema import property_schema
from .property_util import PropertyProvider

Expand Down Expand Up @@ -266,6 +267,11 @@ def get_http_from_req(self):
auth_wrap = AuthWrap(digest_auth=DigestAuth(self.auth.username, self.auth.password))
elif isinstance(self.auth, HttpNtlmAuth):
auth_wrap = AuthWrap(ntlm_auth=NtlmAuthWrap(self.auth.username, self.auth.password))
elif isinstance(self.auth, RequestsHawkAuth):
hawk_id = self.auth.credentials['id']
hawk_key = self.auth.credentials['key']
hawk_algorithm = self.auth.credentials['algorithm']
auth_wrap = AuthWrap(hawk_auth=HawkAuth(hawk_id, hawk_key, hawk_algorithm))
elif isinstance(self.auth, AWS4Auth):
aws_auth: AWS4Auth = self.auth
auth_wrap = AuthWrap(
Expand Down Expand Up @@ -717,6 +723,15 @@ def load_auth(self):
elif ntlm_auth := auth_wrap.ntlm_auth:
self.httpdef.auth = HttpNtlmAuth(self.get_updated_content(ntlm_auth.username),
self.get_updated_content(ntlm_auth.password))
elif hawk_auth := auth_wrap.hawk_auth:
if hawk_auth.algorithm:
algorithm = hawk_auth.algorithm
else:
algorithm = "sha256"
self.httpdef.auth = RequestsHawkAuth(
id=self.get_updated_content(hawk_auth.id),
key=self.get_updated_content(hawk_auth.key),
algorithm=self.get_updated_content(algorithm))
elif aws_auth_wrap := auth_wrap.aws_auth:
access_id = self.get_updated_content(aws_auth_wrap.access_id)
secret_token = self.get_updated_content(aws_auth_wrap.secret_token)
Expand Down
2 changes: 1 addition & 1 deletion dothttp/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.41'
__version__ = '0.0.42.a1'
7 changes: 5 additions & 2 deletions dothttp/http.tx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ HEADER:
;

AUTHWRAP:
digest_auth = DIGESTAUTH | basic_auth = BASICAUTH | ntlm_auth = NTLMAUTH | aws_auth = AWSAUTH
digest_auth = DIGESTAUTH | basic_auth = BASICAUTH | ntlm_auth = NTLMAUTH | hawk_auth=HAWKAUTH | aws_auth = AWSAUTH
;

CERTAUTH:
Expand All @@ -53,10 +53,13 @@ CERTAUTH:
)
;

HAWKAUTH:
'hawkauth' '(' id=DotString ',' key = DotString (',' algorithm=DotString)? ')'
;


NTLMAUTH:
'ntlmauth' '(' username=DotString ',' password= DotString ')'

;


Expand Down
9 changes: 7 additions & 2 deletions dothttp/parse_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class DigestAuth:
username: str
password: str

@dataclass
class HawkAuth:
hawk_id: str
key: str
algorithm: Optional[str] = None


@dataclass
class AwsAuthWrap:
Expand All @@ -50,12 +56,11 @@ class NtlmAuthWrap:

@dataclass
class AuthWrap:
# digest = DIGESTAUTH | basicauth = BASICAUTH
digest_auth: Optional[DigestAuth] = None
basic_auth: Optional[BasicAuth] = None
aws_auth: Optional[AwsAuthWrap] = None
ntlm_auth: Optional[NtlmAuthWrap] = None

hawk_auth: Optional[HawkAuth] = None

@dataclass
class Query:
Expand Down
2 changes: 2 additions & 0 deletions dothttp/request_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ def format_http(http: Http):
output_str += f'{new_line}digestauth("{digest_auth.username}", "{digest_auth.password}")'
elif ntlm_auth := auth_wrap.ntlm_auth:
output_str += f'{new_line}ntlmauth("{ntlm_auth.username}", "{ntlm_auth.password}")'
elif hawk_auth := auth_wrap.hawk_auth:
output_str += f'{new_line}hawkauth("{hawk_auth.hawk_id}", "{hawk_auth.key}", "{hawk_auth.algorithm}")'
elif aws_auth := auth_wrap.aws_auth:
if aws_auth.service and aws_auth.region:
output_str += f'{new_line}awsauth(access_id="{aws_auth.access_id}", secret_key="{aws_auth.secret_token}", service="{aws_auth.service}", region="{aws_auth.region}")'
Expand Down
4 changes: 4 additions & 0 deletions examples/auth/hawkauth.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@name('hawk auth')
GET https://postman-echo.com/auth/hawk
hawkauth('dh37fgj492je','werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn')
json({})
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ requests-unixsocket==0.2.0
requests-aws4auth==1.1.1
requests_ntlm==1.1.0
RestrictedPython==5.2
Faker==13.11.1
Faker==13.11.1
requests-hawk==1.1.1
4 changes: 4 additions & 0 deletions test/core/requests/hawkauth.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@name('hawk auth')
GET https://postman-echo.com/auth/hawk
hawkauth('dh37fgj492je','werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn')
json({})
5 changes: 5 additions & 0 deletions test/core/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ def test_ntlm_auth_integration(self):
resp = req_comp.get_response()
self.assertEqual(200, resp.status_code)

def test_hawk_auth_integration(self):
filename = f"{base_dir}/hawkauth.http"
req_comp = self.get_req_comp(filename, target=1)
resp = req_comp.get_response()
self.assertEqual(200, resp.status_code)



Expand Down
67 changes: 67 additions & 0 deletions test/extensions/commands/http2postman/hawk.postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"collection": {
"auth": null,
"event": null,
"info": {
"_postman_id": null,
"description": null,
"name": "hawkauth.http",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"version": null
},
"item": [
{
"description": null,
"event": null,
"id": null,
"name": "hawk auth",
"protocolProfileBehavior": null,
"request": {
"auth": {
"apikey": null,
"awsv4": null,
"basic": null,
"bearer": null,
"digest": null,
"edgegrid": null,
"hawk": [
{
"key": "hawkId",
"type": "string",
"value": "dh37fgj492je"
},
{
"key": "authKey",
"type": "string",
"value": "werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn"
},
{
"key": "algorithm",
"type": "string",
"value": "sha256"
}
],
"noauth": null,
"ntlm": null,
"oauth1": null,
"oauth2": null,
"type": "hawk"
},
"body": null,
"certificate": null,
"description": "hawk auth",
"header": null,
"method": "GET",
"proxy": null,
"url": "https://postman-echo.com/auth/hawk"
},
"response": null,
"variable": null,
"auth": null,
"item": null
}
],
"protocolProfileBehavior": null,
"variable": null
}
}
4 changes: 4 additions & 0 deletions test/extensions/commands/http2postman/hawkauth.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@name('hawk auth')
GET https://postman-echo.com/auth/hawk
hawkauth('dh37fgj492je','werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn')
json({})
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{"files": {"hawkAuthTest\\imported_from_collection.http": "#!/usr/bin/env dothttp\n\n# imported from https://raw.githubusercontent.com/postmanlabs/newman/v5.2.2/test/integration/hawk-auth-test.postman_collection.json\n\n@name(\"test hawk auth success\")\nGET \"https://postman-echo.com/auth/hawk\"\n\"Authorization\": 'Hawk id=\"dh37fgj492je\", ts=\"1448888081\", nonce=\"HoH6Ay\", ext=\"skjdfklsjhdflkjhsdf\", mac=\"moWleO5f/8QbvIiy7oo2zj1bmezhrYwrCkz4BsXg0M4=\"'\n\n\n\n\n"}}
{
"files": {
"hawkAuthTest\\imported_from_collection.http": "#!/usr/bin/env dothttp\n\n# imported from https://raw.githubusercontent.com/postmanlabs/newman/v5.2.2/test/integration/hawk-auth-test.postman_collection.json\n\n@name(\"test hawk auth success\")\nGET \"https://postman-echo.com/auth/hawk\"\nhawkauth(\"dh37fgj492je\", \"werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn\", \"sha256\")\n\"Authorization\": 'Hawk id=\"dh37fgj492je\", ts=\"1448888081\", nonce=\"HoH6Ay\", ext=\"skjdfklsjhdflkjhsdf\", mac=\"moWleO5f/8QbvIiy7oo2zj1bmezhrYwrCkz4BsXg0M4=\"'\n\n\n\n\n"
}
}
5 changes: 5 additions & 0 deletions test/extensions/test_http2postman.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,8 @@ def test_folder(self):
def test_ntlm_auth(self):
self.execute_n_get(http2postman_dir.joinpath("ntlmauth.postman_collection.json"),
os.path.join(http2postman_dir, "ntlm_auth.http"))


def test_hawk_auth(self):
self.execute_n_get(http2postman_dir.joinpath("hawk.postman_collection.json"),
os.path.join(http2postman_dir, "hawkauth.http"))

0 comments on commit b97a23a

Please sign in to comment.