From 38076f446f64c23e9adb0e5321e4a303613ac5e1 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Thu, 6 Oct 2011 02:10:01 -0400 Subject: [PATCH] login might work if node-expat did --- README | 10 --------- README.markdown | 11 +++++++++ auth.coffee | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 76 insertions(+), 12 deletions(-) delete mode 100644 README create mode 100644 README.markdown diff --git a/README b/README deleted file mode 100644 index b22b84b..0000000 --- a/README +++ /dev/null @@ -1,10 +0,0 @@ -Setup -===== - -1. You'll need to save your auth credentials to ~/.libre.fm-cmus.auth: - - unset HISTFILE - coffee -e "require('./auth.coffee').save_auth 'USERNAME','PASSWORD'" - - (Substituting your libre.fm username and password of course.) The first - line disables history saving in bash, and perhaps other shells. diff --git a/README.markdown b/README.markdown new file mode 100644 index 0000000..b6d6e01 --- /dev/null +++ b/README.markdown @@ -0,0 +1,11 @@ +Setup +===== + +1. You'll need to save your auth credentials to ~/.libre.fm-cmus.auth: + + unset HISTFILE + coffee -e "require('./auth.coffee').save_auth 'USERNAME','PASSWORD'" + + (Substituting your libre.fm username and password of course.) The first + line disables history saving (in bash, and perhaps other shells) so your + password won't get into ~/.bash_history. diff --git a/auth.coffee b/auth.coffee index 619e169..833b553 100644 --- a/auth.coffee +++ b/auth.coffee @@ -1,18 +1,81 @@ fs = require 'fs' +http = require 'http' crypto = require 'crypto' +expat = require 'node-expat' + +auth = {} md5 = (str) -> sum = crypto.createHash 'md5' sum.update str return sum.digest 'hex' -new_auth_token = (user, pass) -> md5(user + md5(pass)) +new_auth_token = (user, pass) -> + token = md5(user + md5(pass)) + auth.user = user + auth.token = token + return token +auth_file = "#{process.env.HOME}/.libre.fm-cmus.auth" save_auth = (user, pass, callback) -> token = new_auth_token(user, pass) text = JSON.stringify user: user, token: new_auth_token(user, pass) - fs.writeFile "#{process.env.HOME}/.libre.fm-cmus.auth", text, 'utf8', callback + fs.writeFile auth_file, text, 'utf8', callback return token +# load login credentials from settings file +load_auth = (callback) -> + if auth.user and auth.token + callback auth + return + + fs.readFile auth_file, 'utf8', (err, data) -> + if err + callback err + else + callback null, JSON.parse data + +# login and get a session key +login = (callback) -> + load_auth (err, auth) -> + return callback(err) if err? + + http.get { host: 'alpha.libre.fm', port: 80, path: "/2.0/?method=auth.getmobilesession&username=#{auth.user}&authToken=#{auth.token}"}, (res) -> + if res.statusCode != 200 + console.log "login response code: #{res.statusCode}" + callback "login response code: #{res.statusCode}" + return + + res.setEncoding 'utf8' + body = '' + res.on 'data', (chunk) -> + body += chunk + res.on 'end', -> + parser = new expat.Parser 'UTF-8' + cur_tag = '' + done = false + parser.on 'startElement', (name, attrs) -> + cur_tag = name + parser.on 'text', (str) -> + if cur_tag is 'key' + done = true + auth.sk = str + console.log("got key \"#{str}\"") + callback null, str + else if cur_tag is 'error' + done = true + callback "login failed: #{str}" + # ignore other stuff in there + unless parser.parse body, true + done = true + callback "login server response wouldn't parse as XML." + console.log "login server sent us bad XML: #{body}" + + unless done + callback "Couldn't understand response from login server" + + + +exports.login = login # fixme remove this from the API and call it automatically exports.new_auth_token = new_auth_token exports.save_auth = save_auth -- 1.7.10.4