WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log

## Unreleased
### Added
- Add support for User API token authentication with Bearer authorization header

## 0.6.2
### Changed
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ on kintone as customize script for kintone app or portal.
(auth/new-auth {:basic {:username "basic-username" :password "basic-password"}
:password {:username "login-name" :password "login-password"}
:api-token "xyz..."})

;; User API token (for cybozu.com User API)
(auth/new-auth {:user-api-token "xyz..."})

;; NOTE: Basic authentication and User API token cannot be used together
;; as they both set the Authorization header. Use one or the other.
```

### Make `Connection` object
Expand Down
25 changes: 18 additions & 7 deletions src/kintone_client/authentication.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
#?(:clj (.encodeToString (Base64/getEncoder) (.getBytes s))
:cljs (goog.base64/encodeString s)))

(defrecord Auth [basic password api-token]
(defrecord Auth [basic password api-token user-api-token]
pt/IAuth
(-header [_]
(cond-> {}
basic (assoc "Authorization" (str "Basic " basic))
password (assoc "X-Cybozu-Authorization" password)
api-token (assoc "X-Cybozu-API-Token" api-token))))
api-token (assoc "X-Cybozu-API-Token" api-token)
user-api-token (assoc "Authorization" (str "Bearer " user-api-token)))))

(defn new-auth
"Make a new Auth object.
Expand All @@ -32,17 +33,27 @@
{:username \"...\" :password \"...\"}

:api-token - kintone app api token.
string"
string

:user-api-token - cybozu.com User API token.
string

NOTE: :basic and :user-api-token cannot be used together as they both
set the Authorization header. Use one or the other."
#?(:cljs
([]
(->Auth nil nil nil)))
([{:keys [basic password api-token]}]
(->Auth nil nil nil nil)))
([{:keys [basic password api-token user-api-token]}]
(let [basic (when (and (seq (:username basic))
(seq (:password basic)))
(base64-encode (str (:username basic) ":" (:password basic))))
password (when (and (seq (:username password))
(seq (:password password)))
(base64-encode (str (:username password) ":" (:password password))))
api-token (when (seq api-token)
api-token)]
(->Auth basic password api-token))))
api-token)
user-api-token (when (seq user-api-token)
user-api-token)]
(assert (not (and basic user-api-token))
"Basic authentication and User API token cannot be used together")
(->Auth basic password api-token user-api-token))))
9 changes: 8 additions & 1 deletion test/kintone_client/authentication_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@
:password "barbar"}
:password {:username "foofoo"
:password "barbar"}
:api-token "XXXYYYXXX"})))))
:api-token "XXXYYYXXX"}))))

(is (= {"Authorization" "Bearer XXXYYYXXX"}
(pt/-header (new-auth {:user-api-token "XXXYYYXXX"}))))

(is (thrown? #?(:clj AssertionError :cljs js/Error)
(new-auth {:basic {:username "foofoo" :password "barbar"}
:user-api-token "XXXYYYXXX"}))))