Skip to content

Commit afaeefd

Browse files
committed
Add support of bearer authentication (closes #8)
1 parent 3b40185 commit afaeefd

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ SPDX-License-Identifier: GPL-3.0-or-later
66

77
# Changelog
88

9+
## [Unreleased]
10+
11+
### Added
12+
13+
- Import `bearer` authentication from collection ([#8](https://github.com/flashcode/impostman/issues/8))
14+
915
## [0.2.0] - 2021-12-11
1016

1117
### Added
@@ -23,5 +29,6 @@ SPDX-License-Identifier: GPL-3.0-or-later
2329

2430
- First release
2531

32+
[Unreleased]: https://github.com/flashcode/impostman/compare/v0.2.0...HEAD
2633
[0.2.0]: https://github.com/flashcode/impostman/compare/v0.1.0...v0.2.0
2734
[0.1.0]: https://github.com/flashcode/impostman/releases/tag/v0.1.0

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
SPDX-FileCopyrightText: 2021-2025 Sébastien Helleu <flashcode@flashtux.org>
2+
SPDX-FileCopyrightText: 2021-2026 Sébastien Helleu <flashcode@flashtux.org>
33
44
SPDX-License-Identifier: GPL-3.0-or-later
55
-->
@@ -256,15 +256,15 @@ Arguments:
256256

257257
For now the package offers a basic support of Postman collections and environments, the following features are partially implemented:
258258

259-
- authentication: only `basic` and `apikey` are supported
259+
- authentication: only `basic`, `bearer` and `apikey` are supported
260260
- body: only `raw` is supported.
261261

262262
Pull requests are welcome to add missing features.
263263

264264
## Copyright
265265

266266
<!-- REUSE-IgnoreStart -->
267-
Copyright © 2020-2025 [Sébastien Helleu](https://github.com/flashcode)
267+
Copyright © 2020-2026 [Sébastien Helleu](https://github.com/flashcode)
268268

269269
This program is free software; you can redistribute it and/or modify
270270
it under the terms of the GNU General Public License as published by

‎impostman.el‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
;;; impostman.el --- Import Postman collections -*- lexical-binding: t -*-
22

3-
;; SPDX-FileCopyrightText: 2020-2025 Sébastien Helleu <flashcode@flashtux.org>
3+
;; SPDX-FileCopyrightText: 2020-2026 Sébastien Helleu <flashcode@flashtux.org>
44

55
;; SPDX-License-Identifier: GPL-3.0-or-later
66

@@ -363,6 +363,18 @@ AUTH is a hash table."
363363
(push
364364
(cons "Authorization" (concat "Basic " auth-base64))
365365
headers)))))
366+
((string= auth-type "bearer")
367+
(let (token (bearer (gethash "bearer" auth [])))
368+
(dotimes (i (length bearer))
369+
(let* ((item (elt bearer i))
370+
(key (gethash "key" item ""))
371+
(value (gethash "value" item "")))
372+
(when (string= key "token")
373+
(setq token value))))
374+
(when token
375+
(push
376+
(cons "Authorization" (concat "Bearer " token))
377+
headers))))
366378
((string= auth-type "apikey")
367379
(let ((apikey (gethash "apikey" auth []))
368380
apikey-key apikey-value apikey-in)

‎tests/impostman-test.el‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
;;; impostman-test.el --- Test suite for impostman -*- lexical-binding: t -*-
22

3-
;; SPDX-FileCopyrightText: 2020-2025 Sébastien Helleu <flashcode@flashtux.org>
3+
;; SPDX-FileCopyrightText: 2020-2026 Sébastien Helleu <flashcode@flashtux.org>
44

55
;; SPDX-License-Identifier: GPL-3.0-or-later
66

@@ -514,6 +514,8 @@ FILENAME is a complete path to a file."
514514
"value" "user1")))
515515
(password #s(hash-table test equal data ("key" "password"
516516
"value" "Password!")))
517+
(token #s(hash-table test equal data ("key" "token"
518+
"value" "1a2b3c4d")))
517519
(apikey-key #s(hash-table test equal data ("key" "key"
518520
"value" "apikey")))
519521
(apikey-value #s(hash-table test equal data ("key" "value"
@@ -556,6 +558,17 @@ FILENAME is a complete path to a file."
556558
`(("Authorization" .
557559
,(concat "Basic "
558560
(base64-encode-string "user1:Password!"))))))
561+
;; test bearer
562+
(clrhash auth)
563+
(puthash "type" "bearer" auth)
564+
(should (equal (impostman--build-auth-headers auth)
565+
nil))
566+
(puthash "bearer" [] auth)
567+
(should (equal (impostman--build-auth-headers auth)
568+
nil))
569+
(puthash "bearer" (vector token) auth)
570+
(should (equal (impostman--build-auth-headers auth)
571+
'(("Authorization" . "Bearer 1a2b3c4d"))))
559572
;; test apikey
560573
(clrhash auth)
561574
(puthash "type" "apikey" auth)

0 commit comments

Comments
 (0)