Skip to content

Commit

Permalink
Added support for login via config.
Browse files Browse the repository at this point in the history
  • Loading branch information
Duncan McGreggor committed Jan 1, 2014
1 parent c18249c commit 1f51cc1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
16 changes: 16 additions & 0 deletions README.rst
Expand Up @@ -166,6 +166,22 @@ In the presence of both defined env vars and cred files, env will allways be
the default source of truth and files will only be used in the absence of
defined env vars.

You may also login using credentials stored in a config/ini file. To use this
function, first create the ``~/.rax/providers.cfg`` config file with content
like the following, but you your own details substituted:

.. code:: ini
[rackspace]
username=alice
apikey=abc123

Then you can use the following call to login:

.. code:: common-lisp
> (: lferax-identity login 'config)
Login Response Data
-------------------
Expand Down
24 changes: 24 additions & 0 deletions src/lferax-config.lfe
@@ -0,0 +1,24 @@
(defmodule lferax-config
(export all))

(defun open ()
(: openstack-config open
(: lferax-const config-file)))

(defun get-value (key)
(: openstack-config open
(: lferax-const config-file))
(: openstack-config get-value
(: lferax-const config-provider) key))

(defun get-username ()
(get-value '"username"))

(defun get-password ()
(get-value '"password"))

(defun get-apikey ()
(get-value '"apikey"))

(defun get-auth-url ()
(get-value '"auth-url"))
2 changes: 2 additions & 0 deletions src/lferax-const.lfe
Expand Up @@ -36,6 +36,8 @@
(defun username-env () '"RAX_USERNAME")
(defun password-env () '"RAX_PASSWORD")
(defun apikey-env () '"RAX_APIKEY")
(defun config-file () (: filename join (list (dot-dir) '"providers.cfg")))
(defun config-provider () '"rackspace")

;; Rackspace Cloud Services
(defun services ()
Expand Down
32 changes: 15 additions & 17 deletions src/lferax-identity.lfe
Expand Up @@ -11,37 +11,31 @@
well. As such, all strings should be converted to binary before being passed
to Jiffy."
((username 'password password)
(json-wrap
(list 'passwordCredentials
(json-wrap-bin (list 'username username
'password password)))))
(: openstack-identity build-creds username 'password password))
((username 'apikey apikey)
(json-wrap
(list 'RAX-KSKEY:apiKeyCredentials
(json-wrap-bin (list 'username username
'apiKey apikey))))))

(defun get-password-auth-payload (username password)
(binary_to_list
(: jiffy encode
(json-wrap (list 'auth (build-creds username 'password password))))))

(defun get-apikey-auth-payload (username apikey)
(binary_to_list
(: jiffy encode
(json-wrap (list 'auth (build-creds username 'apikey apikey))))))

(defun get-auth-payload
((username 'apikey apikey) (get-apikey-auth-payload username apikey))
((username 'password password) (get-password-auth-payload username password)))
((username 'password password)
(: openstack-identity get-password-auth-payload username password)))

(defun password-login (username password)
(: lferax-http post
(: openstack-identity authenticate
(: lferax-const auth-url)
(get-auth-payload username 'password password)))
username
password))

(defun apikey-login (username apikey)
(: lferax-http post
(: openstack-http post
(: lferax-const auth-url)
(get-auth-payload username 'apikey apikey)))

Expand Down Expand Up @@ -91,15 +85,19 @@
((username 'apikey apikey) (apikey-login username apikey))
((username 'password password) (password-login username password)))

(defun login ()
""
(login (get-username) 'apikey (get-apikey)))

(defun login (mode)
""
(cond ((=:= mode 'apikey) (login))
((=:= mode 'config)
(let ((username (: lferax-config get-username))
(apikey (: lferax-config get-apikey)))
(login username 'apikey apikey)))
('true (login (get-username) 'password (get-password)))))

(defun login ()
""
(login (get-username) 'apikey (get-apikey)))

(defun get-token (identity-response)
(binary_to_list
(: ej get
Expand Down

0 comments on commit 1f51cc1

Please sign in to comment.